-# Get os.urandom PRNG
-try:
- os.urandom(1)
- def getRandomBytes(howMany):
- return stringToBytes(os.urandom(howMany))
- prngName = "os.urandom"
-
-except:
- # Else get cryptlib PRNG
- if cryptlibpyLoaded:
- def getRandomBytes(howMany):
- randomKey = cryptlib_py.cryptCreateContext(cryptlib_py.CRYPT_UNUSED,
- cryptlib_py.CRYPT_ALGO_AES)
- cryptlib_py.cryptSetAttribute(randomKey,
- cryptlib_py.CRYPT_CTXINFO_MODE,
- cryptlib_py.CRYPT_MODE_OFB)
- cryptlib_py.cryptGenerateKey(randomKey)
- bytes = createByteArrayZeros(howMany)
- cryptlib_py.cryptEncrypt(randomKey, bytes)
- return bytes
- prngName = "cryptlib"
-
- else:
- #Else get UNIX /dev/urandom PRNG
- try:
- devRandomFile = open("/dev/urandom", "rb")
- def getRandomBytes(howMany):
- return stringToBytes(devRandomFile.read(howMany))
- prngName = "/dev/urandom"
- except IOError:
- #Else get Win32 CryptoAPI PRNG
- try:
- import win32prng
- def getRandomBytes(howMany):
- s = win32prng.getRandomBytes(howMany)
- if len(s) != howMany:
- raise AssertionError()
- return stringToBytes(s)
- prngName ="CryptoAPI"
- except ImportError:
- #Else no PRNG :-(
- def getRandomBytes(howMany):
- raise NotImplementedError("No Random Number Generator "\
- "available.")
- prngName = "None"
+# Check that os.urandom works
+import zlib
+length = len(zlib.compress(os.urandom(1000)))
+assert(length > 900)
+
+def getRandomBytes(howMany):
+ b = bytearray(os.urandom(howMany))
+ assert(len(b) == howMany)
+ return b
+
+prngName = "os.urandom"