public class

SecureRandom

extends Random
java.lang.Object
   ↳ java.util.Random
     ↳ java.security.SecureRandom

Class Overview

SecureRandom is an engine class which is capable of generating cryptographically secure pseudo-random numbers.

Summary

Public Constructors
SecureRandom()
Constructs a new instance of SecureRandom.
SecureRandom(byte[] seed)
Constructs a new instance of SecureRandom.
Protected Constructors
SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider)
Constructs a new instance of SecureRandom using the given implementation from the specified provider.
Public Methods
byte[] generateSeed(int numBytes)
Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by this SecureRandom.
String getAlgorithm()
Returns the name of the algorithm of this SecureRandom.
static SecureRandom getInstance(String algorithm, String provider)
Returns a new instance of SecureRandom that utilizes the specified algorithm from the specified provider.
static SecureRandom getInstance(String algorithm, Provider provider)
Returns a new instance of SecureRandom that utilizes the specified algorithm from the specified provider.
static SecureRandom getInstance(String algorithm)
Returns a new instance of SecureRandom that utilizes the specified algorithm.
final Provider getProvider()
Returns the provider associated with this SecureRandom.
static byte[] getSeed(int numBytes)
Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by this SecureRandom.
synchronized void nextBytes(byte[] bytes)
Generates and stores random bytes in the given byte[] for each array element.
synchronized void setSeed(byte[] seed)
Reseeds this SecureRandom instance with the specified seed.
void setSeed(long seed)
Reseeds this this SecureRandom instance with the eight bytes described by the representation of the given long seed.
Protected Methods
final int next(int numBits)
Generates and returns an int containing the specified number of random bits (right justified, with leading zeros).
[Expand]
Inherited Methods
From class java.util.Random
From class java.lang.Object

Public Constructors

public SecureRandom ()

Constructs a new instance of SecureRandom. An implementation for the highest-priority provider is returned. The constructed instance will not have been seeded.

public SecureRandom (byte[] seed)

Constructs a new instance of SecureRandom. An implementation for the highest-priority provider is returned. The constructed instance will be seeded with the parameter.

Parameters
seed the seed for this generator.

Protected Constructors

protected SecureRandom (SecureRandomSpi secureRandomSpi, Provider provider)

Constructs a new instance of SecureRandom using the given implementation from the specified provider.

Parameters
secureRandomSpi the implementation.
provider the security provider.

Public Methods

public byte[] generateSeed (int numBytes)

Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by this SecureRandom.

Parameters
numBytes the number of seed bytes.
Returns
  • the seed bytes.

public String getAlgorithm ()

Returns the name of the algorithm of this SecureRandom.

Returns
  • the name of the algorithm of this SecureRandom.

public static SecureRandom getInstance (String algorithm, String provider)

Returns a new instance of SecureRandom that 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 SecureRandom that 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 if algorithm is null.

public static SecureRandom getInstance (String algorithm, Provider provider)

Returns a new instance of SecureRandom that utilizes the specified algorithm from the specified provider.

Parameters
algorithm the name of the algorithm to use.
provider the security provider.
Returns
  • a new instance of SecureRandom that utilizes the specified algorithm from the specified provider.
Throws
NoSuchAlgorithmException if the specified algorithm is not available.
NullPointerException if algorithm is null.

public static SecureRandom getInstance (String algorithm)

Returns a new instance of SecureRandom that utilizes the specified algorithm.

Parameters
algorithm the name of the algorithm to use.
Returns
  • a new instance of SecureRandom that utilizes the specified algorithm.
Throws
NoSuchAlgorithmException if the specified algorithm is not available.
NullPointerException if algorithm is null.

public final Provider getProvider ()

Returns the provider associated with this SecureRandom.

Returns
  • the provider associated with this SecureRandom.

public static byte[] getSeed (int numBytes)

Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by this SecureRandom.

Parameters
numBytes the number of seed bytes.
Returns
  • the seed bytes

public synchronized void nextBytes (byte[] bytes)

Generates and stores random bytes in the given byte[] for each array element.

Parameters
bytes the byte[] to be filled with random bytes.

public synchronized void setSeed (byte[] seed)

Reseeds this SecureRandom instance with the specified seed. The seed of this SecureRandom instance is supplemented, not replaced.

Parameters
seed the new seed.

public void setSeed (long seed)

Reseeds this this SecureRandom instance with the eight bytes described by the representation of the given long seed. The seed of this SecureRandom instance is supplemented, not replaced.

Parameters
seed the new seed.

Protected Methods

protected final int next (int numBits)

Generates and returns an int containing the specified number of random bits (right justified, with leading zeros).

Parameters
numBits number of bits to be generated. An input value should be in the range [0, 32].
Returns
  • an int containing the specified number of random bits.