public class SecureRandomNumberGenerator extends Object implements RandomNumberGenerator
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.Modifier and Type | Field and Description |
---|---|
protected static int |
DEFAULT_NEXT_BYTES_SIZE |
Constructor and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
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. |
protected static final int DEFAULT_NEXT_BYTES_SIZE
public SecureRandomNumberGenerator()
SecureRandom
and a
defaultNextBytesSize
of 16
, which equals 128 bits, a size commonly
used in cryptographic algorithms.public void setSeed(byte[] bytes)
SecureRandom
instance with additional seed data.bytes
- the seed bytesSecureRandom.setSeed(byte[])
public SecureRandom getSecureRandom()
SecureRandom
backing this instance.SecureRandom
backing this instance.public void setSecureRandom(SecureRandom random) throws NullPointerException
SecureRandom
to back this instance.random
- the SecureRandom
to back this instance.NullPointerException
- if the method argument is nullpublic int getDefaultNextBytesSize()
nextBytes()
. Defaults to
16
, which equals 128 bits, a size commonly used in cryptographic algorithms.nextBytes()
.public void setDefaultNextBytesSize(int defaultNextBytesSize) throws IllegalArgumentException
nextBytes()
. Defaults to
16
, which equals 128 bits, a size commonly used in cryptographic algorithms.defaultNextBytesSize
- the size of the generated byte array for calls to nextBytes()
.IllegalArgumentException
- if the argument is 0 or negativepublic ByteSource nextBytes()
RandomNumberGenerator
nextBytes(int)
method instead.nextBytes
in interface RandomNumberGenerator
RandomNumberGenerator.nextBytes(int)
public ByteSource nextBytes(int numBytes)
RandomNumberGenerator
nextBytes
in interface RandomNumberGenerator
numBytes
- the number of bytes to be populated with random data.RandomNumberGenerator.nextBytes()
Copyright © 2004–2019 The Apache Software Foundation. All rights reserved.