Files
upstream-traggo/auth/rand/random.go
upstream-mirror cda7c1a474
Some checks failed
build / build (push) Failing after 3s
snapshot of https://github.com/traggo/server.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 05:24:45 +05:00

44 lines
782 B
Go

package rand
import (
"crypto/rand"
"github.com/rs/zerolog/log"
)
var (
chars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
randRead = rand.Read
)
func init() {
// exits with 1 when crypto/rand is not available
Token(1)
}
func randBytes(count int) ([]byte, error) {
bytes := make([]byte, count)
_, err := randRead(bytes)
return bytes, err
}
// Token returns a random token.
func Token(count int) string {
bytes, err := randBytes(count)
if err != nil {
log.Panic().Msg("crypto/rand is not available")
}
return bytesToString(bytes, count)
}
func bytesToString(bytes []byte, count int) string {
token := make([]rune, count)
for index, item := range bytes {
token[index] = chars[int(item)%len(chars)]
}
return string(token)
}