Class SecureRandomSeedGenerator
- java.lang.Object
-
- io.github.pr0methean.betterrandom.seed.SecureRandomSeedGenerator
-
- All Implemented Interfaces:
SeedGenerator,Serializable
public class SecureRandomSeedGenerator extends Object implements SeedGenerator, Serializable
SeedGeneratorimplementation that uses Java's bundledSecureRandomRNG 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
SeedGeneratorimplementations be usable.On Oracle and OpenJDK, SecureRandom uses
sun.security.provider.SeedGenerator; when/dev/randomisn'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 Summary
Fields Modifier and Type Field Description static SecureRandomSeedGeneratorDEFAULT_INSTANCEThe default instance.-
Fields inherited from interface io.github.pr0methean.betterrandom.seed.SeedGenerator
EMPTY_SEED
-
-
Constructor Summary
Constructors Constructor Description SecureRandomSeedGenerator(SecureRandom source)Creates an instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object o)voidgenerateSeed(byte[] output)Generates a seed value for a random number generator in an existing array.byte[]generateSeed(int length)Generates and returns a seed value for a random number generator as a new array.inthashCode()protected ObjectreadResolve()Ensures that all serialized copies ofDEFAULT_INSTANCEdeserialize as the same object.StringtoString()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.github.pr0methean.betterrandom.seed.SeedGenerator
isWorthTrying
-
-
-
-
Field Detail
-
DEFAULT_INSTANCE
public static final SecureRandomSeedGenerator DEFAULT_INSTANCE
The default instance. (This class was formerly a singleton.)
-
-
Constructor Detail
-
SecureRandomSeedGenerator
public SecureRandomSeedGenerator(SecureRandom source)
Creates an instance.- Parameters:
source- theSecureRandomwhoseSecureRandom.generateSeed(int)method will provide seeds
-
-
Method Detail
-
readResolve
protected Object readResolve()
Ensures that all serialized copies ofDEFAULT_INSTANCEdeserialize as the same object.- Returns:
DEFAULT_INSTANCEif this object wasDEFAULT_INSTANCEwhen it was serialized; this object unchanged otherwise
-
generateSeed
public byte[] generateSeed(int length)
Description copied from interface:SeedGeneratorGenerates and returns a seed value for a random number generator as a new array.- Specified by:
generateSeedin interfaceSeedGenerator- Parameters:
length- The length of the seed to generate (in bytes).- Returns:
- A byte array containing the seed data.
-
generateSeed
public void generateSeed(byte[] output)
Description copied from interface:SeedGeneratorGenerates a seed value for a random number generator in an existing array.- Specified by:
generateSeedin interfaceSeedGenerator- Parameters:
output- The array that is to be populated with the seed.
-
-