Semaphore/pkg/random/string.go
Denis Gukov 42ca90a40c
feat(be): use crypto/rand instead of math/rand
For security reason I replaced math/rand to crypto/rand in places where it is critical. I didn't do this in tests.
2024-12-21 01:14:09 +05:00

24 lines
384 B
Go

package random
import (
"crypto/rand"
"math/big"
)
const (
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
)
func String(strlen int) string {
result := make([]byte, strlen)
charLen := big.NewInt(int64(len(chars)))
for i := range result {
r, err := rand.Int(rand.Reader, charLen)
if err != nil {
panic(err)
}
result[i] = chars[r.Int64()]
}
return string(result)
}