java.security
Class SecureRandom

java.lang.Object
  extended by java.util.Random
      extended by java.security.SecureRandom
All Implemented Interfaces:
Serializable

public class SecureRandom
extends Random

An interface to a cryptographically secure pseudo-random number generator (PRNG). Random (or at least unguessable) numbers are used in all areas of security and cryptography, from the generation of keys and initialization vectors to the generation of random padding bytes.

See Also:
Serialized Form

Constructor Summary
  SecureRandom()
          Default constructor for SecureRandom.
  SecureRandom(byte[] seed)
          A constructor for SecureRandom.
protected SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider)
          A constructor for SecureRandom.
 
Method Summary
 byte[] generateSeed(int numBytes)
          Returns the specified number of seed bytes.
 String getAlgorithm()
          Returns the algorithm name used or "unknown" when the algorithm used couldn't be determined (as when constructed by the protected 2 argument constructor).
static SecureRandom getInstance(String algorithm)
          Returns an instance of a SecureRandom from the first provider that implements it.
static SecureRandom getInstance(String algorithm, Provider provider)
          Returns an instance of a SecureRandom for the specified algorithm from the given provider.
static SecureRandom getInstance(String algorithm, String provider)
          Returns an instance of a SecureRandom for the specified algorithm from the named provider.
 Provider getProvider()
          Returns the provider being used by the current SecureRandom class.
static byte[] getSeed(int numBytes)
          Returns the given number of seed bytes.
protected  int next(int numBits)
          Generates an integer containing the user specified number of random bits.
 void nextBytes(byte[] bytes)
          Generates a user specified number of bytes.
 void setSeed(byte[] seed)
          Seeds the SecureRandom.
 void setSeed(long seed)
          Seeds the SecureRandom.
 
Methods inherited from class java.util.Random
nextBoolean, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SecureRandom

public SecureRandom()
Default constructor for SecureRandom. It constructs a new SecureRandom by instantating the first SecureRandom algorithm in the default security provier. It is not seeded and should be seeded using setSeed or else on the first call to getnextBytes it will force a seed. It is maintained for backwards compatibility and programs should use getInstance(java.lang.String).


SecureRandom

public SecureRandom(byte[] seed)
A constructor for SecureRandom. It constructs a new SecureRandom by instantating the first SecureRandom algorithm in the default security provier. It is seeded with the passed function and is useful if the user has access to hardware random device (like a radiation detector). It is maintained for backwards compatibility and programs should use getInstance.

Parameters:
seed - Seed bytes for class

SecureRandom

protected SecureRandom(SecureRandomSpi secureRandomSpi,
                       Provider provider)
A constructor for SecureRandom. It constructs a new SecureRandom using the specified SecureRandomSpi from the specified security provier.

Parameters:
secureRandomSpi - A SecureRandomSpi class
provider - A Provider class
Method Detail

getInstance

public static SecureRandom getInstance(String algorithm)
                                throws NoSuchAlgorithmException
Returns an instance of a SecureRandom from the first provider that implements it.

Parameters:
algorithm - The algorithm name.
Returns:
A new SecureRandom implementing the given algorithm.
Throws:
NoSuchAlgorithmException - If no installed provider implements the given algorithm.
IllegalArgumentException - if algorithm is null or is an empty string.

getInstance

public static SecureRandom getInstance(String algorithm,
                                       String provider)
                                throws NoSuchAlgorithmException,
                                       NoSuchProviderException
Returns an instance of a SecureRandom for the specified algorithm from the named provider.

Parameters:
algorithm - The algorithm name.
provider - The provider name.
Returns:
A new SecureRandom implementing the chosen algorithm.
Throws:
NoSuchAlgorithmException - If the named provider does not implement the algorithm, or if the implementation cannot be instantiated.
NoSuchProviderException - If no provider named provider is currently installed.
IllegalArgumentException - if either algorithm or provider is null or empty.

getInstance

public static SecureRandom getInstance(String algorithm,
                                       Provider provider)
                                throws NoSuchAlgorithmException
Returns an instance of a SecureRandom for the specified algorithm from the given provider.

Parameters:
algorithm - The SecureRandom algorithm to create.
provider - The provider to use.
Throws:
NoSuchAlgorithmException - If the algorithm cannot be found, or if the class cannot be instantiated.
IllegalArgumentException - if either algorithm or provider is null, or if algorithm is an empty string.

getProvider

public final Provider getProvider()
Returns the provider being used by the current SecureRandom class.

Returns:
The provider from which this SecureRandom was attained

getAlgorithm

public String getAlgorithm()
Returns the algorithm name used or "unknown" when the algorithm used couldn't be determined (as when constructed by the protected 2 argument constructor).

Since:
1.5

setSeed

public void setSeed(byte[] seed)
Seeds the SecureRandom. The class is re-seeded for each call and each seed builds on the previous seed so as not to weaken security.

Parameters:
seed - seed bytes to seed with

setSeed

public void setSeed(long seed)
Seeds the SecureRandom. The class is re-seeded for each call and each seed builds on the previous seed so as not to weaken security.

Overrides:
setSeed in class Random
Parameters:
seed - 8 seed bytes to seed with

nextBytes

public void nextBytes(byte[] bytes)
Generates a user specified number of bytes. This function is the basis for all the random functions.

Overrides:
nextBytes in class Random
Parameters:
bytes - array to store generated bytes in

next

protected final int next(int numBits)
Generates an integer containing the user specified number of random bits. It is right justified and padded with zeros.

Overrides:
next in class Random
Parameters:
numBits - number of random bits to get, 0 <= numBits <= 32;
Returns:
the random bits

getSeed

public static byte[] getSeed(int numBytes)
Returns the given number of seed bytes. This method is maintained only for backwards capability.

Parameters:
numBytes - number of seed bytes to get
Returns:
an array containing the seed bytes

generateSeed

public byte[] generateSeed(int numBytes)
Returns the specified number of seed bytes.

Parameters:
numBytes - number of seed bytes to get
Returns:
an array containing the seed bytes