crypto.go 598 B

1234567891011121314151617
  1. // Package crypto provides cryptographic utilities for password hashing and verification.
  2. package crypto
  3. import (
  4. "golang.org/x/crypto/bcrypt"
  5. )
  6. // HashPasswordAsBcrypt generates a bcrypt hash of the given password.
  7. func HashPasswordAsBcrypt(password string) (string, error) {
  8. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  9. return string(hash), err
  10. }
  11. // CheckPasswordHash verifies if the given password matches the bcrypt hash.
  12. func CheckPasswordHash(hash, password string) bool {
  13. return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
  14. }