Contents | Package | Class | Tree | Deprecated | Index | Help | Java 1.2 Beta 3 | ||
PREV | NEXT | SHOW LISTS | HIDE LISTS |
java.lang.Object | +----java.util.Random | +----java.security.SecureRandom
This class provides a crytpographically strong pseudo-random number generator based on the SHA-1 hash algorithm.
The calls inherited from Random will be implemented in terms of the strengthened functionality.
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. It relies on counting the number of times that the calling thread can yield while waiting for another thread to sleep for a specified interval.
Also note that when a random object is deserialized, nextBytes 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 setSeed.
Constructor Summary | |
SecureRandom()
|
|
SecureRandom(byte[] seed)
|
Method Summary | |
static byte[] | getSeed(int numBytes)
|
int | next(int numBits)
|
void | nextBytes(byte[] bytes)
|
void | setSeed(byte[] seed)
|
void | setSeed(long seed)
long seed .
|
Methods inherited from class java.util.Random |
next, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextLong, setSeed |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public SecureRandom()
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.
The seeding procedure implemented by this constructor ensures that the sequence of pseudo-random bytes produced by each SecureRandom instance yields no useful information about the byte-sequence produced by any other instance. If however, the user wishes to produce multiple instances with truly unrelated seeds, the following code yields the desired result (at substantial CPU cost per instance!):
SecureRandom rnd = new SecureRandom(SecureRandom.getSeed(20));
public SecureRandom(byte[] seed)
seed
- the seed.
Method Detail |
public void setSeed(byte[] seed)
seed
- the seed.
public void setSeed(long seed)
long seed
. The given seed supplements,
rather than replaces, the existing seed. Thus, repeated calls
are guaranteed never to reduce randomness.
This method is defined for compatibility with
java.util.Random
.
seed
- the seed.
public void nextBytes(byte[] bytes)
bytes
- the array to be filled in with random bytes.
protected final int next(int numBits)
java.util.Random
method, and serves
to provide a source of random bits to all of the methods inherited
from that class (for example, nextInt
,
nextLong
, and nextFloat
).
numBits
- number of pseudo-random bits to be generated, where
0 <= numBits
<= 32.
public static byte[] getSeed(int numBytes)
numBytes
- the number of seed bytes to generate.
Contents | Package | Class | Tree | Deprecated | Index | Help | Java 1.2 Beta 3 | ||
PREV | NEXT | SHOW LISTS | HIDE LISTS |