public final class RandomUtil extends Object
The current implementation uses a pool SecureRandom.
The pool size depends on the number of processors available, up to a maximum of 32. The minimum is 4.
The pool items are deleted very often to avoid holding them for too long. They are also deleted to avoid holding more instances than threads running.
The PRNG algorithm can be specified by system property or environment
variable. See newSecureRandom().
| Modifier and Type | Method and Description |
|---|---|
static SecureRandom |
newSecureRandom()
Returns a new instance of
SecureRandom. |
static byte[] |
nextBytes(int length)
Returns an array of random bytes.
|
static long |
nextLong()
Returns a random 64-bit number.
|
public static long nextLong()
public static byte[] nextBytes(int length)
length - the array lengthpublic static SecureRandom newSecureRandom()
SecureRandom.
It tries to create an instance with the algorithm name specified in the system property `uuidcreator.securerandom` or in the environment variable `UUIDCREATOR_SECURERANDOM`. If the algorithm name is not supported by the runtime, it returns an instance with the default algorithm.
It can be useful to make use of SHA1PRNG or DRBG as a non-blocking source of random bytes. The SHA1PRNG algorithm is default on operating systems that don't have '/dev/random', e.g., on Windows. The DRBG algorithm is available in JDK 9+.
To control the algorithm used by this method, use the system property `uuidcreator.securerandom` or the environment variable `UUIDCREATOR_SECURERANDOM` as in examples below.
System property:
# Use the the algorithm SHA1PRNG for SecureRandom
-Duuidcreator.securerandom="SHA1PRNG"
# Use the the algorithm DRBG for SecureRandom (JDK9+)
-Duuidcreator.securerandom="DRBG"
Environment variable:
# Use the the algorithm SHA1PRNG for SecureRandom
export UUIDCREATOR_SECURERANDOM="SHA1PRNG"
# Use the the algorithm DRBG for SecureRandom (JDK9+)
export UUIDCREATOR_SECURERANDOM="DRBG"
SecureRandom.Copyright © 2024. All rights reserved.