Is there any special parameters for using SHA-1 for generating pseudo-random numbers? What are the differences between SHA-1 as a PRNG and original SHA-1?


There are many possible constructions but the simplest is probably to initalize a counter with a random seed and hash the counter to produce more random bits and then increment the counter.

| improve this answer | |
  • $\begingroup$ And what about having a sequence made by using SHA-1 on the previous hash and having some big number as a starting value? (it will be a seed). counter=10 seed=12345 r=sha1(seed) for i in range(counter): ` print(r)` #making new random number ` r=sha1(r)` Will it be ok? $\endgroup$ – R Georgy Nov 2 '19 at 18:04
  • $\begingroup$ Yes, that is also ok, you are expected to repeat after aproximately 2^80 steps which is less than what I proposed but should still be sufficient for most purposes. SHA-1 is not considered secure but due to no viable pre-image attack I do not know of a viable attack on either construction. Regardless of PRNG you need a high entropy seed for security. $\endgroup$ – Meir Maor Nov 2 '19 at 18:19
  • $\begingroup$ What? No! That is not OK! The function $k \mapsto H(k) \mathbin\| H(H(k)) \mathbin\| H(H(H(k))) \mathbin\| \dotsb$ is absolutely not a secure pseudorandom generator. Anyone who sees one block of output can trivially predict all subsequent blocks of output. $\endgroup$ – Squeamish Ossifrage Nov 3 '19 at 14:52
  • $\begingroup$ Yes of course, it only works if you output a small part of your state. $\endgroup$ – Meir Maor Nov 3 '19 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.