Class SecureRandomSeedGenerator

  • All Implemented Interfaces:
    SeedGenerator, Serializable

    public class SecureRandomSeedGenerator
    extends Object
    implements SeedGenerator, Serializable

    SeedGenerator implementation that uses Java's bundled SecureRandom RNG to generate random seed data.

    The advantage of using SecureRandom for seeding but not as the primary RNG is that we can use it to seed RNGs that are much faster than SecureRandom.

    This is the only seeding strategy that is guaranteed to work on all platforms and therefore is provided as a fall-back option should none of the other provided SeedGenerator implementations be usable.

    On Oracle and OpenJDK, SecureRandom uses sun.security.provider.SeedGenerator; when /dev/random isn't available, that SeedGenerator class in turn uses the timing of newly-launched threads as a source of randomness, relying on the unpredictable interactions between different configurations of hardware and software and their workloads.

    Author:
    Daniel Dyer
    See Also:
    Serialized Form
    • Field Detail

      • DEFAULT_INSTANCE

        public static final SecureRandomSeedGenerator DEFAULT_INSTANCE
        The default instance. (This class was formerly a singleton.)
    • Method Detail

      • readResolve

        protected Object readResolve()
      • generateSeed

        public byte[] generateSeed​(int length)
        Generates and returns a seed value for a random number generator as a new array.
        Specified by:
        generateSeed in interface SeedGenerator
        Parameters:
        length - The length of the seed to generate (in bytes).
        Returns:
        A byte array containing the seed data.
        Throws:
        SeedException - If a seed cannot be generated for any reason.
      • generateSeed

        public void generateSeed​(byte[] output)
        Description copied from interface: SeedGenerator
        Generates a seed value for a random number generator in an existing array.
        Specified by:
        generateSeed in interface SeedGenerator
        Parameters:
        output - The array that is to be populated with the seed.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • isWorthTrying

        public boolean isWorthTrying()
        Returns true if we cannot determine quickly (i.e. without I/O calls) that this SeedGenerator would throw a SeedException if generateSeed(int) or generateSeed(byte[]) were being called right now.
        Specified by:
        isWorthTrying in interface SeedGenerator
        Returns:
        true if this SeedGenerator will get as far as an I/O call or other slow operation in attempting to generate a seed immediately.