2025-09-20 00:35:50 -07:00
|
|
|
// Package crypto provides cryptographic utilities for password hashing and verification.
|
2025-05-03 02:27:53 -07:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
)
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
// HashPasswordAsBcrypt generates a bcrypt hash of the given password.
|
2025-05-03 02:27:53 -07:00
|
|
|
func HashPasswordAsBcrypt(password string) (string, error) {
|
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
|
return string(hash), err
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
// CheckPasswordHash verifies if the given password matches the bcrypt hash.
|
2025-05-03 02:27:53 -07:00
|
|
|
func CheckPasswordHash(hash, password string) bool {
|
2026-01-04 20:54:56 -08:00
|
|
|
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
|
2025-05-03 02:27:53 -07:00
|
|
|
}
|
2026-06-01 11:48:12 -07:00
|
|
|
|
|
|
|
|
func IsHashed(s string) bool {
|
|
|
|
|
_, err := bcrypt.Cost([]byte(s))
|
|
|
|
|
return err == nil
|
|
|
|
|
}
|