bloat/util/rand.go

26 lines
401 B
Go
Raw Normal View History

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