Class AESCounterRNG
- java.lang.Object
-
- java.util.Random
-
- org.uncommons.maths.random.AESCounterRNG
-
- All Implemented Interfaces:
java.io.Serializable
,RepeatableRNG
public class AESCounterRNG extends java.util.Random implements RepeatableRNG
Non-linear random number generator based on the AES block cipher in counter mode. Uses the seed as a key to encrypt a 128-bit counter using AES(Rijndael).
By default, we only use a 128-bit key for the cipher because any larger key requires the inconvenience of installing the unlimited strength cryptography policy files for the Java platform. Larger keys may be used (192 or 256 bits) but if the cryptography policy files are not installed, a
GeneralSecurityException
will be thrown.NOTE: Because instances of this class require 128-bit seeds, it is not possible to seed this RNG using the
Random.setSeed(long)
method inherited fromRandom
. Calls to this method will have no effect. Instead the seed must be set by a constructor.NOTE: THIS CLASS IS NOT SERIALIZABLE
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
AESCounterRNG.AESKey
Trivial key implementation for use with AES cipher.
-
Field Summary
Fields Modifier and Type Field Description private javax.crypto.Cipher
cipher
private byte[]
counter
private byte[]
currentBlock
private static int
DEFAULT_SEED_SIZE_BYTES
private int
index
private java.util.concurrent.locks.ReentrantLock
lock
private byte[]
seed
-
Constructor Summary
Constructors Constructor Description AESCounterRNG()
Creates a new RNG and seeds it using 128 bits from the default seeding strategy.AESCounterRNG(byte[] seed)
Creates an RNG and seeds it with the specified seed data.AESCounterRNG(int seedSizeBytes)
Seed the RNG using the default seed generation strategy to create a seed of the specified size.AESCounterRNG(SeedGenerator seedGenerator)
Seed the RNG using the provided seed generation strategy to create a 128-bit seed.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]
getSeed()
private void
incrementCounter()
protected int
next(int bits)
private byte[]
nextBlock()
Generates a single 128-bit block (16 bytes).
-
-
-
Field Detail
-
DEFAULT_SEED_SIZE_BYTES
private static final int DEFAULT_SEED_SIZE_BYTES
- See Also:
- Constant Field Values
-
seed
private final byte[] seed
-
cipher
private final javax.crypto.Cipher cipher
-
counter
private final byte[] counter
-
lock
private final java.util.concurrent.locks.ReentrantLock lock
-
currentBlock
private byte[] currentBlock
-
index
private int index
-
-
Constructor Detail
-
AESCounterRNG
public AESCounterRNG() throws java.security.GeneralSecurityException
Creates a new RNG and seeds it using 128 bits from the default seeding strategy.- Throws:
java.security.GeneralSecurityException
- If there is a problem initialising the AES cipher.
-
AESCounterRNG
public AESCounterRNG(SeedGenerator seedGenerator) throws SeedException, java.security.GeneralSecurityException
Seed the RNG using the provided seed generation strategy to create a 128-bit seed.- Parameters:
seedGenerator
- The seed generation strategy that will provide the seed value for this RNG.- Throws:
SeedException
- If there is a problem generating a seed.java.security.GeneralSecurityException
- If there is a problem initialising the AES cipher.
-
AESCounterRNG
public AESCounterRNG(int seedSizeBytes) throws java.security.GeneralSecurityException
Seed the RNG using the default seed generation strategy to create a seed of the specified size.- Parameters:
seedSizeBytes
- The number of bytes to use for seed data. Valid values are 16 (128 bits), 24 (192 bits) and 32 (256 bits). Any other values will result in an exception from the AES implementation.- Throws:
java.security.GeneralSecurityException
- If there is a problem initialising the AES cipher.- Since:
- 1.0.2
-
AESCounterRNG
public AESCounterRNG(byte[] seed) throws java.security.GeneralSecurityException
Creates an RNG and seeds it with the specified seed data.- Parameters:
seed
- The seed data used to initialise the RNG.- Throws:
java.security.GeneralSecurityException
- If there is a problem initialising the AES cipher.
-
-
Method Detail
-
getSeed
public byte[] getSeed()
- Specified by:
getSeed
in interfaceRepeatableRNG
- Returns:
- The seed data used to initialise this pseudo-random number generator.
-
incrementCounter
private void incrementCounter()
-
nextBlock
private byte[] nextBlock() throws java.security.GeneralSecurityException
Generates a single 128-bit block (16 bytes).- Returns:
- A 16-byte block of random data.
- Throws:
java.security.GeneralSecurityException
- If there is a problem with the cipher that generates the random data.
-
next
protected final int next(int bits)
- Overrides:
next
in classjava.util.Random
-
-