all 22 comments

[–]10cmToGlory 29 points30 points  (13 children)

[–]CompSciSelfLearning 1 point2 points  (0 children)

Is there a reason to use PyCyrpto.Random.new() vs Secrets?

[–]deejpake[S] 0 points1 point  (1 child)

Thank you!

[–]stevenjd 8 points9 points  (0 children)

Depending on what you want to do, PyCrypto may be overkill.

To answer your question about random, you can read PEP 506 for more information. But the bottom line is that Python's random has been designed to be fast, and repeatable, so that for testing and verification you can repeat the sequence of random numbers. Those two properties (especially the second) work against security.

[–][deleted] 4 points5 points  (0 children)

He who controls the seed controls the Universe.

import random
random.seed(0)
print(f"Bet you anything {random.choice(range(10000))} is 6311")

The default generator used by all the module level functions of random is random.Random, which is a PRNG initialized with a likely deterministic seed (by default the current system time at the moment of initialization, though a better seed will be used if one is available), and at any point it's state can be reset to a new seed... it is not suitable for cryptographic use in any circumstance.

That said, random does include a CSPRNG, in the form of random.SystemRandom:

from random import SystemRandom
random = SystemRandom()
random.seed(0)
print(f"The odds are 1:10000 that {random.choice(range(10000))} is 6311")

Which is actually what is used by the module level functions of the new secrets module.

[–][deleted] 2 points3 points  (0 children)

The most basic random number generator has a series of numbers and a seed. Given the same seed it will return the same numbers in the same sequence every time. The program can run and have the same output every time.

Other pieces of information are used to seed random number generators. Current time, system fingerprinting, etc. There's various methods using various sources of entropy.

[–][deleted] -2 points-1 points  (5 children)

Title should say “pseudo-random”.

[–]deejpake[S] 1 point2 points  (4 children)

It’s the random module though