Python's pycrypto library for random number generation vs os.urandom

Viewed 5144

I was trying to understand and figure out if I should use os.urandom() or Crypto.Random.new() for cryptographically secure pseudo-random numbers.

The following website seems to suggest to use os.urandom():

https://github.com/mozilla/PyHawk/pull/13

but I don't really see why and the other websites I found online don't really explain which one to use.

If anyone knows which one is secure for cryptographic use, I would appreciate the advice!

One of the things that specifically worry me is that, I plan to use it to generate random numbers but I am worried that one day I generate some random numbers and the next day the state of the generator is the same and it starts to generate the same "random" numbers.

4 Answers

From Python 3.6, the secrets module, which is based on os.urandom and random.SystemRandom, is the suggested secured random number generator.

Warning: Note that the pseudo-random generators in the random module should NOT be used for security purposes. Use secrets on Python 3.6+ and os.urandom() on Python 3.5 and earlier.

PEP 506 has a detailed discussion on this.

Related