hash.go 449 B

1234567891011121314151617
  1. package utils
  2. import (
  3. "golang.org/x/crypto/bcrypt"
  4. )
  5. // BcryptHash 使用 bcrypt 对密码进行加密
  6. func BcryptHash(password string) string {
  7. bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  8. return string(bytes)
  9. }
  10. // BcryptCheck 对比明文密码和数据库的哈希值
  11. func BcryptCheck(password, hash string) bool {
  12. err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
  13. return err == nil
  14. }