diff options
author | r <r@freesoftwareextremist.com> | 2020-01-26 07:45:02 +0000 |
---|---|---|
committer | r <r@freesoftwareextremist.com> | 2020-01-26 07:45:34 +0000 |
commit | c9842c65b475fb11876202c626617b019643ef6b (patch) | |
tree | 4660db3dbfae4ed78b0890dcbfa0579206211f1a /util | |
parent | bf2cfaf0ede0e9744408f52538fb4bcd87a6d5b8 (diff) | |
download | bloat-c9842c65b475fb11876202c626617b019643ef6b.tar.gz bloat-c9842c65b475fb11876202c626617b019643ef6b.zip |
Update random id generation algorithm
Diffstat (limited to 'util')
-rw-r--r-- | util/rand.go | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/util/rand.go b/util/rand.go index 212d6d3..ffe97a0 100644 --- a/util/rand.go +++ b/util/rand.go @@ -1,7 +1,8 @@ package util import ( - "math/rand" + "crypto/rand" + "math/big" ) var ( @@ -9,18 +10,22 @@ var ( runes_length = len(runes) ) -func NewRandId(n int) string { +func NewRandId(n int) (string, error) { data := make([]rune, n) for i := range data { - data[i] = runes[rand.Intn(runes_length)] + num, err := rand.Int(rand.Reader, big.NewInt(int64(runes_length))) + if err != nil { + return "", err + } + data[i] = runes[num.Int64()] } - return string(data) + return string(data), nil } -func NewSessionId() string { +func NewSessionId() (string, error) { return NewRandId(24) } -func NewCSRFToken() string { +func NewCSRFToken() (string, error) { return NewRandId(24) } |