Fix modulo bias in randomBytesMod. See https://github.com/dchest/uniuri/commit/2e215562f605dacd124f15eb89523bdc32da3a41
Dmitry Chestnykh dmitry@codingrobots.com
Fri, 24 Apr 2015 15:21:27 +0200
1 files changed,
8 insertions(+),
1 deletions(-)
jump to
M
random.go
→
random.go
@@ -72,8 +72,14 @@
// randomBytesMod returns a byte slice of the given length, where each byte is // a random number modulo mod. func randomBytesMod(length int, mod byte) (b []byte) { + if length == 0 { + return nil + } + if mod == 0 { + panic("captcha: bad mod argument for randomBytesMod") + } + maxrb := 255 - byte(256 % int(mod)) b = make([]byte, length) - maxrb := byte(256 - (256 % int(mod))) i := 0 for { r := randomBytes(length + (length / 4))@@ -89,6 +95,7 @@ return
} } } + } // randomId returns a new random id string.