bloat/util/rand.go

32 lines
563 B
Go
Raw Normal View History

2019-12-13 20:08:26 +02:00
package util
import (
2020-01-26 09:45:02 +02:00
"crypto/rand"
"math/big"
2019-12-13 20:08:26 +02:00
)
var (
runes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
runes_length = len(runes)
)
2020-01-28 19:51:00 +02:00
func NewRandID(n int) (string, error) {
2019-12-13 20:08:26 +02:00
data := make([]rune, n)
for i := range data {
2020-01-26 09:45:02 +02:00
num, err := rand.Int(rand.Reader, big.NewInt(int64(runes_length)))
if err != nil {
return "", err
}
data[i] = runes[num.Int64()]
2019-12-13 20:08:26 +02:00
}
2020-01-26 09:45:02 +02:00
return string(data), nil
2019-12-13 20:08:26 +02:00
}
2020-01-28 19:51:00 +02:00
func NewSessionID() (string, error) {
return NewRandID(24)
2019-12-13 20:08:26 +02:00
}
2020-01-25 12:07:06 +02:00
2020-01-26 09:45:02 +02:00
func NewCSRFToken() (string, error) {
2020-01-28 19:51:00 +02:00
return NewRandID(24)
2020-01-25 12:07:06 +02:00
}