1
Fork 0

Use crypto/rand for improved security

This commit is contained in:
viktorstrate 2020-02-21 12:13:49 +01:00
parent 39dd89eec2
commit f57388e276
1 changed files with 13 additions and 2 deletions

View File

@ -1,14 +1,25 @@
package utils
import "math/rand"
import "crypto/rand"
import "math/big"
import "log"
func GenerateToken() string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const length = 8
charLen := big.NewInt(int64(len(charset)))
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
n, err := rand.Int(rand.Reader, charLen)
if err != nil {
log.Fatalf("Could not generate random number: %s\n", err)
}
b[i] = charset[n.Int64()]
}
return string(b)
}