The sun.security.provider.SecureRandom implementation contains a static SecureRandom instance that is used to seed SHA1PRNG instances generated for callers. The static instance is seeded using sun.security.provider.SeedGenerator. Depending on the Java Virtual Machine’s configuration, the actual seeding mechanism may vary. The various mechanisms are described in the subsections below.
Regardless of the configuration, the SeedGenerator.getSystemEntropy() method is always used as part of the seed. This method outputs a SHA-1 hash of the following:
- One byte from System.currentTimeMillis();
- All system properties (name/value pairs)
- List of files/directories in java.io.tmpdir
- Exception.hashCode (if an Exception occurs while gathering any of the above values; if an Exception does occur, some of the above values may not be included in the hash)
The following values are used to determine where the rest of the seed entropy is obtained:
- The java.security.egd system property
- The securerandom.source property in the java.security file
The java.security.egd system property takes precedence. By default, java.security.egd is not defined, and securerandom.source is set to file:/dev/urandom. The value set by these properties will be referred to as egdSource below. The seeding mechanisms are chosen as follows:
- If egdSource is set to file:/dev/urandom or file:/dev/random, then sun.security.provider.NativeSeedGenerator is used.
- If egdSource is set to some other value, then the given file/URL is used with sun.security.provider.SeedGenerator.URLSeedGenerator.
- If egdSource is not specified or if the mechanism chosen above fails, then sun.security.provider.SeedGenerator.ThreadedSeedGenerator is used.
The *nix implementation uses sun.security.provider.SeedGenerator.URLSeedGenerator with /dev/random. This results in the seed being read from /dev/random. Note that since reads from /dev/random can block if insufficient entropy is available, the seed generation operation on *nix could stop responding.
The Windows implementation uses the Windows CryptGenRandom() function to generate the seed.
This implementation reads from the given URL (which could be a local file) and returns the first bytes at the URL as the seed.
This implementation spawns a thread that runs for approximately 250ms, and counts the number of times a loop executes while the thread is running. This results in one byte of unpredictable output being generated. This seed generator is quite slow and generates at most 4 bytes per second. Seeding a SHA1PRNG instance using this mechanism takes at least 5 seconds.