package hashing import ( "RenewCMS/internal/domain/apikey" "crypto/rand" "crypto/sha256" "encoding/base64" "encoding/hex" ) const ( apiKeyRandomBytes = 32 apiKeyPrefixLen = 12 ) type SHA256KeyGenerator struct{} func NewSHA256KeyGenerator() *SHA256KeyGenerator { return &SHA256KeyGenerator{} } func (g *SHA256KeyGenerator) Generate() (string, string, string) { random := make([]byte, apiKeyRandomBytes) _, _ = rand.Read(random) rawKey := apikey.RawKeyPrefix + base64.RawURLEncoding.EncodeToString(random) return rawKey, rawKey[:apiKeyPrefixLen], g.Hash(rawKey) } func (g *SHA256KeyGenerator) Hash(rawKey string) string { sum := sha256.Sum256([]byte(rawKey)) return hex.EncodeToString(sum[:]) } var _ apikey.KeyGenerator = &SHA256KeyGenerator{}