mirror of
https://git.freesoftwareextremist.com/bloat
synced 2024-12-22 09:10:42 +00:00
Simplify id generation
This commit is contained in:
parent
c5f12920b0
commit
fcb88c8a85
1 changed files with 7 additions and 13 deletions
20
util/rand.go
20
util/rand.go
|
@ -2,24 +2,18 @@ package util
|
|||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
var (
|
||||
runes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
|
||||
runes_length = len(runes)
|
||||
)
|
||||
var enc = base64.URLEncoding
|
||||
|
||||
func NewRandID(n int) (string, error) {
|
||||
data := make([]rune, n)
|
||||
for i := range data {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(runes_length)))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
data[i] = runes[num.Int64()]
|
||||
data := make([]byte, enc.DecodedLen(n))
|
||||
_, err := rand.Read(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
return enc.EncodeToString(data), nil
|
||||
}
|
||||
|
||||
func NewSessionID() (string, error) {
|
||||
|
|
Loading…
Reference in a new issue