It obtains seed and random numbers by reading system files such as
the special device files /dev/random and /dev/urandom. This
implementation respects the securerandom.source Security
property and java.security.egd System property for obtaining
seed material. If the file specified by the properties does not
exist, /dev/random is the default seed source. /dev/urandom is
the default source of random numbers.
On some Unix platforms, /dev/random may block until enough entropy is available, but that may negatively impact the perceived startup time. By selecting these sources, this implementation tries to strike a balance between performance and security.
generateSeed() and setSeed() attempt to directly read/write to the seed source. However, this file may only be writable by root in many configurations. Because we cannot just ignore bytes specified via setSeed(), we keep a SHA1PRNG around in parallel.
nextBytes() reads the bytes directly from the source of random numbers (and then mixes them with bytes from the SHA1PRNG for the reasons explained above). Reading bytes from the random generator means that we are generally getting entropy from the operating system. This is a notable advantage over the SHA1PRNG model, which acquires entropy only initially during startup although the VM may be running for months.
Also note for nextBytes() that we do not need any initial pure random seed from /dev/random. This is an advantage because on some versions of Linux entropy can be exhausted very quickly and could thus impact startup time.
Finally, note that we use a singleton for the actual work (RandomIO) to avoid having to open and close /dev/[u]random constantly. However, there may be many NativePRNG instances created by the JCA framework.
| Modifier and Type | Class and Description |
|---|---|
| public static class | NativePRNG.
A NativePRNG-like class that uses /dev/random for both seed and random material. |
| public static class | NativePRNG.
A NativePRNG-like class that uses /dev/urandom for both seed and random material. |
| private static class | NativePRNG.
Nested class doing the actual work. |
| private static enum |
| Modifier and Type | Field and Description |
|---|---|
| private static final Debug | |
| private static final NativePRNG. | |
| private static final String | |
| private static final String | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected byte[] | engineGenerateSeed(int numBytes)
Implements abstract java. Returns the given number of seed bytes. |
| protected void | engineNextBytes(byte[] bytes)
Implements abstract java. Generates a user-specified number of random bytes. |
| protected void | engineSetSeed(byte[] seed)
Implements abstract java. Reseeds this random object with the given seed. |
| private static URL | |
| private static NativePRNG. | |
| pack-priv static boolean |
| debug | back to summary |
|---|---|
| private static final Debug debug | |
| INSTANCE | back to summary |
|---|---|
| private static final NativePRNG. | |
| NAME_RANDOM | back to summary |
|---|---|
| private static final String NAME_RANDOM | |
| NAME_URANDOM | back to summary |
|---|---|
| private static final String NAME_URANDOM | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| NativePRNG | back to summary |
|---|---|
| public NativePRNG(SecureRandomParameters params) | |
| engineGenerateSeed | back to summary |
|---|---|
| protected byte[] engineGenerateSeed(int numBytes) Implements abstract java. Doc from java. Returns the given number of seed bytes. This call may be used to seed other random number generators.
| |
| engineNextBytes | back to summary |
|---|---|
| protected void engineNextBytes(byte[] bytes) Implements abstract java. Doc from java. Generates a user-specified number of random bytes.
Some random number generators can only generate a limited amount
of random bytes per invocation. If the size of
| |
| engineSetSeed | back to summary |
|---|---|
| protected void engineSetSeed(byte[] seed) Implements abstract java. Doc from java. Reseeds this random object with the given seed. The seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.
| |
| getEgdUrl | back to summary |
|---|---|
| private static URL getEgdUrl() Get the System egd source (if defined). We only allow "file:" URLs for now. If there is a egd value, parse it.
| |
| initIO | back to summary |
|---|---|
| private static NativePRNG. Create a RandomIO object for all I/O of this Variant type. | |
| isAvailable | back to summary |
|---|---|
| pack-priv static boolean isAvailable() | |
| Modifier and Type | Field and Description |
|---|---|
| private static final NativePRNG. | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected byte[] | engineGenerateSeed(int numBytes)
Implements abstract java. Returns the given number of seed bytes. |
| protected void | engineNextBytes(byte[] bytes)
Implements abstract java. Generates a user-specified number of random bytes. |
| protected void | engineSetSeed(byte[] seed)
Implements abstract java. Reseeds this random object with the given seed. |
| pack-priv static boolean |
| INSTANCE | back to summary |
|---|---|
| private static final NativePRNG. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| Blocking | back to summary |
|---|---|
| public Blocking(SecureRandomParameters params) | |
| engineGenerateSeed | back to summary |
|---|---|
| protected byte[] engineGenerateSeed(int numBytes) Implements abstract java. Doc from java. Returns the given number of seed bytes. This call may be used to seed other random number generators.
| |
| engineNextBytes | back to summary |
|---|---|
| protected void engineNextBytes(byte[] bytes) Implements abstract java. Doc from java. Generates a user-specified number of random bytes.
Some random number generators can only generate a limited amount
of random bytes per invocation. If the size of
| |
| engineSetSeed | back to summary |
|---|---|
| protected void engineSetSeed(byte[] seed) Implements abstract java. Doc from java. Reseeds this random object with the given seed. The seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.
| |
| isAvailable | back to summary |
|---|---|
| pack-priv static boolean isAvailable() | |
| Modifier and Type | Field and Description |
|---|---|
| private static final NativePRNG. | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected byte[] | engineGenerateSeed(int numBytes)
Implements abstract java. Returns the given number of seed bytes. |
| protected void | engineNextBytes(byte[] bytes)
Implements abstract java. Generates a user-specified number of random bytes. |
| protected void | engineSetSeed(byte[] seed)
Implements abstract java. Reseeds this random object with the given seed. |
| pack-priv static boolean |
| INSTANCE | back to summary |
|---|---|
| private static final NativePRNG. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| NonBlocking | back to summary |
|---|---|
| public NonBlocking(SecureRandomParameters params) | |
| engineGenerateSeed | back to summary |
|---|---|
| protected byte[] engineGenerateSeed(int numBytes) Implements abstract java. Doc from java. Returns the given number of seed bytes. This call may be used to seed other random number generators.
| |
| engineNextBytes | back to summary |
|---|---|
| protected void engineNextBytes(byte[] bytes) Implements abstract java. Doc from java. Generates a user-specified number of random bytes.
Some random number generators can only generate a limited amount
of random bytes per invocation. If the size of
| |
| engineSetSeed | back to summary |
|---|---|
| protected void engineSetSeed(byte[] seed) Implements abstract java. Doc from java. Reseeds this random object with the given seed. The seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.
| |
| isAvailable | back to summary |
|---|---|
| pack-priv static boolean isAvailable() | |
| Modifier and Type | Field and Description |
|---|---|
| private int | |
| private int | |
| private int | |
| private long | |
| private final Object | |
| private final Object | |
| private final Object | |
| private static final int | |
| private static final long | |
| private static final int | |
| private volatile SecureRandom | |
| private byte[] | |
| private final InputStream | |
| private static final int | |
| private static final int | |
| pack-priv File | |
| private final InputStream | |
| private OutputStream | |
| private boolean |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| private SecureRandom | |
| private byte[] | |
| private void | |
| private void | |
| private static void |
| buffered | back to summary |
|---|---|
| private int buffered | |
| bufferSize | back to summary |
|---|---|
| private int bufferSize | |
| change_buffer | back to summary |
|---|---|
| private int change_buffer | |
| lastRead | back to summary |
|---|---|
| private long lastRead | |
| LOCK_GET_BYTES | back to summary |
|---|---|
| private final Object LOCK_GET_BYTES | |
| LOCK_GET_SEED | back to summary |
|---|---|
| private final Object LOCK_GET_SEED | |
| LOCK_SET_SEED | back to summary |
|---|---|
| private final Object LOCK_SET_SEED | |
| MAX_BUFFER_SIZE | back to summary |
|---|---|
| private static final int MAX_BUFFER_SIZE | |
| MAX_BUFFER_TIME | back to summary |
|---|---|
| private static final long MAX_BUFFER_TIME | |
| MIN_BUFFER_SIZE | back to summary |
|---|---|
| private static final int MIN_BUFFER_SIZE | |
| mixRandom | back to summary |
|---|---|
| private volatile SecureRandom mixRandom | |
| nextBuffer | back to summary |
|---|---|
| private byte[] nextBuffer | |
| nextIn | back to summary |
|---|---|
| private final InputStream nextIn | |
| REQ_LIMIT_DEC | back to summary |
|---|---|
| private static final int REQ_LIMIT_DEC | |
| REQ_LIMIT_INC | back to summary |
|---|---|
| private static final int REQ_LIMIT_INC | |
| seedFile | back to summary |
|---|---|
| pack-priv File seedFile | |
| seedIn | back to summary |
|---|---|
| private final InputStream seedIn | |
| seedOut | back to summary |
|---|---|
| private OutputStream seedOut | |
| seedOutInitialized | back to summary |
|---|---|
| private boolean seedOutInitialized | |
| RandomIO | back to summary |
|---|---|
| private RandomIO(File seedFile, File nextFile) throws IOException | |
| ensureBufferValid | back to summary |
|---|---|
| private void ensureBufferValid() throws IOException | |
| getMixRandom | back to summary |
|---|---|
| private SecureRandom getMixRandom() | |
| implGenerateSeed | back to summary |
|---|---|
| private byte[] implGenerateSeed(int numBytes) | |
| implNextBytes | back to summary |
|---|---|
| private void implNextBytes(byte[] data) | |
| implSetSeed | back to summary |
|---|---|
| private void implSetSeed(byte[] seed) | |
| readFully | back to summary |
|---|---|
| private static void readFully(InputStream in, byte[] data) throws IOException | |
| Modifier and Type | Field and Description |
|---|---|
| public static final NativePRNG. | |
| public static final NativePRNG. | |
| public static final NativePRNG. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static NativePRNG. | |
| public static NativePRNG. |
| BLOCKING | back to summary |
|---|---|
| public static final NativePRNG. | |
| MIXED | back to summary |
|---|---|
| public static final NativePRNG. | |
| NONBLOCKING | back to summary |
|---|---|
| public static final NativePRNG. | |
| Variant | back to summary |
|---|---|
| private Variant() | |
| valueOf | back to summary |
|---|---|
| public static NativePRNG. | |
| values | back to summary |
|---|---|
| public static NativePRNG. | |