package auth import ( "crypto/rand" "crypto/sha256" "crypto/subtle" "encoding/base64" "encoding/hex" "errors" "golang.org/x/crypto/bcrypt" ) // HashPassword returns a bcrypt hash of the plaintext password. func HashPassword(plain string) (string, error) { b, err := bcrypt.GenerateFromPassword([]byte(plain), bcrypt.DefaultCost) if err != nil { return "", err } return string(b), nil } // CheckPassword reports whether plain matches the stored bcrypt hash. func CheckPassword(hash, plain string) bool { return bcrypt.CompareHashAndPassword([]byte(hash), []byte(plain)) == nil } // NewToken returns a cryptographically random URL-safe token (32 bytes). func NewToken() (string, error) { b := make([]byte, 32) if _, err := rand.Read(b); err != nil { return "", err } return base64.RawURLEncoding.EncodeToString(b), nil } // HashToken hashes a session token for storage. The raw token is only ever // kept in the user's cookie; the DB stores the SHA-256 digest. func HashToken(token string) string { sum := sha256.Sum256([]byte(token)) return hex.EncodeToString(sum[:]) } // NewCSRF returns a random CSRF token (32 bytes, hex). func NewCSRF() (string, error) { b := make([]byte, 32) if _, err := rand.Read(b); err != nil { return "", err } return hex.EncodeToString(b), nil } // ConstantTimeEqual compares two strings in constant time. func ConstantTimeEqual(a, b string) bool { return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1 } // ErrToken is returned when token generation fails (system entropy error). var ErrToken = errors.New("failed to generate secure token")