Take the 2-minute tour ×

I have a basic question. Why 'SHA1PRNG' is used in SecureRandom Class. It will be helpful if someone explains about it. Thanks in advance.

EX: SecureRandom.getInstance("SHA1PRNG");

share|improve this question
Interesting discussion on the implementation of SHA1PRNG here. –  Maarten Bodewes - owlstead Apr 1 at 11:45

2 Answers 2

See the IBM Docs on the subject. It is just ensuring the random number generated is as close to "truly random" as possible. Easily guessable random numbers break encryption.

share|improve this answer
Also interesting is what constitutes 'good' random numbers. Read up on it, but your head will explode. –  Tony Ennis Oct 4 '12 at 11:45
@TonyEnnis If that doesn't explode your head you can try and understand the NIST test cases :) –  Maarten Bodewes - owlstead Oct 6 '12 at 21:22
link is broken... –  assylias Aug 27 '13 at 16:17

SHA1PRNG is a pseudo random number generator (the PRNG in the name). That means that it uses the SHA1 hash function to generate a stream of random numbers.

The SHA1 hash function is to create the output of the RNG and to hash the seed information before it is used in the PRNG. The SHA1PRNG output is decoupled from the internal state (so an attacker cannot recreate the internal state using just the output of the RNG).

The internal state is relatively large (currently limited to 160 bits, the hash size, for SHA1PRNG in Java 1.7). That means that it is almost impossible to create cycles. A cycle is created if the same internal state is encountered more than once - the following states would be the same as well (unless additional entropy is added using setSeed()).

PRNG's are deterministic. That means that they generate the same stream from the same input material (seed). The SUN SHA1PRNG will however seed itself from entropy retrieved from the operating system when the random pool is first accessed.

A special property of the SUN SHA1PRNG is that it will only use the seed given by setSeed() if it is called before the random pool is accessed. In that case the stream will only depend on the given seed and the implemented algorithm; the PRNG is in that case fully deterministic. This can be useful during testing, but please do not rely on this property in production code. Even the SUN SHA1PRNG implementation has seen changes, so you cannot rely on the output to remain constant over different versions.

Note that implementations of SHA1PRNG may differ among cryptographic providers. The code on Android particularly is different and less stable than the SUN SHA1PRNG. Please only use SecureRandom for its intended purpose: generating secure random values.

share|improve this answer

Your Answer


