2025-09-20 07:35:50 +00:00
|
|
|
// Package crypto provides cryptographic utilities for password hashing and verification.
|
2025-05-03 09:27:53 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// HashPasswordAsBcrypt generates a bcrypt hash of the given password.
|
2025-05-03 09:27:53 +00:00
|
|
|
func HashPasswordAsBcrypt(password string) (string, error) {
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
return string(hash), err
|
|
|
|
}
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// CheckPasswordHash verifies if the given password matches the bcrypt hash.
|
2025-05-03 09:27:53 +00:00
|
|
|
func CheckPasswordHash(hash, password string) bool {
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
|
|
return err == nil
|
|
|
|
}
|