Package net.i2p.util
Class FortunaRandomSource
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.StreamableGenerator
-
Field Summary
Fields inherited from class net.i2p.util.RandomSource
_context
-
Constructor Summary
ConstructorDescriptionFortunaRandomSource
(I2PAppContext context) May block up to 10 seconds or forever -
Method Summary
Modifier and TypeMethodDescriptionvoid
feedEntropy
(String source, byte[] data, int offset, int len) reseed the fortunavoid
feedEntropy
(String source, long data, int bitoffset, int bits) reseed the fortunastatic void
Outputs to stdout for dieharder:java -cp build/i2p.jar net.i2p.util.FortunaRandomSource | dieharder -a -g 200
protected int
nextBits
(int numBits) Pull the next numBits of random data off the fortuna instance (returning 0 through 2^numBits-1 Caller must synchronize!boolean
byte
nextByte()
Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it.void
nextBytes
(byte[] buf) void
nextBytes
(byte[] buf, int offset, int length) Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it.double
Implementation from sun's java.util.Random javadocsfloat
Implementation from sun's java.util.Random javadocsdouble
Implementation from sun's java.util.Random javadocsint
nextInt()
int
nextInt
(int n) According to the java docs (http://java.sun.com/j2se/1.4.1/docs/api/java/util/Random.html#nextInt(int)) nextInt(n) should return a number between 0 and n (including 0 and excluding n).long
nextLong()
long
nextLong
(long n) Like the modified nextInt, nextLong(n) returns a random number from 0 through n, including 0, excluding n.void
setSeed
(byte[] buf) void
shutdown()
Note - methods may hang or NPE or throw IllegalStateExceptions after thisint
This code unused, see FortunaRandomSource overrideMethods inherited from class net.i2p.util.RandomSource
getInstance, harvester, initSeed, loadSeed, saveSeed, writeSeed
Methods inherited from class java.security.SecureRandom
generateSeed, getAlgorithm, getInstance, getInstance, getInstance, getInstance, getInstance, getInstance, getInstanceStrong, getParameters, getProvider, getSeed, next, nextBytes, reseed, reseed, setSeed, toString
Methods inherited from class java.util.Random
doubles, doubles, doubles, doubles, from, ints, ints, ints, ints, longs, longs, longs, longs
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.random.RandomGenerator
isDeprecated, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextGaussian, nextInt, nextLong
-
Constructor Details
-
FortunaRandomSource
May block up to 10 seconds or forever
-
-
Method Details
-
shutdown
public void shutdown()Note - methods may hang or NPE or throw IllegalStateExceptions after this- Since:
- 0.8.8
-
setSeed
public void setSeed(byte[] buf) - Overrides:
setSeed
in classSecureRandom
-
nextInt
public int nextInt(int n) According to the java docs (http://java.sun.com/j2se/1.4.1/docs/api/java/util/Random.html#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 thats what it has been used for. Unlike Java's Random, which throws IAE, this returns 0 if n is 0.- Specified by:
nextInt
in interfacejava.util.random.RandomGenerator
- Overrides:
nextInt
in classRandomSource
- Parameters:
n
- non-negative- Throws:
IllegalArgumentException
- if n is negative
-
nextInt
public int nextInt() -
signedNextInt
public int signedNextInt()Description copied from class:RandomSource
This code unused, see FortunaRandomSource override- Overrides:
signedNextInt
in classRandomSource
- Returns:
- all possible int values, positive and negative
- Since:
- 0.9.54
-
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:
nextLong
in interfacejava.util.random.RandomGenerator
- Overrides:
nextLong
in classRandomSource
- Parameters:
n
- non-negative- Throws:
IllegalArgumentException
- if n is negative
-
nextLong
public long nextLong() -
nextBoolean
public boolean nextBoolean()- Specified by:
nextBoolean
in interfacejava.util.random.RandomGenerator
- Overrides:
nextBoolean
in classRandom
-
nextBytes
public void nextBytes(byte[] buf) - Specified by:
nextBytes
in interfacejava.util.random.RandomGenerator
- Overrides:
nextBytes
in 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:
nextBytes
in classRandomSource
- Since:
- 0.8.12
-
nextByte
public byte nextByte()Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it.- Since:
- 0.9.24
-
nextDouble
public double nextDouble()Implementation from sun's java.util.Random javadocs- Specified by:
nextDouble
in interfacejava.util.random.RandomGenerator
- Overrides:
nextDouble
in classRandom
-
nextFloat
public float nextFloat()Implementation from sun's java.util.Random javadocs -
nextGaussian
public double nextGaussian()Implementation from sun's java.util.Random javadocs- Specified by:
nextGaussian
in interfacejava.util.random.RandomGenerator
- Overrides:
nextGaussian
in classRandom
-
nextBits
protected int nextBits(int numBits) Pull the next numBits of random data off the fortuna instance (returning 0 through 2^numBits-1 Caller must synchronize! -
feedEntropy
reseed the fortuna- Specified by:
feedEntropy
in interfaceEntropyHarvester
- Overrides:
feedEntropy
in classRandomSource
- Parameters:
source
- origin of the entropy, allowing the harvester to determine how much to value the databitoffset
- 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:
feedEntropy
in interfaceEntropyHarvester
- Overrides:
feedEntropy
in classRandomSource
- Parameters:
source
- origin of the entropy, allowing the harvester to determine how much to value the dataoffset
- index into the data array to startlen
- how many bytes to use
-
main
Outputs to stdout for dieharder:java -cp build/i2p.jar net.i2p.util.FortunaRandomSource | dieharder -a -g 200
-