Compare commits

...

4 Commits

Author SHA1 Message Date
Alexey Kostin 6a79c34702
Merge 6fc33ad3a0 into 1eecc22fb3 2024-04-11 08:35:15 +00:00
Andrea Spacca 6fc33ad3a0
Merge branch 'main' into tokensecure 2024-04-11 17:35:12 +09:00
Alexey Kostin 27c38e6a1e simplier and faster casting random symbols 2024-03-07 12:26:26 +03:00
Alexey Kostin ca5c89f130 Generate secure token using crypto rand 2024-03-06 18:43:44 +03:00
1 changed files with 10 additions and 5 deletions

View File

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