org.apache.shiro.crypto
Class SecureRandomNumberGenerator

java.lang.Object
  extended by org.apache.shiro.crypto.SecureRandomNumberGenerator
All Implemented Interfaces:
RandomNumberGenerator

public class SecureRandomNumberGenerator
extends Object
implements RandomNumberGenerator

Default implementation of the RandomNumberGenerator interface, backed by a SecureRandom instance.

This class is a little easier to use than using the JDK's SecureRandom class directly. It also allows for JavaBeans-style of customization, convenient for Shiro's INI configuration or other IoC configuration mechanism.

Since:
1.1

Field Summary
protected static int DEFAULT_NEXT_BYTES_SIZE
           
 
Constructor Summary
SecureRandomNumberGenerator()
          Creates a new instance with a default backing SecureRandom and a defaultNextBytesSize of 16, which equals 128 bits, a size commonly used in cryptographic algorithms.
 
Method Summary
 int getDefaultNextBytesSize()
          Returns the size of the generated byte array for calls to nextBytes().
 SecureRandom getSecureRandom()
          Returns the SecureRandom backing this instance.
 ByteSource nextBytes()
          Generates a byte array of fixed length filled with random data, often useful for generating salts, initialization vectors or other seed data.
 ByteSource nextBytes(int numBytes)
          Generates a byte array of the specified length filled with random data.
 void setDefaultNextBytesSize(int defaultNextBytesSize)
          Sets the size of the generated byte array for calls to nextBytes().
 void setSecureRandom(SecureRandom random)
          Sets the SecureRandom to back this instance.
 void setSeed(byte[] bytes)
          Seeds the backing SecureRandom instance with additional seed data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_NEXT_BYTES_SIZE

protected static final int DEFAULT_NEXT_BYTES_SIZE
See Also:
Constant Field Values
Constructor Detail

SecureRandomNumberGenerator

public SecureRandomNumberGenerator()
Creates a new instance with a default backing SecureRandom and a defaultNextBytesSize of 16, which equals 128 bits, a size commonly used in cryptographic algorithms.

Method Detail

setSeed

public void setSeed(byte[] bytes)
Seeds the backing SecureRandom instance with additional seed data.

Parameters:
bytes - the seed bytes
See Also:
SecureRandom.setSeed(byte[])

getSecureRandom

public SecureRandom getSecureRandom()
Returns the SecureRandom backing this instance.

Returns:
the SecureRandom backing this instance.

setSecureRandom

public void setSecureRandom(SecureRandom random)
                     throws NullPointerException
Sets the SecureRandom to back this instance.

Parameters:
random - the SecureRandom to back this instance.
Throws:
NullPointerException - if the method argument is null

getDefaultNextBytesSize

public int getDefaultNextBytesSize()
Returns the size of the generated byte array for calls to nextBytes(). Defaults to 16, which equals 128 bits, a size commonly used in cryptographic algorithms.

Returns:
the size of the generated byte array for calls to nextBytes().

setDefaultNextBytesSize

public void setDefaultNextBytesSize(int defaultNextBytesSize)
                             throws IllegalArgumentException
Sets the size of the generated byte array for calls to nextBytes(). Defaults to 16, which equals 128 bits, a size commonly used in cryptographic algorithms.

Parameters:
defaultNextBytesSize - the size of the generated byte array for calls to nextBytes().
Throws:
IllegalArgumentException - if the argument is 0 or negative

nextBytes

public ByteSource nextBytes()
Description copied from interface: RandomNumberGenerator
Generates a byte array of fixed length filled with random data, often useful for generating salts, initialization vectors or other seed data. The length is specified as a configuration value on the underlying implementation.

If you'd like per-invocation control the number of bytes generated, use the nextBytes(int) method instead.

Specified by:
nextBytes in interface RandomNumberGenerator
Returns:
a byte array of fixed length filled with random data.
See Also:
RandomNumberGenerator.nextBytes(int)

nextBytes

public ByteSource nextBytes(int numBytes)
Description copied from interface: RandomNumberGenerator
Generates a byte array of the specified length filled with random data.

Specified by:
nextBytes in interface RandomNumberGenerator
Parameters:
numBytes - the number of bytes to be populated with random data.
Returns:
a byte array of the specified length filled with random data.
See Also:
RandomNumberGenerator.nextBytes()


Copyright © 2004-2014 The Apache Software Foundation. All Rights Reserved.