com.motorola.iden.security
Class SecureRandom

java.lang.Object
  |
  +--java.util.Random
        |
        +--com.motorola.iden.security.SecureRandom
All Implemented Interfaces:
java.io.Serializable

public class SecureRandom
extends java.util.Random

This class provides a cryptographically strong pseudo-random number generator (PRNG).

Like other algorithm-based classes in Java Security, SecureRandom provides implementation-independent algorithms, whereby a caller (application code) requests a particular PRNG algorithm and is handed back a SecureRandom object for that algorithm.

  • If just an algorithm name is specified, as in:
          SecureRandom random = SecureRandom.getInstance("FIPS186");
     

    The SecureRandom implementation attempts to completely randomize the internal state of the generator itself unless the caller follows the call to a getInstance method with a call to the setSeed method:

          SecureRandom random = SecureRandom.getInstance("FIPS186");
          random.setSeed(seed);
     

    After the caller obtains the SecureRandom object from the getInstance call, it can call nextBytes to generate random bytes:

          byte bytes[] = new byte[20];
          random.nextBytes(bytes);
     

    The caller may also invoke the generateSeed method to generate a given number of seed bytes (to seed other random number generators, for example):

          byte seed[] = random.generateSeed(20);
     

    MOTOROLA and the Stylized M Logo are registered trademarks of Motorola, Inc. Reg. U.S. Pat. & Tm. Off.
    © Copyright 2002 - 2004 Motorola, Inc. All Rights Reserved.

    See Also:
    Serialized Form

    Constructor Summary
    SecureRandom(byte[] seed)
              This constructor is provided for backwards compatibility.
     
    Method Summary
     byte[] generateSeed(int numBytes)
              Returns the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself.
    static SecureRandom getInstance(java.lang.String algorithm)
              Creates a SecureRandom object.
    static SecureRandom getInstance(java.lang.String algorithm, java.lang.String Package)
              Creates a SecureRandom object.
     void nextBytes(byte[] bytes)
              Generates a user-specified number of random bytes.
     void setSeed(byte[] seed)
              Reseeds this random object.
     
    Methods inherited from class java.util.Random
    next, nextBoolean, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong, setSeed
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Constructor Detail

    SecureRandom

    public SecureRandom(byte[] seed)

    This constructor is provided for backwards compatibility. The caller is encouraged to use one of the alternative getInstance methods to obtain a SecureRandom object, and then to call the setSeed method to seed it.

    Parameters:
    seed - the seed.
    Method Detail

    getInstance

    public static SecureRandom getInstance(java.lang.String algorithm)
                                    throws NoSuchAlgorithmException
    Creates a SecureRandom object.
    Returns:
    the new SecureRandom object.
    Throws:
    NoSuchAlgorithmException - if the PRNG algorithm is not available in the caller's environment.

    getInstance

    public static SecureRandom getInstance(java.lang.String algorithm,
                                           java.lang.String Package)
                                    throws NoSuchAlgorithmException
    Creates a SecureRandom object.
    Parameters:
    algorithm - the name of RNG algorithm
    Package - the Package which the end user provied RNG belongs to.
    Returns:
    the new SecureRandom object.
    Throws:
    NoSuchAlgorithmException - if the PRNG algorithm is not available in the caller's environment.

    setSeed

    public void setSeed(byte[] seed)
    Reseeds this random object. The given seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.If seed is null, system generated seed would be used.
    Parameters:
    seed - the seed.

    nextBytes

    public void nextBytes(byte[] bytes)
    Generates a user-specified number of random bytes. If bytes equals to null, no random bytes would be generated.
    Overrides:
    nextBytes in class java.util.Random
    Parameters:
    bytes - the array to be filled in with random bytes.

    generateSeed

    public byte[] generateSeed(int numBytes)
    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. If the numBytes equals to or less than zero, null is returned.
    Parameters:
    numBytes - the number of seed bytes to generate.
    Returns:
    the seed bytes.