Files
OwnCord/Server/auth/session.go
T
jevb ecbffddf19 refactor: extract magic numbers to named constants
Create Server/api/constants.go (26 constants) and Server/auth/constants.go
(6 constants) for rate limits, timeouts, size limits, and token generation.
Replace all inline magic numbers with descriptive names across 9 source files.
No behavior changes — same values, just named for contributor readability.
2026-04-01 11:37:36 +02:00

25 lines
620 B
Go

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) {
raw := make([]byte, sessionTokenBytes) // 256 bits
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[:])
}