java.lang.Object
gnu.crypto.prng.BasePRNGStandalone
gnu.crypto.prng.FortunaStandalone
gnu.crypto.prng.AsyncFortunaStandalone
- All Implemented Interfaces:
IRandomStandalone, Serializable, Cloneable, Runnable
Fortuna instance that tries to avoid blocking if at all possible by using separate filled buffer segments rather than one buffer (and blocking when that buffer's data has been eaten). Note that this class is not fully Thread safe! The following methods must be synchronized externally, they are not synced here or in super(): addRandomByte(), addRandomBytes(), nextByte(), nextBytes(),
seed(byte[])- See Also:
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classOne buffer filled with random bytes in the background.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intHow many buffers are cycled through, never less than 2.private final intThe size in bytes of each buffer, never less than 16384.private final I2PAppContextThe app context.The buffer the PRNG is currently handing out bytes from.private final LinkedBlockingQueue<AsyncFortunaStandalone.AsyncBuffer> Consumed buffers waiting to be refilled.private final LinkedBlockingQueue<AsyncFortunaStandalone.AsyncBuffer> Buffers holding freshly generated output, waiting to be consumed.private booleanTrue untilshutdown()is called, which stops the refill thread.private final LogThe logger for this class.private ThreadThe thread that refills empty buffers in the background.private final ObjectThe lock.private static final intNumber of buffers filled in the background.private static final intSize of each background buffer, in bytes.Fields inherited from class FortunaStandalone
generator, lastReseed, MIN_POOL_SIZE, NUM_POOLS, pool0Count, pools, reseedCount, SEEDModifier and TypeFieldDescriptionprotected final IRandomStandaloneThe underlying PRNG generator.protected longTimestamp of last reseed.(package private) static final intMinimum bytes in pool 0 before reseed.(package private) static final intNumber of entropy pools.protected intBytes added to pool 0.protected final MessageDigest[]Null if usingDevRandom.protected intNumber of reseeds performed.static final StringProperty name for the seed attribute.Fields inherited from class BasePRNGStandalone
buffer, initialised, name, ndxModifier and TypeFieldDescriptionprotected byte[]A temporary buffer to serve random bytes.protected booleanIndicate if this instance has already been initialised or not.protected final StringThe canonical name prefix of the PRNG algorithm.protected intThe index into buffer of where the next byte will come from.Constructor Summary
ConstructorsConstructorDescriptionAsyncFortunaStandalone(I2PAppContext context) Create a new AsyncFortunaStandalone.Method Summary
Modifier and TypeMethodDescriptionclone()Return a copy of this object.private voiddoFill(byte[] buf) Fills one buffer with random bytes.voidFill the PRNG output block.protected voidMake the next available filled buffer current, scheduling any unfilled buffers for refill, and blocking until at least one buffer is ready.voidrun()The refiller thread.voidseed(byte[] val) The seed is only propogated once the prng is started withstartup().voidshutdown()Stop the PRNG.voidstartup()Start the PRNG.Methods inherited from class FortunaStandalone
addRandomByte, addRandomBytes, setupModifier and TypeMethodDescriptionvoidaddRandomByte(byte b) Add a random byte to the entropy pool.voidaddRandomBytes(byte[] buf, int offset, int length) Add random bytes to the entropy pool.voidSet up the PRNG instance.Methods inherited from class BasePRNGStandalone
addRandomBytes, init, isInitialised, name, nextByte, nextBytes, nextBytesModifier and TypeMethodDescriptionvoidaddRandomBytes(byte[] buffer) Supplement, or possibly replace, the random state of this PRNG with a sequence of new random bytes.voidInitialises the pseudo-random number generator scheme with the appropriate attributes.booleanCheck whether this instance has been initialised.name()Returns the canonical name of this instance.bytenextByte()Returns the next 8 bits of random data generated from this instance.voidnextBytes(byte[] out) Fill the output byte array with random bytes.voidnextBytes(byte[] out, int offset, int length) Fills the designated byte array, starting from byte at indexoffset, for a maximum oflengthbytes with the output of this generator instance.
Field Details
DEFAULT_BUFFERS
private static final int DEFAULT_BUFFERSNumber of buffers filled in the background.- See Also:
DEFAULT_BUFSIZE
private static final int DEFAULT_BUFSIZESize of each background buffer, in bytes._bufferCount
private final int _bufferCountHow many buffers are cycled through, never less than 2._bufferSize
private final int _bufferSizeThe size in bytes of each buffer, never less than 16384.asyncBuffers
The lock._context
The app context._log
The logger for this class._isRunning
private volatile boolean _isRunningTrue untilshutdown()is called, which stops the refill thread._refillThread
The thread that refills empty buffers in the background._fullBuffers
Buffers holding freshly generated output, waiting to be consumed._emptyBuffers
Consumed buffers waiting to be refilled._currentBuffer
The buffer the PRNG is currently handing out bytes from.
Constructor Details
AsyncFortunaStandalone
Create a new AsyncFortunaStandalone.- Parameters:
context- the I2P app context
Method Details
clone
Return a copy of this object.- Specified by:
clonein interfaceIRandomStandalone- Overrides:
clonein classFortunaStandalone- Returns:
- a clone copy of this instance.
startup
public void startup()Start the PRNG.shutdown
public void shutdown()Stop the PRNG.seed
public void seed(byte[] val) The seed is only propogated once the prng is started withstartup().- Overrides:
seedin classFortunaStandalone- Parameters:
val- the seed data (unused)
rotateBuffer
protected void rotateBuffer()Make the next available filled buffer current, scheduling any unfilled buffers for refill, and blocking until at least one buffer is ready.run
fillBlock
doFill
private void doFill(byte[] buf) Fills one buffer with random bytes.- Parameters:
buf- the buffer to fill