Browse Source

Merge branch 'leonklingele-fix-biased-rng' into develop

pull/265/head
Simon Eisenmann 10 years ago
parent
commit
8d1e18b007
  1. 1
      AUTHORS
  2. 28
      src/app/spreed-webrtc-server/random.go

1
AUTHORS

@ -6,6 +6,7 @@
Simon Eisenmann <simon@struktur.de> Simon Eisenmann <simon@struktur.de>
Joachim Bauch <bauch@struktur.de> Joachim Bauch <bauch@struktur.de>
Evan Theurer <theurer@struktur.de> Evan Theurer <theurer@struktur.de>
Leon Klingele <klingele@struktur.de>
Translation: Translation:
Curt Frisemo <curt.frisemo@spreed.com> Curt Frisemo <curt.frisemo@spreed.com>

28
src/app/spreed-webrtc-server/random.go

@ -23,30 +23,34 @@ package main
import ( import (
"crypto/rand" "crypto/rand"
"math/big"
pseudoRand "math/rand" pseudoRand "math/rand"
"time" "time"
) )
const ( const (
dict = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW0123456789" dict = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
) )
func NewRandomString(length int) string { func newRandomInt(max *big.Int) int {
rand, err := rand.Int(rand.Reader, max)
buf := make([]byte, length)
_, err := rand.Read(buf)
if err != nil { if err != nil {
// fallback to pseudo-random // Fallback to pseudo-random
for i := 0; i < length; i++ { return pseudoRand.Intn(int(max.Int64()))
buf[i] = dict[pseudoRand.Intn(len(dict))]
} }
} else { return int(rand.Int64())
}
// NewRandomString returns a alphanumeric random string with
// the specified length using crypto/rand with fallback to
// math/rand on error.
func NewRandomString(length int) string {
buf := make([]byte, length)
max := big.NewInt(int64(len(dict)))
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
buf[i] = dict[int(buf[i])%len(dict)] buf[i] = dict[newRandomInt(max)]
}
} }
return string(buf) return string(buf)
} }
func init() { func init() {

Loading…
Cancel
Save