Java Reference ยท Text, Formatting and Regex
The SecureRandom class
Reference for java.security.SecureRandom: cryptographically strong random bytes, tokens and numbers, algorithms, seeding and blocking myths, and safe usage.
java.security.SecureRandom is Java's cryptographically strong random number generator. Use it whenever unpredictability matters: session tokens, password-reset codes, salts, initialization vectors, encryption keys, nonces. It draws entropy from the operating system, and its output cannot be predicted from earlier output the way Random can.
SecureRandom extends Random, so all the usual methods (nextInt(bound), nextBytes, ...) work, and it implements RandomGenerator. Its output is different on every run; do not try to seed it for reproducibility.
SecureRandom secure = new SecureRandom(); byte[] token = new byte[32]; secure.nextBytes(token);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public class SecureRandom extends Random public SecureRandom() public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException public static SecureRandom getInstanceStrong() throws NoSuchAlgorithmException
| Member | Description |
|---|---|
new SecureRandom() |
Generator with the platform's default algorithm, self-seeded from the OS. |
getInstance(String) |
Generator for a named algorithm (for example "SHA1PRNG", "DRBG", "NativePRNG"). |
getInstanceStrong() |
Generator from the securerandom.strongAlgorithms security property. May block on some systems. |
nextBytes(byte[]) |
Fills the array with secure random bytes. |
nextInt(bound), nextLong(), nextDouble() |
Inherited convenience methods, all secure. |
generateSeed(int) |
Returns seed bytes from the entropy source (can be slow). |
getAlgorithm() |
Name of the algorithm in use. |
reseed() |
Reseeds from the system entropy source (since Java 9). |
Throws: NoSuchAlgorithmException when an algorithm name is unknown.
Usage notes
- Never use
RandomorMath.random()for secrets. Their output can be reconstructed from a handful of values. - Do not seed it yourself. Calling
setSeedon a default instance only supplements the existing entropy, but passing a weak seed to some algorithms makes the output predictable. Leave seeding to the platform. - Create one instance and reuse it. It is thread-safe. Constructing many instances is wasteful and, on some platforms, can drain entropy.
- Blocking. On modern JDKs the default generator does not block waiting for entropy.
getInstanceStrong()may, on some Linux configurations, so use it for long-lived keys rather than per-request values. - Rejection of bias:
nextInt(bound)is unbiased. Do not computenextInt() % nyourself. - Output encoding: turn bytes into text with
HexFormatorBase64(see HexFormat and Base64), not withnew String(bytes). - Passwords: hashing a password needs a slow hash such as PBKDF2, bcrypt or Argon2;
SecureRandomis only for the salt.
Related
Random,RandomGenerator,UUID- Tutorial: Random numbers