Generate secure token using crypto rand

This commit is contained in:
Alexey Kostin 2024-03-06 18:43:44 +03:00
parent 54b4f1aa86
commit ca5c89f130

View file

@ -25,7 +25,9 @@ THE SOFTWARE.
package server package server
import ( import (
"math/rand" "crypto/rand"
"log"
"math/big"
) )
const ( const (
@ -37,8 +39,11 @@ const (
func token(length int) string { func token(length int) string {
result := "" result := ""
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
x := rand.Intn(len(SYMBOLS) - 1) x, err := rand.Int(rand.Reader, big.NewInt(int64(len(SYMBOLS))))
result = string(SYMBOLS[x]) + result if err != nil {
log.Fatal("Failed to generate token")
}
result = string(SYMBOLS[x.Int64()]) + result
} }
return result return result