java.lang.Object
java.util.Random
java.security.SecureRandom
net.i2p.util.RandomSource
net.i2p.util.FortunaRandomSource
- All Implemented Interfaces:
Serializable, java.util.random.RandomGenerator, EntropyHarvester
Wrapper around GNU-Crypto's Fortuna PRNG. This seeds from /dev/urandom and ./prngseed.rnd on startup (if they exist), writing a new seed to ./prngseed.rnd on an explicit call to saveSeed().
- See Also:
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.random.RandomGenerator
java.util.random.RandomGenerator.ArbitrarilyJumpableGenerator, java.util.random.RandomGenerator.JumpableGenerator, java.util.random.RandomGenerator.LeapableGenerator, java.util.random.RandomGenerator.SplittableGenerator, java.util.random.RandomGenerator.StreamableGeneratorField Summary
FieldsModifier and TypeFieldDescriptionprivate final AsyncFortunaStandaloneThe Fortuna PRNG instance.private booleanWhether a cached Gaussian value is available.private doubleNext Gaussian value, cached for the following call.Fields inherited from class RandomSource
_contextModifier and TypeFieldDescriptionprotected final I2PAppContextThe context this generator was created for.Constructor Summary
ConstructorsConstructorDescriptionFortunaRandomSource(I2PAppContext context) May block up to 10 seconds or forever.Method Summary
Modifier and TypeMethodDescriptionvoidfeedEntropy(String source, byte[] data, int offset, int len) Reseed the fortuna.voidfeedEntropy(String source, long data, int bitoffset, int bits) Reseed the fortuna.private longMix as much varying system state into a seed value as can be found.protected intnextBits(int numBits) Pull the next numBits of random data off the fortuna instance (returning 0 through 2^numBits-1booleanRandom boolean.bytenextByte()Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it.voidnextBytes(byte[] buf) Fill buffer with random bytes.voidnextBytes(byte[] buf, int offset, int length) Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it.doubleImplementation from sun's java.util.Random javadocs.floatImplementation from sun's java.util.Random javadocs.doubleImplementation from sun's java.util.Random javadocs.intnextInt()Non-negative random int.intnextInt(int n) According to the java docs (Random.nextInt(int)) nextInt(n) should return a number between 0 and n (including 0 and excluding n).longnextLong()All possible long values, positive and negative.longnextLong(long n) Like the modified nextInt, nextLong(n) returns a random number from 0 through n, including 0, excluding n.voidsetSeed(byte[] buf) Seed the Fortuna PRNG.voidshutdown()Note - methods may hang or NPE or throw IllegalStateExceptions after this.intNext random int, signed.Methods inherited from class RandomSource
getInstance, harvester, initSeed, loadSeed, saveSeed, writeSeedModifier and TypeMethodDescriptionstatic RandomSourceSingleton for whatever PRNG i2p uses.This random source as an entropy harvester.final booleaninitSeed(byte[] buf) May block up to 10 seconds.voidloadSeed()May block up to 10 seconds.voidsaveSeed()Save the current state as a new seed.static final voidwriteSeed(byte[] buf) Write the seed to the seed file.Methods inherited from class SecureRandom
generateSeed, getAlgorithm, getInstance, getInstance, getInstance, getInstance, getInstance, getInstance, getInstanceStrong, getParameters, getProvider, getSeed, next, nextBytes, reseed, reseed, setSeed, toStringMethods inherited from class Random
doubles, doubles, doubles, doubles, from, ints, ints, ints, ints, longs, longs, longs, longsMethods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.random.RandomGenerator
equiDoubles, isDeprecated, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextGaussian, nextInt, nextLong
Field Details
_fortuna
The Fortuna PRNG instance._nextGaussian
private double _nextGaussianNext Gaussian value, cached for the following call._haveNextGaussian
private boolean _haveNextGaussianWhether a cached Gaussian value is available.
Constructor Details
FortunaRandomSource
May block up to 10 seconds or forever.- Parameters:
context- the router context
Method Details
mixedSeed
private long mixedSeed()Mix as much varying system state into a seed value as can be found. Only used when every real entropy source has failed - it is not cryptographically strong on its own, but it beats a ~32 bit time seed.- Returns:
- a seed mixed from as much varying system state as could be found
shutdown
public void shutdown()Note - methods may hang or NPE or throw IllegalStateExceptions after this.setSeed
nextInt
public int nextInt(int n) According to the java docs (Random.nextInt(int)) nextInt(n) should return a number between 0 and n (including 0 and excluding n). However, their pseudocode, as well as sun's, kaffe's, and classpath's implementation INCLUDES NEGATIVE VALUES. Ok, so we're going to have it return between 0 and n (including 0, excluding n), since that's what it has been used for.Unlike Java's Random, which throws IAE, this returns 0 if n is 0.
- Specified by:
nextIntin interfacejava.util.random.RandomGenerator- Overrides:
nextIntin classRandomSource- Parameters:
n- non-negative- Throws:
IllegalArgumentException- if n is negative
nextInt
signedNextInt
public int signedNextInt()Next random int, signed.- Overrides:
signedNextIntin classRandomSource- Returns:
- all possible int values, positive and negative
nextLong
public long nextLong(long n) Like the modified nextInt, nextLong(n) returns a random number from 0 through n, including 0, excluding n.Unlike Java's Random, which throws IAE, this returns 0 if n is 0.
- Specified by:
nextLongin interfacejava.util.random.RandomGenerator- Overrides:
nextLongin classRandomSource- Parameters:
n- non-negative- Returns:
- a random long from 0 (inclusive) to n (exclusive)
- Throws:
IllegalArgumentException- if n is negative
nextLong
public long nextLong()All possible long values, positive and negative.- Specified by:
nextLongin interfacejava.util.random.RandomGenerator- Overrides:
nextLongin classSecureRandom
nextBoolean
public boolean nextBoolean()Random boolean.- Specified by:
nextBooleanin interfacejava.util.random.RandomGenerator- Overrides:
nextBooleanin classRandom
nextBytes
public void nextBytes(byte[] buf) Fill buffer with random bytes.- Specified by:
nextBytesin interfacejava.util.random.RandomGenerator- Overrides:
nextBytesin classSecureRandom
nextBytes
public void nextBytes(byte[] buf, int offset, int length) Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it.- Overrides:
nextBytesin classRandomSource- Parameters:
buf- the array to fill with random bytesoffset- where in the array to start writinglength- how many bytes to write at offset
nextByte
public byte nextByte()Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it.- Returns:
- a single random byte
nextDouble
public double nextDouble()Implementation from sun's java.util.Random javadocs.- Specified by:
nextDoublein interfacejava.util.random.RandomGenerator- Overrides:
nextDoublein classRandom
nextFloat
nextGaussian
public double nextGaussian()Implementation from sun's java.util.Random javadocs.- Specified by:
nextGaussianin interfacejava.util.random.RandomGenerator- Overrides:
nextGaussianin classRandom
nextBits
protected int nextBits(int numBits) Pull the next numBits of random data off the fortuna instance (returning 0 through 2^numBits-1Caller must synchronize!
- Parameters:
numBits- how many random bits to return, at least 1- Returns:
- a value in the range 0 through 2^numBits - 1
feedEntropy
Reseed the fortuna.- Specified by:
feedEntropyin interfaceEntropyHarvester- Overrides:
feedEntropyin classRandomSource- Parameters:
source- origin of the entropy, allowing the harvester to determine how much to value the datadata- value whose bits are to be harvestedbitoffset- bit index into the data array to start (using java standard big-endian)bits- how many bits to use
feedEntropy
Reseed the fortuna.- Specified by:
feedEntropyin interfaceEntropyHarvester- Overrides:
feedEntropyin classRandomSource- Parameters:
source- origin of the entropy, allowing the harvester to determine how much to value the datadata- array holding the entropy bytes to harvestoffset- index into the data array to startlen- how many bytes to use