2020-02-09 21:25:33 +01:00
|
|
|
package utils
|
|
|
|
|
2020-02-21 12:13:49 +01:00
|
|
|
import "crypto/rand"
|
|
|
|
|
|
|
|
import "math/big"
|
|
|
|
|
|
|
|
import "log"
|
2020-02-09 21:25:33 +01:00
|
|
|
|
|
|
|
func GenerateToken() string {
|
|
|
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
const length = 8
|
|
|
|
|
2020-02-21 12:13:49 +01:00
|
|
|
charLen := big.NewInt(int64(len(charset)))
|
|
|
|
|
2020-02-09 21:25:33 +01:00
|
|
|
b := make([]byte, length)
|
|
|
|
for i := range b {
|
2020-02-21 12:13:49 +01:00
|
|
|
|
|
|
|
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()]
|
2020-02-09 21:25:33 +01:00
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|