org.hd.d.efs
Class Whitener

java.lang.Object
  extended by java.util.Random
      extended by java.security.SecureRandom
          extended by org.hd.d.efs.Whitener
All Implemented Interfaces:
java.io.Serializable

public final class Whitener
extends java.security.SecureRandom

This class whitens one source of random numbers with another. This has an overall interface of SecureRandom, and takes two SecureRandom generators (which must not be the same nor null) and XORs their output to gain a new whitened stream.

The one-argument constructor automatically uses a private instance of the default implementation of SecureRandom() as the whitener.

An application of this might be where although some part of a system might want to use a shared EntropyPool, it is particularly paranoid about security and may wish to whiten the output with its own SecureRandom instance to ensure that even compromise of the shared generator is unlikely to compromise its own generator quickly.

All we need to override is nextBytes() and the seed() calls.

See Also:
Serialized Form

Constructor Summary
Whitener(java.security.SecureRandom main)
          Construct a combo generator with a main source and a private whitener.
Whitener(java.security.SecureRandom main, java.security.SecureRandom whitener)
          Construct a combo generator with main and whitener sources.
 
Method Summary
 byte[] generateSeed(int numBytes)
          Returns seed bytes from both generators XORed together.
 void nextBytes(byte[] result)
          Generates a user-specified number of random bytes.
 void setSeed(byte[] seed)
          Reseeds both underlying generators with this seed.
 void setSeed(long seed)
          Reseeds both underlying generators with this seed.
 
Methods inherited from class java.security.SecureRandom
getAlgorithm, getInstance, getInstance, getInstance, getProvider, getSeed, next
 
Methods inherited from class java.util.Random
nextBoolean, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Whitener

public Whitener(java.security.SecureRandom main,
                java.security.SecureRandom whitener)
         throws java.lang.IllegalArgumentException
Construct a combo generator with main and whitener sources. Neither source may be null, and the sources must de distinct. We can only test that the objects passed are not the same; it is the caller's responsibility to ensure that the generators passed in are sensible.

Throws:
java.lang.IllegalArgumentException

Whitener

public Whitener(java.security.SecureRandom main)
Construct a combo generator with a main source and a private whitener. The whitener is a private instance of SecureRandom(), which while probably not introducing any entropy should make the combination source resistant to attack even if the main source is compromised.

Method Detail

setSeed

public void setSeed(byte[] seed)
Reseeds both underlying generators with this seed. The given seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.

Overrides:
setSeed in class java.security.SecureRandom
Parameters:
seed - the seed.

setSeed

public void setSeed(long seed)
Reseeds both underlying generators with this seed. Reseeds this random object, using the eight bytes contained in the given 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.

Overrides:
setSeed in class java.security.SecureRandom
Parameters:
seed - the seed.

nextBytes

public void nextBytes(byte[] result)
Generates a user-specified number of random bytes. This method is used as the basis of all random entities returned by this class (except seed bytes).

Overrides:
nextBytes in class java.security.SecureRandom
Parameters:
result - the array to be filled in with random bytes.

generateSeed

public byte[] generateSeed(int numBytes)
Returns seed bytes from both generators XORed together. 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.

Overrides:
generateSeed in class java.security.SecureRandom
Parameters:
numBytes - the number of seed bytes to generate.
Returns:
the seed bytes.