create RNG in token module

This commit is contained in:
wish
2023-03-12 23:29:50 +11:00
parent cce558db9c
commit ccfd2ac36f
5 changed files with 19 additions and 15 deletions

View File

@@ -1,13 +1,22 @@
package token
import "math/rand"
import (
"math/rand"
"time"
)
// Generate returns an alphanumeric token of specified length
func Generate(length int) string {
rng := RNG()
var chars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length)
for i := range b {
b[i] = chars[rand.Intn(len(chars))]
b[i] = chars[rng.Intn(len(chars))]
}
return string(b)
}
// RNG returns a new RNG generator
func RNG() *rand.Rand {
return rand.New(rand.NewSource(time.Now().UnixNano()))
}