snapshot of https://github.com/traggo/server.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
build / build (push) Failing after 3s
Some checks failed
build / build (push) Failing after 3s
This commit is contained in:
43
auth/rand/random.go
Normal file
43
auth/rand/random.go
Normal file
@@ -0,0 +1,43 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user