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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | SecureRandom.
This static object will be seeded by SeedGenerator, and used to seed future instances of SHA1PRNG SecureRandoms. |
| Modifier and Type | Field and Description |
|---|---|
| private transient MessageDigest | |
| private static final int | |
| private byte[] | |
| private int | |
| private static final long | |
| private byte[] |
| Access | Constructor and Description |
|---|---|
| public | |
| private | SecureRandom(byte[]
the seed. seed)This constructor is used to instantiate the private seeder object with a given seed from the SeedGenerator. |
| Modifier and Type | Method and Description |
|---|---|
| public byte[] | Returns: the seed bytes.the number of seed bytes to generate. numBytes)Implements abstract java. 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. Generates a user-specified number of random bytes. |
| public synchronized void | engineSetSeed(byte[]
the seed. seed)Implements abstract java. 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 |
| digest | back to summary |
|---|---|
| private transient MessageDigest digest | |
| DIGEST_SIZE | back to summary |
|---|---|
| private static final int DIGEST_SIZE | |
| remainder | back to summary |
|---|---|
| private byte[] remainder | |
| remCount | back to summary |
|---|---|
| private int remCount | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides java. | |
| state | back to summary |
|---|---|
| private byte[] state | |
| SecureRandom | back 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. | |
| SecureRandom | back to summary |
|---|---|
| private SecureRandom(byte[] seed) This constructor is used to instantiate the private seeder object with a given seed from the SeedGenerator.
| |
| engineGenerateSeed | back to summary |
|---|---|
| public byte[] engineGenerateSeed(int numBytes) Implements abstract java. 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.
| |
| engineNextBytes | back to summary |
|---|---|
| public synchronized void engineNextBytes(byte[] result) Implements abstract java. Generates a user-specified number of random bytes.
| |
| engineSetSeed | back to summary |
|---|---|
| public synchronized void engineSetSeed(byte[] seed) Implements abstract java. Reseeds this random object. The given seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.
| |
| init | back to summary |
|---|---|
| private void init(byte[] seed) This call, used by the constructors, instantiates the SHA digest and sets the seed, if given. | |
| readObject | back to summary |
|---|---|
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
| |
| updateState | back to summary |
|---|---|
| private static void updateState(byte[] state, byte[] output) | |
Bloch, Effective Java Second Edition: Item 71
| Modifier and Type | Field and Description |
|---|---|
| private static final SecureRandom |
| Access | Constructor and Description |
|---|---|
| private |
| seeder | back to summary |
|---|---|
| private static final SecureRandom seeder | |
| SeederHolder | back to summary |
|---|---|
| private SeederHolder() | |