7167656: Multiple Seeders are being created

Reviewed-by: smarks, mduigou, ahgross
This commit is contained in:
Bradford Wetmore 2012-05-21 15:42:15 -07:00
parent 1e5b3e3ded
commit 3b10a3f9b8

View File

@ -56,12 +56,6 @@ implements java.io.Serializable {
private static final long serialVersionUID = 3581829991155417889L; private static final long serialVersionUID = 3581829991155417889L;
/**
* This static object will be seeded by SeedGenerator, and used
* to seed future instances of SecureRandom
*/
private static SecureRandom seeder;
private static final int DIGEST_SIZE = 20; private static final int DIGEST_SIZE = 20;
private transient MessageDigest digest; private transient MessageDigest digest;
private byte[] state; private byte[] state;
@ -172,6 +166,28 @@ implements java.io.Serializable {
state[0]++; state[0]++;
} }
/**
* This static object will be seeded by SeedGenerator, and used
* to seed future instances of SHA1PRNG SecureRandoms.
*
* Bloch, Effective Java Second Edition: Item 71
*/
private static class SeederHolder {
private static final SecureRandom seeder;
static {
/*
* Call to SeedGenerator.generateSeed() to add additional
* seed material (likely from the Native implementation).
*/
seeder = new SecureRandom(SeedGenerator.getSystemEntropy());
byte [] b = new byte[DIGEST_SIZE];
SeedGenerator.generateSeed(b);
seeder.engineSetSeed(b);
}
}
/** /**
* Generates a user-specified number of random bytes. * Generates a user-specified number of random bytes.
* *
@ -183,13 +199,8 @@ implements java.io.Serializable {
byte[] output = remainder; byte[] output = remainder;
if (state == null) { if (state == null) {
if (seeder == null) {
seeder = new SecureRandom(SeedGenerator.getSystemEntropy());
seeder.engineSetSeed(engineGenerateSeed(DIGEST_SIZE));
}
byte[] seed = new byte[DIGEST_SIZE]; byte[] seed = new byte[DIGEST_SIZE];
seeder.engineNextBytes(seed); SeederHolder.seeder.engineNextBytes(seed);
state = digest.digest(seed); state = digest.digest(seed);
} }