com.mindbright.util
Class RandomSeed

java.lang.Object
  extended by com.mindbright.util.RandomSeed
All Implemented Interfaces:
java.awt.event.ComponentListener, java.awt.event.FocusListener, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.util.EventListener

public final class RandomSeed
extends java.lang.Object
implements java.awt.event.MouseMotionListener, java.awt.event.MouseListener, java.awt.event.KeyListener, java.awt.event.FocusListener, java.awt.event.ComponentListener

Generates a few high quality random numbers which are useful to seed other pseudo random number generators. This class will read frome system devices if possible. But it also has code to collect and handle randomness from user interaction.

The whole concept of cryptographically strong random numbers is very complicated. This class holds a pool of randomness and always estimates how much entropy (randomness) the pool contains. The internal pool is always stirred whenever entropy is extracted.


Constructor Summary
RandomSeed()
          Create an instance which does not load random numbers from system devices.
RandomSeed(java.lang.String devRandName, java.lang.String devURandName)
          Create an instance which tries to use system random devices.
 
Method Summary
 void addEntropyGenerator(java.awt.Component c)
          Register event handles for a component so entropy can be collected from events.
 void addProgress(Progress progress)
          Connect to a progress meeter which gives feedback how the initialization goes.
 void componentHidden(java.awt.event.ComponentEvent e)
          Extract entropy from a component event.
 void componentMoved(java.awt.event.ComponentEvent e)
          Extract entropy from a component event.
 void componentResized(java.awt.event.ComponentEvent e)
          Extract entropy from a component event.
 void componentShown(java.awt.event.ComponentEvent e)
          Extract entropy from a component event.
 void focusGained(java.awt.event.FocusEvent e)
          Extract entropy from a focus event.
 void focusLost(java.awt.event.FocusEvent e)
          Extract entropy from a focus event.
 int getAvailableBits()
          Gets the estimate of how much entropy the randomness pool contains.
 byte[] getBytes(int numBytes)
          Gets random bytes in a non-blocking way.
 byte[] getBytesBlocking(int numBytes)
          Gets random bytes in a blocking way.
 byte[] getBytesBlocking(int numBytes, boolean generatorIfPresent)
          Gets random bytes in a blocking way.
static byte[] getSystemStateHash()
          Get a hash-value which reflects the current system state.
 boolean haveEntropyGenerator()
          Checks if an entropy generator has been added
 void keyPressed(java.awt.event.KeyEvent e)
          Extracts entropy from key presses.
 void keyReleased(java.awt.event.KeyEvent e)
          Extracts entropy from key releases.
 void keyTyped(java.awt.event.KeyEvent e)
          Extracts entropy from key typed events.
 void mouseClicked(java.awt.event.MouseEvent e)
          Extract entropy from a mouse event.
 void mouseDragged(java.awt.event.MouseEvent e)
          Extract entropy from a mouse event.
 void mouseEntered(java.awt.event.MouseEvent e)
          Extract entropy from a mouse event.
 void mouseExited(java.awt.event.MouseEvent e)
          Extract entropy from a mouse event.
 void mouseMoved(java.awt.event.MouseEvent e)
          Extract entropy from a mouse event.
 void mousePressed(java.awt.event.MouseEvent e)
          Extract entropy from a mouse event.
 void mouseReleased(java.awt.event.MouseEvent e)
          Extract entropy from a mouse event.
 void removeEntropyGenerator(java.awt.Component c)
          Unregister event handles for a component.
 void removeProgress()
          Detach from progress meter.
 void resetEntropyCount()
          Reset the entropy count to zero.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomSeed

public RandomSeed()
Create an instance which does not load random numbers from system devices.


RandomSeed

public RandomSeed(java.lang.String devRandName,
                  java.lang.String devURandName)
Create an instance which tries to use system random devices.

Parameters:
devRandName - name of blocking random device. Typically /dev/random
devURandName - name of non-blocking random device. Typically /dev/urandom
Method Detail

addProgress

public void addProgress(Progress progress)
Connect to a progress meeter which gives feedback how the initialization goes.

Parameters:
progress - progress indicator

removeProgress

public void removeProgress()
Detach from progress meter.


addEntropyGenerator

public void addEntropyGenerator(java.awt.Component c)
Register event handles for a component so entropy can be collected from events.

Parameters:
c - component to attach event handles to

removeEntropyGenerator

public void removeEntropyGenerator(java.awt.Component c)
Unregister event handles for a component.

Parameters:
c - component to detach from

haveEntropyGenerator

public boolean haveEntropyGenerator()
Checks if an entropy generator has been added

Returns:
true if an entropy generator has been added

getAvailableBits

public int getAvailableBits()
Gets the estimate of how much entropy the randomness pool contains.

Returns:
the estimated entropy measured in bits

resetEntropyCount

public void resetEntropyCount()
Reset the entropy count to zero.


getBytes

public byte[] getBytes(int numBytes)
Gets random bytes in a non-blocking way. Note that this function may generate random numbers of lower quality of there is not enough entropy in the pool.

Parameters:
numBytes - number of bytes to extract
Returns:
an array containing the requested number of random bytes.

getBytesBlocking

public byte[] getBytesBlocking(int numBytes)
Gets random bytes in a blocking way. This function will block if there is not enough entropy available.

Parameters:
numBytes - number of bytes to extract
Returns:
an array containing the requested number of random bytes.

getBytesBlocking

public byte[] getBytesBlocking(int numBytes,
                               boolean generatorIfPresent)
Gets random bytes in a blocking way. This function will block if there is not enough entropy available. With this function is possible to control how entropy is collected if there is not enough available.

Parameters:
numBytes - number of bytes to extract
generatorIfPresent - if true then the function waits for the attached generators to generate enough randomness. If false the function will use the local spin generator.
Returns:
an array containing the requested number of random bytes.

getSystemStateHash

public static byte[] getSystemStateHash()
Get a hash-value which reflects the current system state. The value is the SHA1 hash over a bunch of data while includes the current time, memory information, all system properties, IP address etc. This hash can be used as a random seed, it is not cryptographically strong but is a lot better than nothing.

Returns:
a byte array containg data which reflects the current system state.

keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Extracts entropy from key presses.

Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Extracts entropy from key releases.

Specified by:
keyReleased in interface java.awt.event.KeyListener

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Extracts entropy from key typed events.

Specified by:
keyTyped in interface java.awt.event.KeyListener

componentHidden

public void componentHidden(java.awt.event.ComponentEvent e)
Extract entropy from a component event.

Specified by:
componentHidden in interface java.awt.event.ComponentListener

componentMoved

public void componentMoved(java.awt.event.ComponentEvent e)
Extract entropy from a component event.

Specified by:
componentMoved in interface java.awt.event.ComponentListener

componentResized

public void componentResized(java.awt.event.ComponentEvent e)
Extract entropy from a component event.

Specified by:
componentResized in interface java.awt.event.ComponentListener

componentShown

public void componentShown(java.awt.event.ComponentEvent e)
Extract entropy from a component event.

Specified by:
componentShown in interface java.awt.event.ComponentListener

focusGained

public void focusGained(java.awt.event.FocusEvent e)
Extract entropy from a focus event.

Specified by:
focusGained in interface java.awt.event.FocusListener

focusLost

public void focusLost(java.awt.event.FocusEvent e)
Extract entropy from a focus event.

Specified by:
focusLost in interface java.awt.event.FocusListener

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Extract entropy from a mouse event.

Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Extract entropy from a mouse event.

Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Extract entropy from a mouse event.

Specified by:
mouseExited in interface java.awt.event.MouseListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Extract entropy from a mouse event.

Specified by:
mousePressed in interface java.awt.event.MouseListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Extract entropy from a mouse event.

Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Extract entropy from a mouse event.

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Extract entropy from a mouse event.

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener