| 1 |
""" |
| 2 |
PRNG management routines, thin wrappers. |
| 3 |
""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import warnings |
| 8 |
|
| 9 |
from OpenSSL._util import lib as _lib |
| 10 |
|
| 11 |
warnings.warn( |
| 12 |
"OpenSSL.rand is deprecated - you should use os.urandom instead", |
| 13 |
DeprecationWarning, |
| 14 |
stacklevel=3, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def add(buffer: bytes, entropy: int) -> None: |
| 19 |
""" |
| 20 |
Mix bytes from *string* into the PRNG state. |
| 21 |
|
| 22 |
The *entropy* argument is (the lower bound of) an estimate of how much |
| 23 |
randomness is contained in *string*, measured in bytes. |
| 24 |
|
| 25 |
For more information, see e.g. :rfc:`1750`. |
| 26 |
|
| 27 |
This function is only relevant if you are forking Python processes and |
| 28 |
need to reseed the CSPRNG after fork. |
| 29 |
|
| 30 |
:param buffer: Buffer with random data. |
| 31 |
:param entropy: The entropy (in bytes) measurement of the buffer. |
| 32 |
|
| 33 |
:return: :obj:`None` |
| 34 |
""" |
| 35 |
if not isinstance(buffer, bytes): |
| 36 |
raise TypeError("buffer must be a byte string") |
| 37 |
|
| 38 |
if not isinstance(entropy, int): |
| 39 |
raise TypeError("entropy must be an integer") |
| 40 |
|
| 41 |
_lib.RAND_add(buffer, len(buffer), entropy) |
| 42 |
|
| 43 |
|
| 44 |
def status() -> int: |
| 45 |
""" |
| 46 |
Check whether the PRNG has been seeded with enough data. |
| 47 |
|
| 48 |
:return: 1 if the PRNG is seeded enough, 0 otherwise. |
| 49 |
""" |
| 50 |
return _lib.RAND_status() |