hash.go 444 B

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