- java.lang.Object
-
- com.github.f4b6a3.uuid.util.internal.RandomUtil
-
public final class RandomUtil extends Object
Utility class that provides random generator services.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().
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static SecureRandomnewSecureRandom()Returns a new instance ofSecureRandom.static byte[]nextBytes(int length)Returns an array of random bytes.static longnextLong()Returns a random 64-bit number.
-
-
-
Method Detail
-
nextLong
public static long nextLong()
Returns a random 64-bit number.- Returns:
- a number
-
nextBytes
public static byte[] nextBytes(int length)
Returns an array of random bytes.- Parameters:
length- the array length- Returns:
- a byte array
-
newSecureRandom
public static SecureRandom newSecureRandom()
Returns a new instance ofSecureRandom.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"- Returns:
- a new
SecureRandom.
-
-