In this lesson, I will tell you how to generate a cryptographically secure random number in Python. Random numbers and data generated by the random class are not cryptographically protected. An output of all random module functions is not cryptographically secure, whether it is used to create a random number or pick random elements from a sequence.
What is cryptographically secure pseudo-random number generator?
A cryptographically secure pseudo-random number generator is a random number generator that generates the random number or data using synchronization methods so that no two processes can obtain the same random number simultaneously.
Also, see: –
A secure random generator is useful in cryptography applications where data security is essential. Most cryptographic applications require safe random numbers and String. For example, key and secrets generation, nonces, OTP, Passwords, PINs, secure tokens, and URLs.
In this lesson, you’ll learn the following ways to cryptographically secure random number generators in Python
- The
os.urandom()method - The
random.SystemRandomclass - Python 3.6’s Secrets module to secure random data
Table of contents
os.urandom() function
The os.urandom() returns a string of size random bytes suitable for cryptographic use.
It can returns a string and random bytes. Random bytes returned by this function depend on the underlying operating system’s random data source (OS.randoms). The quality of randomness is different for each operating system.
- On Windows,
os.urandom()internally uses CryptGenRandom() - Linux 3.17 and newer, the
getrandom()syscall is used when available. On OpenBSD 5.6 and newer, the Cgetentropy()function is used.
The data returned by the os.urandom() is enough for cryptographic applications.
Example
The os.urandom() generates a string of random bytes. Use the struct module to convert bytes into the format you want such as integer, float or string.
- The
struct.unpack(format, buffer)method is used to convert bytes into the format you want – for example,ifor integer, andffor float. - A buffer is the source of bytes. In our case, it is
os.urandom().
Note: the struct.unpack(format, buffer) returns the result in tuple format.
Let see the example to convert os.urandom() to string, integer, and float.
Use SystemRandom class to cryptographically secure the random generator
Instead of doing the conversion on your own, you can directly use random.SystemRandom class. The SystemRandom class internally uses os.urandom() function to provide the secure random numbers.
SystemRandom class internally uses the os.urandom() function for generating random numbers from sources provided by the operating system.
Use the random module to get this class. Use random.SystemRandom() function to get the instance of SystemRandom class. Using this instance, you can call all random module functions to secure your random data.
Syntax of SystemRandom class
random.SystemRandom([seed])Code language: Python (python)
- The
seed()method has no effect and is ignored. - The
random.getState()andrandom.setState()function is not available under this class and raisesNotImplementedErrorif called.
Examples
Let see how to use random.SystemRandom to generate cryptographically secure random numbers.
Let’s see the example to secure the output of the functions of the random module such as random(), randint(), randrange(), choice, sample(), uniform().
Secrets module to secure random data
Python 3.6 introduced a new module called secrets for generating a reliable, secure random number, URLs, and tokens.
Refer to our complete guide on Secrets Module to explore this module in detail.
Example
Next Steps
Try to solve the following exercise and quiz to have a better understanding of working with random data in Python.
- Python random data generation Exercise to practice and master the random data generation techniques in Python.
- Python random data generation Quiz to test your random data generation concepts.
