FileDocCategorySizeDatePackage
SecureRandom.javaAPI DocAndroid 1.5 API12220Wed May 06 22:41:06 BST 2009java.security

SecureRandom

public class SecureRandom extends Random
{@code SecureRandom} is an engine class which is capable of generating cryptographically secure pseudo-random numbers.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private static final transient String
SERVICE
private static transient org.apache.harmony.security.fortress.Engine
engine
private Provider
provider
private SecureRandomSpi
secureRandomSpi
private String
algorithm
private byte[]
state
private byte[]
randomBytes
private int
randomBytesUsed
private long
counter
private static transient SecureRandom
internalSecureRandom
Constructors Summary
public SecureRandom()
Constructs a new instance of {@code SecureRandom}. An implementation for the highest-priority provider is returned. The constructed instance will not have been seeded.

since
Android 1.0

    
                                    
      
        super(0);
        Provider.Service service = findService();
        if (service == null) {
            this.provider = null;
            this.secureRandomSpi = new SHA1PRNG_SecureRandomImpl();
            this.algorithm = "SHA1PRNG"; //$NON-NLS-1$
        } else {
            try {
                this.provider = service.getProvider();
                this.secureRandomSpi = (SecureRandomSpi)service.newInstance(null);
                this.algorithm = service.getAlgorithm();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }            
        }    
    
public SecureRandom(byte[] seed)
Constructs a new instance of {@code SecureRandom}. An implementation for the highest-priority provider is returned. The constructed instance will be seeded with the parameter.

param
seed the seed for this generator.
since
Android 1.0

        this();
        setSeed(seed);
    
protected SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider)
Constructs a new instance of {@code SecureRandom} using the given implementation from the specified provider.

param
secureRandomSpi the implementation.
param
provider the security provider.
since
Android 1.0

        this(secureRandomSpi, provider, "unknown"); //$NON-NLS-1$
    
private SecureRandom(SecureRandomSpi secureRandomSpi, Provider provider, String algorithm)

        super(0);
        this.provider = provider;
        this.algorithm = algorithm;
        this.secureRandomSpi = secureRandomSpi;
    
Methods Summary
private java.security.Provider$ServicefindService()

        Set s;
        Provider.Service service;
        for (Iterator it1 = Services.getProvidersList().iterator(); it1.hasNext();) {
            service = ((Provider)it1.next()).getService("SecureRandom"); //$NON-NLS-1$
            if (service != null) {
                return service;
            }
        }
        return null;
    
public byte[]generateSeed(int numBytes)
Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by this {@code SecureRandom}.

param
numBytes the number of seed bytes.
return
the seed bytes.
since
Android 1.0

        return secureRandomSpi.engineGenerateSeed(numBytes);
    
public java.lang.StringgetAlgorithm()
Returns the name of the algorithm of this {@code SecureRandom}.

return
the name of the algorithm of this {@code SecureRandom}.
since
Android 1.0

        return algorithm;
    
public static java.security.SecureRandomgetInstance(java.lang.String algorithm)
Returns a new instance of {@code SecureRandom} that utilizes the specified algorithm.

param
algorithm the name of the algorithm to use.
return
a new instance of {@code SecureRandom} that utilizes the specified algorithm.
throws
NoSuchAlgorithmException if the specified algorithm is not available.
throws
NullPointerException if {@code algorithm} is {@code null}.
since
Android 1.0

        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("security.01")); //$NON-NLS-1$
        }
        synchronized (engine) {
            engine.getInstance(algorithm, null);
            return new SecureRandom((SecureRandomSpi)engine.spi, engine.provider, algorithm);
        }
    
public static java.security.SecureRandomgetInstance(java.lang.String algorithm, java.lang.String provider)
Returns a new instance of {@code SecureRandom} that utilizes the specified algorithm from the specified provider.

param
algorithm the name of the algorithm to use.
param
provider the name of the provider.
return
a new instance of {@code SecureRandom} that utilizes the specified algorithm from the specified provider.
throws
NoSuchAlgorithmException if the specified algorithm is not available.
throws
NoSuchProviderException if the specified provider is not available.
throws
NullPointerException if {@code algorithm} is {@code null}.
since
Android 1.0

        if ((provider == null) || (provider.length() == 0)) {
            throw new IllegalArgumentException(
                    Messages.getString("security.02")); //$NON-NLS-1$
        }
        Provider p = Security.getProvider(provider);
        if (p == null) {
            throw new NoSuchProviderException(Messages.getString("security.03", provider));  //$NON-NLS-1$
        }
        return getInstance(algorithm, p);    
    
public static java.security.SecureRandomgetInstance(java.lang.String algorithm, java.security.Provider provider)
Returns a new instance of {@code SecureRandom} that utilizes the specified algorithm from the specified provider.

param
algorithm the name of the algorithm to use.
param
provider the security provider.
return
a new instance of {@code SecureRandom} that utilizes the specified algorithm from the specified provider.
throws
NoSuchAlgorithmException if the specified algorithm is not available.
throws
NullPointerException if {@code algorithm} is {@code null}.
since
Android 1.0

        if (provider == null) {
            throw new IllegalArgumentException(Messages.getString("security.04")); //$NON-NLS-1$
        }
        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("security.01")); //$NON-NLS-1$
        }
        synchronized (engine) {
            engine.getInstance(algorithm, provider, null);
            return new SecureRandom((SecureRandomSpi)engine.spi, provider, algorithm);
        }
    
public final java.security.ProvidergetProvider()
Returns the provider associated with this {@code SecureRandom}.

return
the provider associated with this {@code SecureRandom}.
since
Android 1.0

        return provider;
    
public static byte[]getSeed(int numBytes)
Generates and returns the specified number of seed bytes, computed using the seed generation algorithm used by this {@code SecureRandom}.

param
numBytes the number of seed bytes.
return
the seed bytes
since
Android 1.0

        if (internalSecureRandom == null) {
            internalSecureRandom = new SecureRandom();
        }
        return internalSecureRandom.generateSeed(numBytes);
    
protected final intnext(int numBits)
Generates and returns an {@code int} containing the specified number of random bits (right justified, with leading zeros).

param
numBits number of bits to be generated. An input value should be in the range [0, 32].
return
an {@code int} containing the specified number of random bits.
since
Android 1.0

        if (numBits < 0) {
            numBits = 0;
        } else {
            if (numBits > 32) {
                numBits = 32;
            }
        }
        int bytes = (numBits+7)/8;
        byte[] next = new byte[bytes];
        int ret = 0;
         
        nextBytes(next);
        for (int i = 0; i < bytes; i++) {
            ret = (next[i] & 0xFF) | (ret << 8);
        }    
        ret = ret >>> (bytes*8 - numBits);
        return ret;
    
public synchronized voidnextBytes(byte[] bytes)
Generates and stores random bytes in the given {@code byte[]} for each array element.

param
bytes the {@code byte[]} to be filled with random bytes.
since
Android 1.0

        secureRandomSpi.engineNextBytes(bytes);
    
public synchronized voidsetSeed(byte[] seed)
Reseeds this {@code SecureRandom} instance with the specified {@code seed}. The seed of this {@code SecureRandom} instance is supplemented, not replaced.

param
seed the new seed.
since
Android 1.0

        secureRandomSpi.engineSetSeed(seed);
    
public voidsetSeed(long seed)
Reseeds this this {@code SecureRandom} instance with the eight bytes described by the representation of the given {@code long seed}. The seed of this {@code SecureRandom} instance is supplemented, not replaced.

param
seed the new seed.
since
Android 1.0

        if (seed == 0) {    // skip call from Random
            return;
        }
        byte[] byteSeed = {
                (byte)((seed >> 56) & 0xFF),
                (byte)((seed >> 48) & 0xFF),
                (byte)((seed >> 40) & 0xFF),
                (byte)((seed >> 32) & 0xFF),
                (byte)((seed >> 24) & 0xFF),
                (byte)((seed >> 16) & 0xFF),
                (byte)((seed >> 8) & 0xFF),
                (byte)((seed) & 0xFF)
        };
        setSeed(byteSeed);