2019-12-13 18:08:26 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2020-01-26 07:45:02 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
2019-12-13 18:08:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
runes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
|
|
|
|
runes_length = len(runes)
|
|
|
|
)
|
|
|
|
|
2020-01-28 17:51:00 +00:00
|
|
|
func NewRandID(n int) (string, error) {
|
2019-12-13 18:08:26 +00:00
|
|
|
data := make([]rune, n)
|
|
|
|
for i := range data {
|
2020-01-26 07:45:02 +00: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 18:08:26 +00:00
|
|
|
}
|
2020-01-26 07:45:02 +00:00
|
|
|
return string(data), nil
|
2019-12-13 18:08:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 17:51:00 +00:00
|
|
|
func NewSessionID() (string, error) {
|
|
|
|
return NewRandID(24)
|
2019-12-13 18:08:26 +00:00
|
|
|
}
|
2020-01-25 10:07:06 +00:00
|
|
|
|
2020-01-26 07:45:02 +00:00
|
|
|
func NewCSRFToken() (string, error) {
|
2020-01-28 17:51:00 +00:00
|
|
|
return NewRandID(24)
|
2020-01-25 10:07:06 +00:00
|
|
|
}
|