md5.go 589 B

12345678910111213141516171819202122232425
  1. package md5
  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. func MD5V(str []byte, b ...byte) string {
  18. h := md5.New()
  19. h.Write(str)
  20. return hex.EncodeToString(h.Sum(b))
  21. }