public final class

SecureRandom

extends SecureRandomSpi
implements Serializable
java.lang.Object
   ↳ java.security.SecureRandomSpi
     ↳ sun.security.provider.SecureRandom

Class Overview

This class provides a crytpographically 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.

Summary

Public Constructors
SecureRandom()
This empty constructor automatically seeds the generator.
Public Methods
byte[] engineGenerateSeed(int numBytes)
Returns the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself.
synchronized void engineNextBytes(byte[] result)
Generates a user-specified number of random bytes.
synchronized void engineSetSeed(byte[] seed)
Reseeds this random object.
[Expand]
Inherited Methods
From class java.security.SecureRandomSpi
From class java.lang.Object

Public Constructors

public SecureRandom ()

This empty constructor automatically seeds the generator. We attempt to provide sufficient seed bytes to completely randomize the internal state of the generator (20 bytes). Note, however, that our seed generation algorithm has not been thoroughly studied or widely deployed.

The first time this constructor is called in a given Virtual Machine, it may take several seconds of CPU time to seed the generator, depending on the underlying hardware. Successive calls run quickly because they rely on the same (internal) pseudo-random number generator for their seed bits.

Public Methods

public byte[] engineGenerateSeed (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. 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 the number of seed bytes to generate.
Returns
  • the seed bytes.

public synchronized void engineNextBytes (byte[] result)

Generates a user-specified number of random bytes.

Parameters
result the array to be filled in with random bytes.

public synchronized void engineSetSeed (byte[] seed)

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 the seed.