2026-03-14 20:52:11 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"crypto/sha256"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// GenerateToken returns a cryptographically random 256-bit token encoded as a
|
|
|
|
|
// 64-character lowercase hex string.
|
|
|
|
|
func GenerateToken() (string, error) {
|
2026-04-01 11:37:36 +02:00
|
|
|
raw := make([]byte, sessionTokenBytes) // 256 bits
|
2026-03-14 20:52:11 +01:00
|
|
|
if _, err := rand.Read(raw); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return hex.EncodeToString(raw), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HashToken returns the SHA-256 hex digest of token. Store this hash in the
|
|
|
|
|
// database; never store the plaintext token.
|
|
|
|
|
func HashToken(token string) string {
|
|
|
|
|
sum := sha256.Sum256([]byte(token))
|
|
|
|
|
return hex.EncodeToString(sum[:])
|
|
|
|
|
}
|