mirror of
https://github.com/taigrr/pastebin
synced 2025-01-18 04:03:18 -08:00
simplify random string generator
This commit is contained in:
parent
fabc69c1f7
commit
c52f7b6050
17
utils.go
17
utils.go
@ -2,18 +2,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// RandomString generates random bytes and then encodes them using base64
|
||||||
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
// which guarantees they are URL-safe. The resultant output is not necessarily
|
||||||
)
|
// a valid base-64 string.
|
||||||
|
|
||||||
// RandomString ...
|
|
||||||
func RandomString(length int) string {
|
func RandomString(length int) string {
|
||||||
bytes := make([]byte, length)
|
bytes := make([]byte, length*2)
|
||||||
rand.Read(bytes)
|
rand.Read(bytes)
|
||||||
for i, b := range bytes {
|
se := base64.StdEncoding.EncodeToString(bytes)
|
||||||
bytes[i] = alphabet[b%byte(len(alphabet))]
|
return se[0:length]
|
||||||
}
|
|
||||||
return string(bytes)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user