Random numbers

We can use three functions in order to deal with random numbers:

# secrs/secr_rand.py
import secrets
print(secrets.choice('Choose one of these words'.split()))
print(secrets.randbelow(10 ** 6))
print(secrets.randbits(32))

The first one, choice, picks an element at random from a non-empty sequence. The second one, randbelow, generates a random integer between 0 and the argument you call it with, and the third one, randbits, generates an integer with n random bits in it. Running that code produces the following output (which is always different):

$ python secr_rand.py
one
504156
3172492450

You should use these functions instead of those from the random module whenever you need randomness in the context of cryptography, as these are specially designed for this task. Let's see what the module gives us for tokens.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset