diff options
author | Dwayne C. Litzenberger <dlitz@dlitz.net> | 2009-08-28 12:04:58 -0400 |
---|---|---|
committer | Dwayne C. Litzenberger <dlitz@dlitz.net> | 2009-08-28 12:10:25 -0400 |
commit | d5d88413fc9703249a87a193cc7e6e22d1c553e6 (patch) | |
tree | 3181fc8d2c15b8d1fd83cb57ad20dc10a86b4c14 /lib/Crypto/Random | |
parent | a94b21dcb3c8f0213a84f776ad689cebe28f1233 (diff) | |
download | pycrypto-d5d88413fc9703249a87a193cc7e6e22d1c553e6.tar.gz |
Random: Add Crypto.Random.get_random_bytes()
This should allow people to use something like this if they want
backwards-compatibility:
try:
from Crypto.Random import get_random_bytes
except ImportError:
try:
from os import urandom as get_random_bytes
except ImportError:
get_random_bytes = open("/dev/urandom", "rb").read
Diffstat (limited to 'lib/Crypto/Random')
-rw-r--r-- | lib/Crypto/Random/_UserFriendlyRNG.py | 4 | ||||
-rw-r--r-- | lib/Crypto/Random/__init__.py | 4 |
2 files changed, 8 insertions, 0 deletions
diff --git a/lib/Crypto/Random/_UserFriendlyRNG.py b/lib/Crypto/Random/_UserFriendlyRNG.py index 104750c..58b2246 100644 --- a/lib/Crypto/Random/_UserFriendlyRNG.py +++ b/lib/Crypto/Random/_UserFriendlyRNG.py @@ -198,4 +198,8 @@ def new(): def reinit(): _get_singleton().reinit() +def get_random_bytes(n): + """Return the specified number of cryptographically-strong random bytes.""" + return _get_singleton().read(n) + # vim:set ts=4 sw=4 sts=4 expandtab: diff --git a/lib/Crypto/Random/__init__.py b/lib/Crypto/Random/__init__.py index 6230809..0d0e6f2 100644 --- a/lib/Crypto/Random/__init__.py +++ b/lib/Crypto/Random/__init__.py @@ -36,4 +36,8 @@ def atfork(): """Call this whenever you call os.fork()""" _UserFriendlyRNG.reinit() +def get_random_bytes(n): + """Return the specified number of cryptographically-strong random bytes.""" + return _UserFriendlyRNG.get_random_bytes(n) + # vim:set ts=4 sw=4 sts=4 expandtab: |