Class EntropyBlockingRandomWrapper<T extends Random>

    • Constructor Detail

      • EntropyBlockingRandomWrapper

        public EntropyBlockingRandomWrapper​(T wrapped,
                                            long minimumEntropy,
                                            @Nullable
                                            SeedGenerator sameThreadSeedGen)
        Parameters:
        wrapped - the Random to wrap
        minimumEntropy - the minimum entropy, below which requests for pseudorandom numbers will block until the PRNG is reseeded
        sameThreadSeedGen - a SeedGenerator, if any, to use on the calling thread if this PRNG does not have a running RandomSeeder
    • Method Detail

      • wrapJavaUtilRandom

        public static EntropyBlockingRandomWrapper<Random> wrapJavaUtilRandom​(long minimumEntropy,
                                                                              byte[] seed,
                                                                              @Nullable
                                                                              SeedGenerator sameThreadSeedGen)
        Creates an instance wrapping a basic Random.
        Parameters:
        seed - the seed
        minimumEntropy - the minimum entropy, below which requests for pseudorandom numbers will block until the PRNG is reseeded
        sameThreadSeedGen - a SeedGenerator, if any, to use on the calling thread if this PRNG does not have a running RandomSeeder
        Returns:
        an instance
      • setWrapped

        public void setWrapped​(T wrapped)
        Description copied from class: RandomWrapper
        Replaces the wrapped PRNG with the given one on subsequent calls.
        Overrides:
        setWrapped in class RandomWrapper<T extends Random>
        Parameters:
        wrapped - an Random instance to wrap
      • nextBytes

        public void nextBytes​(byte[] bytes)
        Description copied from class: BaseRandom
        Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array. Reimplemented for entropy-counting purposes.
        Overrides:
        nextBytes in class RandomWrapper<T extends Random>
      • getSameThreadSeedGen

        @Nullable
        public SeedGenerator getSameThreadSeedGen()
        Returns the seed generator that is used on the calling thread if not registered with a running RandomSeeder.
        Returns:
        the seed generator
      • setSameThreadSeedGen

        public void setSameThreadSeedGen​(@Nullable
                                         SeedGenerator newSeedGen)
        Sets the seed generator that is used on the calling thread if not registered with a running RandomSeeder.
        Parameters:
        newSeedGen - the new seed generator
      • debitEntropy

        protected void debitEntropy​(long bits)
        Description copied from class: BaseRandom
        Record that entropy has been spent, and schedule a reseeding if this PRNG has now spent as much as it's been seeded with.
        Overrides:
        debitEntropy in class BaseRandom
        Parameters:
        bits - The number of bits of entropy spent.
      • setSeed

        public void setSeed​(long seed)
        Description copied from class: BaseRandom
        Sets the seed of this random number generator using a single long seed, if this implementation supports that. If it is capable of using 64 bits or less of seed data (i.e. if {@link #getNewSeedLength()} <= {@link Long#BYTES}), then this method shall replace the entire seed as Random.setSeed(long) does; otherwise, it shall either combine the input with the existing seed as SecureRandom.setSeed(long) does, or it shall generate a new seed using the DefaultSeedGenerator. The latter is a backward-compatibility measure and can be very slow.
        Overrides:
        setSeed in class RandomWrapper<T extends Random>
        See Also:
        ByteArrayReseedableRandom.preferSeedWithLong()
      • needsReseedingEarly

        public boolean needsReseedingEarly()
        Description copied from interface: EntropyCountingRandom
        If true, this PRNG needs reseeding even though its entropy is positive. Added to deal with EntropyBlockingRandomWrapper.
        Returns:
        true if this PRNG needs reseeding regardless of entropy count; false otherwise
      • checkMaxOutputAtOnce

        protected void checkMaxOutputAtOnce()
        Ensures that the attached PRNG can output 64 bits between reseedings, given that its current entropy is the maximum possible. Methods such as Random.nextLong() and Random.nextDouble() would become much slower and more complex if we didn't require this.
        Throws:
        IllegalArgumentException - if the PRNG cannot output 64 bits between reseedings
      • onSeedingStateChanged

        protected void onSeedingStateChanged​(boolean reseeded)
        Called when a new seed generator or RandomSeeder is attached or a new seed is generated, so that operations can unblock.
        Parameters:
        reseeded - true if the seed has changed; false otherwise