java.lang.Object
java.util.Random
java.security.SecureRandom
net.i2p.util.RandomSource
- All Implemented Interfaces:
Serializable, java.util.random.RandomGenerator, EntropyHarvester
- Direct Known Subclasses:
FortunaRandomSource
Singleton for whatever PRNG i2p uses.
- 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 TypeFieldDescriptionprotected final I2PAppContextThe context this generator was created for.Constructor Summary
ConstructorsConstructorDescriptionRandomSource(I2PAppContext context) Deprecated - do not instantiate this directly, as you won't get the good one (Fortuna).Method Summary
Modifier and TypeMethodDescriptionvoidfeedEntropy(String source, byte[] data, int offset, int len) Feed the given entropy into the generator.voidfeedEntropy(String source, long data, int bitoffset, int bits) Feed the given entropy into the generator.static 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.voidnextBytes(byte[] buf, int offset, int length) Not part of java.util.SecureRandom, but added since Fortuna supports it.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(long n) Like the modified nextInt, nextLong(n) returns a random number from 0 through n, including 0, excluding n.voidsaveSeed()Save the current state as a new seed.intThis code unused, see FortunaRandomSource override.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, nextBytes, nextLong, reseed, reseed, setSeed, setSeed, toStringMethods inherited from class Random
doubles, doubles, doubles, doubles, from, ints, ints, ints, ints, longs, longs, longs, longs, nextBoolean, nextDouble, nextFloat, nextGaussian, nextIntMethods 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
_context
The context this generator was created for.
Constructor Details
RandomSource
Deprecated - do not instantiate this directly, as you won't get the good one (Fortuna). UsegetInstance()orI2PAppContext.getGlobalContext()random()to get the FortunaRandomSource instance.- Parameters:
context- the application context this source is bound to
Method Details
getInstance
Singleton for whatever PRNG i2p uses. Same asI2PAppContext.getGlobalContext()random(); use context.random() if you have a context already.- Returns:
I2PAppContext.getGlobalContext()random()
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 thats what it has been used for.This code unused, see
FortunaRandomSourceoverride.signedNextInt
public int signedNextInt()This code unused, see FortunaRandomSource override.- 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.This code unused, see FortunaRandomSource override NOPMD - nextLong(long) doesn't override SecureRandom (added in Java 17).
- Specified by:
nextLongin interfacejava.util.random.RandomGenerator
nextBytes
public void nextBytes(byte[] buf, int offset, int length) Not part of java.util.SecureRandom, but added since Fortuna supports it.This code unused, see FortunaRandomSource override.
- Parameters:
buf- the array to fill with random bytesoffset- where in the array to start writinglength- how many bytes to write at offset
harvester
This random source as an entropy harvester.- Returns:
- this source, since a PRNG is its own harvester
feedEntropy
Feed the given entropy into the generator.- Specified by:
feedEntropyin interfaceEntropyHarvester- 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
Feed the given entropy into the generator.- Specified by:
feedEntropyin interfaceEntropyHarvester- 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
loadSeed
public void loadSeed()May block up to 10 seconds.saveSeed
public void saveSeed()Save the current state as a new seed.writeSeed
public static final void writeSeed(byte[] buf) Write the seed to the seed file.- Parameters:
buf- the seed bytes to store; a failure to write is ignored
initSeed
public final boolean initSeed(byte[] buf) May block up to 10 seconds.- Parameters:
buf- the buffer to receive the merged seed; its size bounds the amount read- Returns:
- true if any entropy source contributed, false if the buffer is still all zeros