lundi 15 juin 2015

Generate n random bytes based on m-byte seed in Java

I'd like to generate n random bytes from a given m-byte seed. The generated sequence has to be reproducible; for the same seed the same sequence has to be generated. n can be either higher or lower than m.

The two following trivial approaches that are coming to my mind are biased:

  • Hash the m bytes to create a long seed to feed a new java.util.Random generator. Problem: I discard entropy if n<8, as the default Random seed in Java is a 8-byte long.
  • Hash the m bytes to generate some "random" data. Problem: this cap n to some value (20 for SHA1 for example).

Is there a standard way of doing this? I did not see any relevant class in java.security, but I guess this is a basic need for cryptography?

Note: I do not need "crypto-level extra-secure" random, just random that passes basic statistical randomness tests. Also I'd prefer relying on standard code instead of having to code everything by myself.




Aucun commentaire:

Enregistrer un commentaire