hash.go 644 B

12345678910111213141516171819202122232425262728
  1. // Package hash 哈希操作类
  2. package hash
  3. import (
  4. "github.com/runningwater/gohub/pkg/logger"
  5. "golang.org/x/crypto/bcrypt"
  6. )
  7. // BcryptHash bcrypt 哈希, 用于加密密码
  8. func BcryptHash(password string) string {
  9. bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  10. logger.LogIf(err)
  11. return string(bytes)
  12. }
  13. // BcryptCheck 检查密码是否匹配
  14. func BcryptCheck(password, hash string) bool {
  15. err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
  16. return err == nil
  17. }
  18. // BcryptIsHashed 检查密码是否已经被哈希
  19. func BcryptIsHashed(str string) bool {
  20. return len(str) == 60
  21. }