iaik.security.random
Class SecRandom

java.lang.Object
  |
  +--java.util.Random
        |
        +--java.security.SecureRandom
              |
              +--iaik.security.random.SecRandom
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AnsiRandom, FIPS186Random, MessageDigestRandom

public abstract class SecRandom
extends SecureRandom

The SecRandom class is the base class of all IAIK implemented secure pseudo random number generators (PRNGs). For compatibility with the JDK it is a subclass of java.security.SecureRandom, which in turn extends java.util.Random. Programmers who themselves want to implement a secure PRNG should subclass this class for compatibility and because this class takes care of some of the pecularities of the JDK SecureRandom.

Subclasses just need to implement the abstract engineNextBytes() and engineSetSeed() methods and a no-args constructor. Subclasses should not seed themselves in the constructor, they should wait for the application to call a setSeed() method explicitly with data from an appropriate random source. This class makes sure that engineNextBytes() is never called before a seed has been set, if necessary by calling setSeed() itself with data from the default SeedGenerator.

This class also serves as storage point for the system's default secure PRNG. Applications which want to use a secure PRNG should typically call SecRandom.getDefault() instead of instantiating one particular PRNG directly. This will allow the PRNG used throughout the application to be set by a simple SecRandom.setDefault() call. The initial default PRNG is iaik.security.random.SHA1Random.

Version:
File Revision 19
See Also:
SecureRandom, MessageDigestRandom, SHA1Random, SeedGenerator, Serialized Form

Constructor Summary
protected SecRandom()
          Constructor for use by subclasses.
 
Method Summary
protected abstract  void engineNextBytes(byte[] bytes)
          Return the next bytes.length random bytes.
protected abstract  void engineSetSeed(byte[] seed)
          Set the seed to be used for random number generation.
static SecureRandom getDefault()
          Return a new instance of the default random number generator.
 void nextBytes(byte[] bytes)
          Return the next bytes.length random bytes.
static void setDefault(Class clazz)
          Set the default random number generator.
 void setSeed()
          Set the seed to the next seed generated by the current default seed generator.
 void setSeed(byte[] seed)
          Set the seed to be used for random number generation.
 void setSeed(long seed)
          Set the seed to the given long int.
 
Methods inherited from class java.security.SecureRandom
generateSeed, 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

SecRandom

protected SecRandom()
Constructor for use by subclasses. It does not do anything except call the SecureRandom superclass constructor.
Method Detail

getDefault

public static final SecureRandom getDefault()
Return a new instance of the default random number generator. It will not be assigned a seed (see also nextBytes()). Calling getDefault() is the recommended way of obtaining a PRNG.

setDefault

public static final void setDefault(Class clazz)
                             throws RandomException
Set the default random number generator. Instances of this PRNG will be returned by subsequent calls of getDefault(). The PRNG must have a no-args constructor accessible to this class.
Parameters:
clazz - The default random class. It must be java.security.SecureRandom or a subclass of it.
Throws:
IllegalArgumentException - if there was an error with the SecureRandom

nextBytes

public final void nextBytes(byte[] bytes)
Return the next bytes.length random bytes. If the SecRandom has not been seeded before, setSeed() will be called. This methods calls engineNextBytes().
Overrides:
nextBytes in class SecureRandom

setSeed

public final void setSeed(byte[] seed)
Set the seed to be used for random number generation. This methods calls engineSetSeed().
Overrides:
setSeed in class SecureRandom

setSeed

public final void setSeed(long seed)
Set the seed to the given long int. This method converts the long to a byte array (two's complement, MSB first) and calls setSeed(byte[]). This method is included mainly for compatibility, a 64 bit seed will generally be too short for cryptographic applications and should not be used.
Overrides:
setSeed in class SecureRandom

setSeed

public final void setSeed()
Set the seed to the next seed generated by the current default seed generator. That means, calling this method is equivalent to calling setSeed(SeedGenerator.getDefault().getSeed());.

engineNextBytes

protected abstract void engineNextBytes(byte[] bytes)
Return the next bytes.length random bytes. This methods has to be implemented by subclasses.

engineSetSeed

protected abstract void engineSetSeed(byte[] seed)
Set the seed to be used for random number generation. This methods has to be implemented by subclasses.

This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK