A cryptographically strong random number minimally complies with the
statistical random number generator tests specified in
FIPS 140-2, Security Requirements for Cryptographic Modules,
section 4.9.1.
Additionally, SecureRandom
must produce non-deterministic output.
Therefore, any seed material passed to a SecureRandom
object must be
unpredictable, and all SecureRandom
output sequences must be
cryptographically strong, as described in
RFC 4086: Randomness Requirements for Security.
Many SecureRandom
implementations are in the form of a
pseudo-random number generator (PRNG, also known as deterministic random
bits generator or DRBG), which means they use a deterministic algorithm
to produce a pseudo-random sequence from a random seed.
Other implementations may produce true random numbers,
and yet others may use a combination of both techniques.
A caller obtains a SecureRandom
instance via the
no-argument constructor or one of the getInstance
methods.
For example:
SecureRandom r1 = new SecureRandom(); SecureRandom r2 = SecureRandom.getInstance("NativePRNG"); SecureRandom r3 = SecureRandom.getInstance("DRBG", DrbgParameters.instantiation(128, RESEED_ONLY, null));
The third statement above returns a SecureRandom
object of the
specific algorithm supporting the specific instantiate parameters. The
implementation's effective instantiated parameters must match this minimum
request but is not necessarily the same. For example, even if the request
does not require a certain feature, the actual instantiation can provide
the feature. An implementation may lazily instantiate a SecureRandom
until it's actually used, but the effective instantiate parameters must be
determined right after it's created and getParameters()
should
always return the same result unchanged.
Typical callers of SecureRandom
invoke the following methods
to retrieve random bytes:
SecureRandom random = new SecureRandom(); byte[] bytes = new byte[20]; random.nextBytes(bytes);
Callers may also invoke the generateSeed
method
to generate a given number of seed bytes (to seed other random number
generators, for example):
byte[] seed = random.generateSeed(20);
A newly created PRNG SecureRandom
object is not seeded (except
if it is created by SecureRandom(byte[])
). The first call to
nextBytes
will force it to seed itself from an implementation-
specific entropy source. This self-seeding will not occur if setSeed
was previously called.
A SecureRandom
can be reseeded at any time by calling the
reseed
or setSeed
method. The reseed
method
reads entropy input from its entropy source to reseed itself.
The setSeed
method requires the caller to provide the seed.
Please note that reseed
may not be supported by all
SecureRandom
implementations.
Some SecureRandom
implementations may accept a
SecureRandomParameters
parameter in its
nextBytes(byte[], SecureRandomParameters)
and
reseed(SecureRandomParameters)
methods to further
control the behavior of the methods.
Note
Depending on the implementation, the generateSeed
,
reseed
and nextBytes
methods may block as entropy is being
gathered, for example, if the entropy source is /dev/random on various
Unix-like operating systems.
SecureRandom
objects are safe for use by multiple concurrent threads.
Implementation Specification
A SecureRandom
service provider can advertise that it is thread-safe
by setting the service
provider attribute "ThreadSafe" to "true" when registering the provider.
Otherwise, this class will instead synchronize access to the following
methods of the SecureRandomSpi
implementation:
java.security.SecureRandomSpi
, java.util.Random
Modifier and Type | Class and Description |
---|---|
private static class |
Modifier and Type | Field and Description |
---|---|
private String | algorithm
The algorithm name or |
private long | |
private MessageDigest | |
private static final Debug | |
private Provider | provider
The provider. |
private byte[] | |
private int | |
private SecureRandomSpi | secureRandomSpi
The provider implementation. |
private static volatile SecureRandom | |
pack-priv static final long | |
private static final boolean | |
private byte[] | |
private final boolean | threadSafe
Thread safety. |
Access | Constructor and Description |
---|---|
public | SecureRandom()
Constructs a secure random number generator (RNG) implementing the default random number algorithm. |
public | SecureRandom(byte[]
the seed. seed)Constructs a secure random number generator (RNG) implementing the default random number algorithm. |
protected | SecureRandom(SecureRandomSpi
the secureRandomSpi, Provider SecureRandom implementation.the provider. provider)Creates a |
private |
Modifier and Type | Method and Description |
---|---|
public byte[] | Returns: the seed bytes.the number of seed bytes to generate. numBytes)Returns the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself. |
public String | Returns: the name of the algorithm orunknown
if the algorithm name cannot be determined.Returns the name of the algorithm implemented by this
|
private void | |
public static SecureRandom | Returns: the newSecureRandom objectthe name of the RNG algorithm.
See the algorithm)SecureRandom section in the
Java Security Standard Algorithm Names Specification
for information about standard RNG algorithm names.Returns a |
public static SecureRandom | Returns: the newSecureRandom objectthe name of the RNG algorithm.
See the algorithm, String SecureRandom section in the
Java Security Standard Algorithm Names Specification
for information about standard RNG algorithm names.the name of the provider. provider)Returns a |
public static SecureRandom | Returns: the newSecureRandom objectthe name of the RNG algorithm.
See the algorithm, Provider SecureRandom section in the
Java Security Standard Algorithm Names Specification
for information about standard RNG algorithm names.the provider. provider)Returns a |
public static SecureRandom | Returns: the newSecureRandom objectthe name of the RNG algorithm.
See the algorithm, SecureRandomParameters SecureRandom section in the
Java Security Standard Algorithm Names Specification
for information about standard RNG algorithm names.the params)SecureRandomParameters
the newly created SecureRandom object must support.Returns a |
public static SecureRandom | Returns: the newSecureRandom objectthe name of the RNG algorithm.
See the algorithm, SecureRandomParameters SecureRandom section in the
Java Security Standard Algorithm Names Specification
for information about standard RNG algorithm names.the params, String SecureRandomParameters
the newly created SecureRandom object must support.the name of the provider. provider)Returns a |
public static SecureRandom | Returns: the newSecureRandom objectthe name of the RNG algorithm.
See the algorithm, SecureRandomParameters SecureRandom section in the
Java Security Standard Algorithm Names Specification
for information about standard RNG algorithm names.the params, Provider SecureRandomParameters
the newly created SecureRandom object must support.the provider. provider)Returns a |
public static SecureRandom | Returns: a strongSecureRandom implementation as indicated
by the securerandom.strongAlgorithms Security propertyReturns a |
public SecureRandomParameters | |
public final Provider | Returns: the provider of thisSecureRandom object.Returns the provider of this |
private String | |
public static byte[] | Returns: the seed bytes.the number of seed bytes to generate. numBytes)Returns the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself. |
private boolean | |
private static byte[] | longToByteArray(long l)
Helper function to convert a long into a byte array (least significant byte first). |
protected final int | Returns: anint containing the user-specified number
of pseudo-random bits (right justified, with leading zeros).number of pseudo-random bits to be generated, where
numBits)0 <= numBits <= 32 .Overrides java. Generates an integer containing the user-specified number of pseudo-random bits (right justified, with leading zeros). |
public void | nextBytes(byte[]
the array to be filled in with random bytes. bytes)Overrides java. Overrides default java. Generates a user-specified number of random bytes. |
public void | nextBytes(byte[]
the array to be filled in with random bytes bytes, SecureRandomParameters additional parameters params)Generates a user-specified number of random bytes with additional parameters. |
public void | |
public void | reseed(SecureRandomParameters
extra parameters params)Reseeds this |
public void | |
public void | setSeed(long
the seed. seed)Overrides java. Reseeds this random object, using the eight bytes contained
in the given |
public String | Returns: the string representationOverrides java. Returns a Human-readable string representation of this
|
algorithm | back to summary |
---|---|
private String algorithm The algorithm name or
|
counter | back to summary |
---|---|
private long counter |
digest | back to summary |
---|---|
private MessageDigest digest
|
pdebug | back to summary |
---|---|
private static final Debug pdebug |
provider | back to summary |
---|---|
private Provider provider The provider.
|
randomBytes | back to summary |
---|---|
private byte[] randomBytes |
randomBytesUsed | back to summary |
---|---|
private int randomBytesUsed |
secureRandomSpi | back to summary |
---|---|
private SecureRandomSpi secureRandomSpi The provider implementation.
|
seedGenerator | back to summary |
---|---|
private static volatile SecureRandom seedGenerator |
serialVersionUID | back to summary |
---|---|
pack-priv static final long serialVersionUID Hides java. |
skipDebug | back to summary |
---|---|
private static final boolean skipDebug |
state | back to summary |
---|---|
private byte[] state |
threadSafe | back to summary |
---|---|
private final boolean threadSafe Thread safety.
|
SecureRandom | back to summary |
---|---|
public SecureRandom() Constructs a secure random number generator (RNG) implementing the default random number algorithm. This constructor traverses the list of registered security Providers,
starting with the most preferred Provider.
A new Note that the list of registered providers may be retrieved via
the See the |
SecureRandom | back to summary |
---|---|
public SecureRandom(byte[] seed) Constructs a secure random number generator (RNG) implementing the
default random number algorithm.
The This constructor traverses the list of registered security Providers,
starting with the most preferred Provider.
A new Note that the list of registered providers may be retrieved via
the See the
|
SecureRandom | back to summary |
---|---|
protected SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider) Creates a
|
SecureRandom | back to summary |
---|---|
private SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider, String algorithm) |
generateSeed | back to summary |
---|---|
public byte[] generateSeed(int numBytes) Returns the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself. This call may be used to seed other random number generators.
|
getAlgorithm | back to summary |
---|---|
public String getAlgorithm() Returns the name of the algorithm implemented by this
|
getDefaultPRNG | back to summary |
---|---|
private void getDefaultPRNG(boolean setSeed, byte[] seed) |
getInstance | back to summary |
---|---|
public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException Returns a This method traverses the list of registered security Providers,
starting with the most preferred Provider.
A new Note that the list of registered providers may be retrieved via
the Implementation Note The JDK Reference Implementation additionally uses the
|
getInstance | back to summary |
---|---|
public static SecureRandom getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException Returns a A new Note that the list of registered providers may be retrieved via
the
|
getInstance | back to summary |
---|---|
public static SecureRandom getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException Returns a A new
|
getInstance | back to summary |
---|---|
public static SecureRandom getInstance(String algorithm, SecureRandomParameters params) throws NoSuchAlgorithmException Returns a This method traverses the list of registered security providers,
starting with the most preferred provider.
A new Note that the list of registered providers may be retrieved via
the Implementation Note The JDK Reference Implementation additionally uses the
|
getInstance | back to summary |
---|---|
public static SecureRandom getInstance(String algorithm, SecureRandomParameters params, String provider) throws NoSuchAlgorithmException, NoSuchProviderException Returns a A new Note that the list of registered providers may be retrieved via
the
|
getInstance | back to summary |
---|---|
public static SecureRandom getInstance(String algorithm, SecureRandomParameters params, Provider provider) throws NoSuchAlgorithmException Returns a A new
|
getInstanceStrong | back to summary |
---|---|
public static SecureRandom getInstanceStrong() throws NoSuchAlgorithmException Returns a
Some situations require strong random values, such as when
creating high-value/long-lived secrets like RSA public/private
keys. To help guide applications in selecting a suitable strong
Every implementation of the Java platform is required to
support at least one strong
|
getParameters | back to summary |
---|---|
public SecureRandomParameters getParameters() Returns the effective
The returned value can be different from the
A caller can use the returned value to find out what features this
|
getProvider | back to summary |
---|---|
public final Provider getProvider() Returns the provider of this
|
getProviderName | back to summary |
---|---|
private String getProviderName() |
getSeed | back to summary |
---|---|
public static byte[] getSeed(int numBytes) Returns the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself. This call may be used to seed other random number generators. This method is only included for backwards compatibility.
The caller is encouraged to use one of the alternative
|
getThreadSafe | back to summary |
---|---|
private boolean getThreadSafe() |
longToByteArray | back to summary |
---|---|
private static byte[] longToByteArray(long l) Helper function to convert a long into a byte array (least significant byte first). |
next | back to summary |
---|---|
protected final int next(int numBits) Overrides java. Generates an integer containing the user-specified number of
pseudo-random bits (right justified, with leading zeros). This
method overrides a
|
nextBytes | back to summary |
---|---|
public void nextBytes(byte[] bytes) Overrides java. Overrides default java. Generates a user-specified number of random bytes.
|
nextBytes | back to summary |
---|---|
public void nextBytes(byte[] bytes, SecureRandomParameters params) Generates a user-specified number of random bytes with additional parameters.
|
reseed | back to summary |
---|---|
public void reseed() Reseeds this
|
reseed | back to summary |
---|---|
public void reseed(SecureRandomParameters params) Reseeds this
Note that entropy is obtained from an entropy source. While
some data in
|
setSeed | back to summary |
---|---|
public void setSeed(byte[] seed) Reseeds this random object with the given seed. The seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.
A PRNG
|
setSeed | back to summary |
---|---|
public void setSeed(long seed) Overrides java. Reseeds this random object, using the eight bytes contained
in the given
A PRNG This method is defined for compatibility with
|
toString | back to summary |
---|---|
public String toString() Overrides java. Returns a Human-readable string representation of this
|
Access | Constructor and Description |
---|---|
private |
pattern | back to summary |
---|---|
private static final Pattern pattern |
StrongPatternHolder | back to summary |
---|---|
private StrongPatternHolder() |