bloat/util/rand.go

26 lines
401 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"
2022-01-02 13:38:06 +02:00
"encoding/base64"
2019-12-13 20:08:26 +02:00
)
2022-01-02 13:38:06 +02:00
var enc = base64.URLEncoding
2019-12-13 20:08:26 +02:00
2020-01-28 19:51:00 +02:00
func NewRandID(n int) (string, error) {
2022-01-02 13:38:06 +02:00
data := make([]byte, enc.DecodedLen(n))
_, err := rand.Read(data)
if err != nil {
return "", err
2019-12-13 20:08:26 +02:00
}
2022-01-02 13:38:06 +02:00
return enc.EncodeToString(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
}