APIdia Launch Partner
Top Description Inners Fields Constructors Methods
sun.security.provider

public final Class SecureRandom

extends SecureRandomSpi
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.IOException, .InvalidObjectException, java.security.MessageDigest, .SecureRandomSpi, .NoSuchAlgorithmException, .NoSuchProviderException, java.util.Arrays

This class provides a cryptographically strong pseudo-random number generator based on the SHA-1 hash algorithm.

Note that if a seed is not provided, we attempt to provide sufficient seed bytes to completely randomize the internal state of the generator (20 bytes). However, our seed generation algorithm has not been thoroughly studied or widely deployed.

Also note that when a random object is deserialized, engineNextBytes invoked on the restored random object will yield the exact same (random) bytes as the original object. If this behaviour is not desired, the restored random object should be seeded, using engineSetSeed.

Authors
Benjamin Renaud, Josh Bloch, Gadi Guy

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SecureRandom.SeederHolder

This static object will be seeded by SeedGenerator, and used to seed future instances of SHA1PRNG SecureRandoms.

Field Summary

Modifier and TypeField and Description
private transient MessageDigest
private static final int
private byte[]
private int
private static final long
private byte[]

Constructor Summary

AccessConstructor and Description
public
SecureRandom()

An empty constructor that creates an unseeded SecureRandom object.

private
SecureRandom(byte[]
the seed.
seed
)

This constructor is used to instantiate the private seeder object with a given seed from the SeedGenerator.

Method Summary

Modifier and TypeMethod and Description
public byte[]

Returns:

the seed bytes.
engineGenerateSeed
(int
the number of seed bytes to generate.
numBytes
)

Implements abstract java.security.SecureRandomSpi.engineGenerateSeed.

Returns the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself.

public synchronized void
engineNextBytes(byte[]
the array to be filled in with random bytes.
result
)

Implements abstract java.security.SecureRandomSpi.engineNextBytes.

Generates a user-specified number of random bytes.

public synchronized void
engineSetSeed(byte[]
the seed.
seed
)

Implements abstract java.security.SecureRandomSpi.engineSetSeed.

Reseeds this random object.

private void
init(byte[] seed)

This call, used by the constructors, instantiates the SHA digest and sets the seed, if given.

private void
private static void
updateState(byte[] state, byte[] output)

Inherited from java.security.SecureRandomSpi:
engineGetParametersengineNextBytesengineReseedtoString

Field Detail

digestback to summary
private transient MessageDigest digest
DIGEST_SIZEback to summary
private static final int DIGEST_SIZE
remainderback to summary
private byte[] remainder
remCountback to summary
private int remCount
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.security.SecureRandomSpi.serialVersionUID.

Annotations
@Serial
stateback to summary
private byte[] state

Constructor Detail

SecureRandomback to summary
public SecureRandom()

An empty constructor that creates an unseeded SecureRandom object.

Unless the user calls setSeed(), the first call to engineGetBytes() will have the SeedGenerator provide sufficient seed bytes to completely randomize the internal state of the generator (20 bytes). Note that the old threaded seed generation algorithm is provided only as a fallback, and has not been thoroughly studied or widely deployed.

The SeedGenerator relies on a VM-wide entropy pool to generate seed bytes for these objects. The first time the SeedGenerator is called, it may take several seconds of CPU time to initialize, depending on the underlying hardware. Successive calls run quickly because they rely on the same (internal) pseudo-random number generator for their seed bits.

SecureRandomback to summary
private SecureRandom(byte[] seed)

This constructor is used to instantiate the private seeder object with a given seed from the SeedGenerator.

Parameters
seed:byte[]

the seed.

Method Detail

engineGenerateSeedback to summary
public byte[] engineGenerateSeed(int numBytes)

Implements abstract java.security.SecureRandomSpi.engineGenerateSeed.

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. While we attempt to return a "truly random" sequence of bytes, we do not know exactly how random the bytes returned by this call are. (See the empty constructor SecureRandom for a brief description of the underlying algorithm.) The prudent user will err on the side of caution and get extra seed bytes, although it should be noted that seed generation is somewhat costly.

Parameters
numBytes:int

the number of seed bytes to generate.

Returns:byte[]

the seed bytes.

Annotations
@Override
engineNextBytesback to summary
public synchronized void engineNextBytes(byte[] result)

Implements abstract java.security.SecureRandomSpi.engineNextBytes.

Generates a user-specified number of random bytes.

Parameters
result:byte[]

the array to be filled in with random bytes.

Annotations
@Override
engineSetSeedback to summary
public synchronized void engineSetSeed(byte[] seed)

Implements abstract java.security.SecureRandomSpi.engineSetSeed.

Reseeds this random object. The given seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.

Parameters
seed:byte[]

the seed.

Annotations
@Override
initback to summary
private void init(byte[] seed)

This call, used by the constructors, instantiates the SHA digest and sets the seed, if given.

readObjectback to summary
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
Annotations
@Serial
updateStateback to summary
private static void updateState(byte[] state, byte[] output)
sun.security.provider back to summary

private Class SecureRandom.SeederHolder

extends Object
Class Inheritance

This static object will be seeded by SeedGenerator, and used to seed future instances of SHA1PRNG SecureRandoms.

Bloch, Effective Java Second Edition: Item 71

Field Summary

Modifier and TypeField and Description
private static final SecureRandom

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

seederback to summary
private static final SecureRandom seeder

Constructor Detail

SeederHolderback to summary
private SeederHolder()