5 ms·
Yes, don't use % even with a CSPRNG! https://stackoverflow.com/a/31374501/2224584 https://stackoverflow.com/a/31374501/2224584
by sarciszewski 11y ago
Yes, don't use % even with a CSPRNG!
https://stackoverflow.com/a/31374501/2224584 https://stackoverflow.com/a/31374501/2224584
- tptacek 11y agoIt's true that's an anti-pattern, but it's not a particularly exploitable one in most of the places that really need CSPRNGs.
- paragon_init 11y agoAgreed. It's a red flag for "expect more exploitable issues to be found around the corner" and can result in biased distributions, but it by itself does not break a RNG.
- lucb1e 11y agoOh? I get perfectly sane results using modulo, as it should if it is actually a completely random number: http://3v4l.org/nIBGT http://3v4l.org/nIBGT
- sarciszewski 11y agoYou found the exception! Congratulations! You're using a bucket size which is a power of 2. A random byte fits on the interval of [0, 255] which is evenly distributed modulo 256, which if you reduce mod 2^N for 1 < N < 8 will still produce a uniform distribution. If you copied my JS code into your console and changed the 54 to 64 the bias evaporates. Hooray :D However, for arbitrary ranges, you should use an unbiased selection strategy to ensure a uniform distribution. http://3v4l.org/EgfPR http://3v4l.org/EgfPR - notice 0 is significantly more common than the other values when reduced mod 17 http://3v4l.org/LXmKq http://3v4l.org/LXmKq - same with mod 15 https://defuse.ca/generating-random-passwords.htm https://defuse.ca/generating-random-passwords.htm