34 lines
611 B
Go
34 lines
611 B
Go
package httpserver
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
rand.Seed(time.Now().UnixNano())
|
|
}
|
|
|
|
type TokenGenerator interface {
|
|
GenerateToken(n int) string
|
|
}
|
|
|
|
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
type RandomTokenGenerator struct{}
|
|
|
|
func (g RandomTokenGenerator) GenerateToken(n int) string {
|
|
b := make([]rune, n)
|
|
for i := range b {
|
|
b[i] = letters[rand.Intn(len(letters))]
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func encodeSHA256(s string) string {
|
|
enc := sha256.Sum256([]byte(s))
|
|
return base64.RawURLEncoding.EncodeToString(enc[:])
|
|
}
|