BrainMinder/internal/token/token.go

35 lines
562 B
Go
Raw Normal View History

2024-08-22 10:13:16 +02:00
package token
import (
"crypto/rand"
"crypto/sha256"
"encoding/base32"
"encoding/hex"
"strings"
)
func randomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
func New() (string, error) {
b, err := randomBytes(16)
if err != nil {
return "", err
}
return strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(b)), nil
}
func Hash(plaintext string) string {
hash := sha256.Sum256([]byte(plaintext))
return hex.EncodeToString(hash[:])
}