Spaces:
Running
Running
Download internal/auth/auth.go from Soumik-404/parseFlow: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/Soumik-404/parseFlow/resolve/main/internal/auth/auth.go
- Command line
-
hf download hf://spaces/Soumik-404/parseFlow/internal/auth/auth.go
-
curl -L -o auth.go https://huggingface.co/spaces/Soumik-404/parseFlow/resolve/main/internal/auth/auth.go
1.61 kB
| 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") | |