Class SecureRandom
- java.lang.Object
-
- java.util.Random
-
- java.security.SecureRandom
-
- All Implemented Interfaces:
Serializable
public class SecureRandom extends Random
This class generates cryptographically secure pseudo-random numbers. It is best to invokeSecureRandomusing the default constructor. This will provide an instance of the most cryptographically strong provider available:SecureRandom sr = new SecureRandom(); byte[] output = new byte[16]; sr.nextBytes(output);
The default algorithm is defined by the first
SecureRandomSpiprovider found in the installed security providers. UseSecurityto install customSecureRandomSpiproviders.Note that the output of a
SecureRandominstance should never be relied upon to be deterministic. For deterministic output from a given input, seeMessageDigestwhich provides one-way hash functions. For deriving keys from passwords, seeSecretKeyFactory.Seeding
A seed is an array of bytes used to bootstrap random number generation. To produce cryptographically secure random numbers, both the seed and the algorithm must be secure.SecureRandommay be insecureBy default, instances of this class will generate an initial seed using an internal entropy source, such as
/dev/urandom. This seed is unpredictable and appropriate for secure use.Using the
seeded constructoror callingsetSeed(byte[])may completely replace the cryptographically strong default seed causing the instance to return a predictable sequence of numbers unfit for secure use. Due to variations between implementations it is not recommended to usesetSeedat all.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description SecureRandom()Constructs a newSecureRandomthat uses the default algorithm.SecureRandom(byte[] seed)Constructs a new seededSecureRandomthat uses the default algorithm.protectedSecureRandom(SecureRandomSpi secureRandomSpi, Provider provider)Constructs a new instance ofSecureRandomusing the given implementation from the specified provider.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]generateSeed(int numBytes)Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by thisSecureRandom.StringgetAlgorithm()Returns the name of the algorithm of thisSecureRandom.static SecureRandomgetInstance(String algorithm)Returns a new instance ofSecureRandomthat utilizes the specified algorithm.static SecureRandomgetInstance(String algorithm, String provider)Returns a new instance ofSecureRandomthat utilizes the specified algorithm from the specified provider.static SecureRandomgetInstance(String algorithm, Provider provider)Returns a new instance ofSecureRandomthat utilizes the specified algorithm from the specified provider.ProvidergetProvider()Returns the provider associated with thisSecureRandom.static byte[]getSeed(int numBytes)Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by thisSecureRandom.protected intnext(int numBits)Generates and returns anintcontaining the specified number of random bits (right justified, with leading zeros).voidnextBytes(byte[] bytes)Generates and stores random bytes in the givenbyte[]for each array element.voidsetSeed(byte[] seed)Seeds thisSecureRandominstance with the specifiedseed.voidsetSeed(long seed)Seeds thisSecureRandominstance with the specified eight-byteseed.-
Methods inherited from class java.util.Random
nextBoolean, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong
-
-
-
-
Constructor Detail
-
SecureRandom
public SecureRandom()
Constructs a newSecureRandomthat uses the default algorithm.
-
SecureRandom
public SecureRandom(byte[] seed)
Constructs a new seededSecureRandomthat uses the default algorithm. SeedingSecureRandommay be insecure.
-
SecureRandom
protected SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider)
Constructs a new instance ofSecureRandomusing the given implementation from the specified provider.- Parameters:
secureRandomSpi- the implementation.provider- the security provider.
-
-
Method Detail
-
getInstance
public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException
Returns a new instance ofSecureRandomthat utilizes the specified algorithm.- Parameters:
algorithm- the name of the algorithm to use.- Returns:
- a new instance of
SecureRandomthat utilizes the specified algorithm. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not available.NullPointerException- ifalgorithmisnull.
-
getInstance
public static SecureRandom getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
Returns a new instance ofSecureRandomthat utilizes the specified algorithm from the specified provider.- Parameters:
algorithm- the name of the algorithm to use.provider- the name of the provider.- Returns:
- a new instance of
SecureRandomthat utilizes the specified algorithm from the specified provider. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not available.NoSuchProviderException- if the specified provider is not available.NullPointerException- ifalgorithmisnull.IllegalArgumentException- ifprovider == null || provider.isEmpty()
-
getInstance
public static SecureRandom getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
Returns a new instance ofSecureRandomthat utilizes the specified algorithm from the specified provider. Theprovidersupplied does not have to be registered.- Parameters:
algorithm- the name of the algorithm to use.provider- the security provider.- Returns:
- a new instance of
SecureRandomthat utilizes the specified algorithm from the specified provider. - Throws:
NoSuchAlgorithmException- if the specified algorithm is not available.NullPointerException- ifalgorithmisnull.IllegalArgumentException- ifprovider == null
-
getProvider
public final Provider getProvider()
Returns the provider associated with thisSecureRandom.- Returns:
- the provider associated with this
SecureRandom.
-
getAlgorithm
public String getAlgorithm()
Returns the name of the algorithm of thisSecureRandom.- Returns:
- the name of the algorithm of this
SecureRandom.
-
setSeed
public void setSeed(byte[] seed)
-
setSeed
public void setSeed(long seed)
Seeds thisSecureRandominstance with the specified eight-byteseed. SeedingSecureRandommay be insecure.
-
nextBytes
public void nextBytes(byte[] bytes)
Generates and stores random bytes in the givenbyte[]for each array element.
-
next
protected final int next(int numBits)
Generates and returns anintcontaining the specified number of random bits (right justified, with leading zeros).
-
getSeed
public static byte[] getSeed(int numBytes)
Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by thisSecureRandom.- Parameters:
numBytes- the number of seed bytes.- Returns:
- the seed bytes
-
generateSeed
public byte[] generateSeed(int numBytes)
Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by thisSecureRandom.- Parameters:
numBytes- the number of seed bytes.- Returns:
- the seed bytes.
-
-