crypto.go 610 B

123456789101112131415161718
  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. err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
  14. return err == nil
  15. }