diff options
author | r <r@freesoftwareextremist.com> | 2022-01-02 11:38:06 +0000 |
---|---|---|
committer | r <r@freesoftwareextremist.com> | 2022-01-02 11:38:06 +0000 |
commit | fcb88c8a85fd2918c4c9e30c3ef49e716d77f2b0 (patch) | |
tree | f927a1b68c76c6d26b5643ea8f150139e851b21e /util | |
parent | c5f12920b0879498b0a20342650a09b0ad5c9d7c (diff) | |
download | bloat-fcb88c8a85fd2918c4c9e30c3ef49e716d77f2b0.tar.gz bloat-fcb88c8a85fd2918c4c9e30c3ef49e716d77f2b0.zip |
Simplify id generation
Diffstat (limited to 'util')
-rw-r--r-- | util/rand.go | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/util/rand.go b/util/rand.go index 1e4ec95..90e66a5 100644 --- a/util/rand.go +++ b/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) { |