Enum RandomDotOrgSeedGenerator
- java.lang.Object
-
- java.lang.Enum<RandomDotOrgSeedGenerator>
-
- io.github.pr0methean.betterrandom.seed.RandomDotOrgSeedGenerator
-
- All Implemented Interfaces:
SeedGenerator
,Serializable
,Comparable<RandomDotOrgSeedGenerator>
public enum RandomDotOrgSeedGenerator extends Enum<RandomDotOrgSeedGenerator> implements SeedGenerator
Connects to random.org's old API (via HTTPS) and downloads a set of random bits to use as seed data. It is generally better to use the
DevRandomSeedGenerator
where possible, as it should be much quicker. This seed generator is most useful on Microsoft Windows without Cygwin, and other platforms that do not provide /dev/random.Random.org collects randomness from atmospheric noise using 9 radios, located at undisclosed addresses in Dublin and Copenhagen and tuned to undisclosed AM/FM frequencies. (The secrecy is intended to help prevent tampering with the output using a well-placed radio transmitter, and the use of AM/FM helps ensure that any such tampering would cause illegal interference with broadcasts and quickly attract regulatory attention.)
Random.org has two APIs: an old API and a newer JSON-RPC API. Since the new one requires a key obtained from random.org, the old one is used by default. However, if you have a key, you can provide it by calling
setApiKey(UUID)
, and the new API will then be used.Note that when using the old API, random.org limits the supply of free random numbers to any one IP address; if you operate from a fixed address (at least if you use IPv4), you can check your quota and buy more. On the new API, the quota is per key rather than per IP, and commercial-use pricing follows a different scheme.
- Author:
- Daniel Dyer (old API), Chris Hennick (new API; refactoring)
-
-
Enum Constant Summary
Enum Constants Enum Constant Description DELAYED_RETRY
Upon a failed request, this version of the client waits 10 seconds before trying again.RANDOM_DOT_ORG_SEED_GENERATOR
This version of the client may make HTTP requests as fast as your computer is capable of sending them.
-
Field Summary
-
Fields inherited from interface io.github.pr0methean.betterrandom.seed.SeedGenerator
EMPTY_SEED
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
generateSeed(byte[] seed)
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.boolean
isWorthTrying()
Returns true if we cannot determine quickly (i.e.static void
setApiKey(UUID apiKey)
Sets the API key.static void
setProxy(Proxy proxy)
Sets the proxy to use to connect to random.org.static void
setSslSocketFactory(SSLSocketFactory socketFactory)
Sets the socket factory to use to connect to random.org.String
toString()
Returns "https://www.random.org (with retry delay)" or "https://www.random.org (without retry delay)".static RandomDotOrgSeedGenerator
valueOf(String name)
Returns the enum constant of this type with the specified name.static RandomDotOrgSeedGenerator[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
RANDOM_DOT_ORG_SEED_GENERATOR
public static final RandomDotOrgSeedGenerator RANDOM_DOT_ORG_SEED_GENERATOR
This version of the client may make HTTP requests as fast as your computer is capable of sending them. Since it is inherently spammy, it is recommended only when you know your usage is light and/or no other source of randomness will do.
-
DELAYED_RETRY
public static final RandomDotOrgSeedGenerator DELAYED_RETRY
Upon a failed request, this version of the client waits 10 seconds before trying again. If called again during that waiting period, throwsSeedException
. TheDefaultSeedGenerator
uses this version.
-
-
Method Detail
-
values
public static RandomDotOrgSeedGenerator[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (RandomDotOrgSeedGenerator c : RandomDotOrgSeedGenerator.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static RandomDotOrgSeedGenerator valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
setApiKey
public static void setApiKey(@Nullable UUID apiKey)
Sets the API key. If not null, random.org's JSON API is used. Otherwise, the old API is used.- Parameters:
apiKey
- An API key obtained from random.org.
-
setProxy
public static void setProxy(@Nullable Proxy proxy)
Sets the proxy to use to connect to random.org. If null, the JVM default is used.- Parameters:
proxy
- a proxy, or null for the JVM default
-
setSslSocketFactory
public static void setSslSocketFactory(@Nullable SSLSocketFactory socketFactory)
Sets the socket factory to use to connect to random.org. If null, the JVM default is used. This method provides flexibility in how the user protects against downgrade attacks such as POODLE and weak cipher suites, even if the random.org connection needs separate handling from connections to other services by the same application.- Parameters:
socketFactory
- a socket factory, or null for the JVM default
-
generateSeed
public void generateSeed(byte[] seed) throws SeedException
Description copied from interface:SeedGenerator
Generates a seed value for a random number generator in an existing array.- Specified by:
generateSeed
in interfaceSeedGenerator
- Parameters:
seed
- The array that is to be populated with the seed.- Throws:
SeedException
- If a seed cannot be generated for any reason.
-
isWorthTrying
public boolean isWorthTrying()
Description copied from interface:SeedGenerator
Returns true if we cannot determine quickly (i.e. without I/O calls) that this SeedGenerator would throw aSeedException
ifSeedGenerator.generateSeed(int)
orSeedGenerator.generateSeed(byte[])
were being called right now.- Specified by:
isWorthTrying
in interfaceSeedGenerator
- 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.
-
toString
public String toString()
Returns "https://www.random.org (with retry delay)" or "https://www.random.org (without retry delay)".- Overrides:
toString
in classEnum<RandomDotOrgSeedGenerator>
-
generateSeed
public byte[] generateSeed(int length) throws SeedException
Description copied from interface:SeedGenerator
Generates and returns a seed value for a random number generator as a new array.- Specified by:
generateSeed
in interfaceSeedGenerator
- 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.
-
-