aboutsummaryrefslogtreecommitdiff
path: root/util/rand.go
diff options
context:
space:
mode:
Diffstat (limited to 'util/rand.go')
-rw-r--r--util/rand.go17
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)
}