All Implemented Interfaces:
Serializable, java.util.random.RandomGenerator, EntropyHarvester
Direct Known Subclasses:
FortunaRandomSource

public class RandomSourceextends SecureRandom implements EntropyHarvester
Singleton for whatever PRNG i2p uses.
See Also:
  • Field Details

    • _context

      protected final transient I2PAppContext _context
      The context this generator was created for.
  • Constructor Details

    • RandomSource

      public RandomSource(I2PAppContext context)
      Deprecated - do not instantiate this directly, as you won't get the good one (Fortuna). Use getInstance() or I2PAppContext.getGlobalContext()random() to get the FortunaRandomSource instance.
      Parameters:
      context - the application context this source is bound to
  • Method Details

    • getInstance

      public static RandomSource getInstance()
      Singleton for whatever PRNG i2p uses. Same as I2PAppContext.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 FortunaRandomSource override.

      Specified by:
      nextInt in interface java.util.random.RandomGenerator
      Overrides:
      nextInt in class Random
    • 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:
      nextLong in interface java.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 bytes
      offset - where in the array to start writing
      length - how many bytes to write at offset
    • harvester

      public EntropyHarvester harvester()
      This random source as an entropy harvester.
      Returns:
      this source, since a PRNG is its own harvester
    • feedEntropy

      public void feedEntropy(String source, long data, int bitoffset, int bits)
      Feed the given entropy into the generator.
      Specified by:
      feedEntropy in interface EntropyHarvester
      Parameters:
      source - origin of the entropy, allowing the harvester to determine how much to value the data
      data - value whose bits are to be harvested
      bitoffset - bit index into the data array to start (using java standard big-endian)
      bits - how many bits to use
    • feedEntropy

      public void feedEntropy(String source, byte[] data, int offset, int len)
      Feed the given entropy into the generator.
      Specified by:
      feedEntropy in interface EntropyHarvester
      Parameters:
      source - origin of the entropy, allowing the harvester to determine how much to value the data
      data - array holding the entropy bytes to harvest
      offset - index into the data array to start
      len - 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