This repository has been archived on 2022-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
elon-eats-my-tweets/httpserver/token_generator.go

34 lines
611 B
Go
Raw Normal View History

2022-05-20 19:52:54 +00:00
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[:])
}