photoprism/pkg/rnd/charset.go
Michael Mayer f5a8c5a45d Auth: Session and ACL enhancements #98 #1746
Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-09-28 09:01:17 +02:00

35 lines
850 B
Go

package rnd
import (
"crypto/rand"
"math/big"
)
const CharsetBase36 = "abcdefghijklmnopqrstuvwxyz0123456789"
const CharsetBase62 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// Base36 generates a random token containing lowercase letters and numbers.
func Base36(length int) string {
return Charset(length, CharsetBase36)
}
// Base62 generates a random token containing upper and lower case letters as well as numbers.
func Base62(length int) string {
return Charset(length, CharsetBase62)
}
// Charset generates a random token with the specified length and charset.
func Charset(length int, charset string) string {
max := big.NewInt(int64(len(charset)))
b := make([]byte, length)
for i := range b {
if r, err := rand.Int(rand.Reader, max); err == nil {
b[i] = charset[r.Int64()]
}
}
return string(b)
}