Package org.apache.shiro.crypto
Class SecureRandomNumberGenerator
- java.lang.Object
-
- org.apache.shiro.crypto.SecureRandomNumberGenerator
-
- All Implemented Interfaces:
RandomNumberGenerator
public class SecureRandomNumberGenerator extends Object implements RandomNumberGenerator
Default implementation of theRandomNumberGeneratorinterface, backed by aSecureRandominstance. This class is a little easier to use than using the JDK'sSecureRandomclass 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
Fields Modifier and Type Field Description protected static intDEFAULT_NEXT_BYTES_SIZE
-
Constructor Summary
Constructors Constructor Description SecureRandomNumberGenerator()Creates a new instance with a default backingSecureRandomand adefaultNextBytesSizeof16, which equals 128 bits, a size commonly used in cryptographic algorithms.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intgetDefaultNextBytesSize()Returns the size of the generated byte array for calls tonextBytes().SecureRandomgetSecureRandom()Returns theSecureRandombacking this instance.org.apache.shiro.util.ByteSourcenextBytes()Generates a byte array of fixed length filled with random data, often useful for generating salts, initialization vectors or other seed data.org.apache.shiro.util.ByteSourcenextBytes(int numBytes)Generates a byte array of the specified length filled with random data.voidsetDefaultNextBytesSize(int defaultNextBytesSize)Sets the size of the generated byte array for calls tonextBytes().voidsetSecureRandom(SecureRandom random)Sets theSecureRandomto back this instance.voidsetSeed(byte[] bytes)Seeds the backingSecureRandominstance with additional seed data.
-
-
-
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 backingSecureRandomand adefaultNextBytesSizeof16, which equals 128 bits, a size commonly used in cryptographic algorithms.
-
-
Method Detail
-
setSeed
public void setSeed(byte[] bytes)
Seeds the backingSecureRandominstance with additional seed data.- Parameters:
bytes- the seed bytes- See Also:
SecureRandom.setSeed(byte[])
-
getSecureRandom
public SecureRandom getSecureRandom()
Returns theSecureRandombacking this instance.- Returns:
- the
SecureRandombacking this instance.
-
setSecureRandom
public void setSecureRandom(SecureRandom random) throws NullPointerException
Sets theSecureRandomto back this instance.- Parameters:
random- theSecureRandomto 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 tonextBytes(). Defaults to16, 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 IllegalArgumentExceptionSets the size of the generated byte array for calls tonextBytes(). Defaults to16, which equals 128 bits, a size commonly used in cryptographic algorithms.- Parameters:
defaultNextBytesSize- the size of the generated byte array for calls tonextBytes().- Throws:
IllegalArgumentException- if the argument is 0 or negative
-
nextBytes
public org.apache.shiro.util.ByteSource nextBytes()
Description copied from interface:RandomNumberGeneratorGenerates 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 thenextBytes(int)method instead.- Specified by:
nextBytesin interfaceRandomNumberGenerator- Returns:
- a byte array of fixed length filled with random data.
- See Also:
RandomNumberGenerator.nextBytes(int)
-
nextBytes
public org.apache.shiro.util.ByteSource nextBytes(int numBytes)
Description copied from interface:RandomNumberGeneratorGenerates a byte array of the specified length filled with random data.- Specified by:
nextBytesin interfaceRandomNumberGenerator- 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()
-
-