hash.go 726 B

12345678910111213141516171819202122232425262728293031
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "golang.org/x/crypto/bcrypt"
  6. )
  7. // BcryptHash 使用 bcrypt 对密码进行加密
  8. func BcryptHash(password string) string {
  9. bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  10. return string(bytes)
  11. }
  12. // BcryptCheck 对比明文密码和数据库的哈希值
  13. func BcryptCheck(password, hash string) bool {
  14. err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
  15. return err == nil
  16. }
  17. //@author: [piexlmax](https://github.com/piexlmax)
  18. //@function: MD5V
  19. //@description: md5加密
  20. //@param: str []byte
  21. //@return: string
  22. func MD5V(str []byte, b ...byte) string {
  23. h := md5.New()
  24. h.Write(str)
  25. return hex.EncodeToString(h.Sum(b))
  26. }