Use crypto/rand for improved security
This commit is contained in:
parent
39dd89eec2
commit
f57388e276
|
@ -1,14 +1,25 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import "math/rand"
|
import "crypto/rand"
|
||||||
|
|
||||||
|
import "math/big"
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
func GenerateToken() string {
|
func GenerateToken() string {
|
||||||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
const length = 8
|
const length = 8
|
||||||
|
|
||||||
|
charLen := big.NewInt(int64(len(charset)))
|
||||||
|
|
||||||
b := make([]byte, length)
|
b := make([]byte, length)
|
||||||
for i := range b {
|
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)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue