Random Generators
Overview
This page contains recommendations for generating random values.
General
Use only cryptographically strong random generators to produce random values, see the Cryptographically strong random generators section.
Do not use standard pseudo-random number generators, such as those used in mathematics to generate pseudo-random numbers.
Make sure the seed that is used to initialize a generator has enough entropy.
Periodically reinitialize seeds.
Cryptographically strong random generators
Use the crypto/rand package to generate cryptographically strong random values in Go.
Use the java.security.SecureRandom class to generate cryptographically strong random values.
Starting with JDK8, use SecureRandom.getInstanceStrong() to get a SecureRandom instance that implements a strong generation algorithm. For a UNIX-like OS, the default strong generation algorithm is NativePRNGBlocking, which is based on /dev/random. As a result, SecureRandom.getInstanceStrong() will return a SecureRandom implementation that can block a thread when the generateSeed or nextBytes methods are called. Use NativePRNGNonBlocking to avoid possible thread blocking.
For older versions of the JDK for UNIX-like OS, create a SecureRandom instance using the default constructor. However, in this case, calling the generateSeed method may cause thread blocking. For Windows, use Windows-PRNG.
Avoid using the SHA1PRNG generation algorithm. If it is necessary to use SHA1PRNG:
Generate random values of length <= 20 bytes.
Create a new instance of
SecureRandomfor each generation.
Use the crypto package to generate cryptographically strong random values.
Use the secrets package to generate cryptographically strong random values.
Last updated