苏州瑞玛APS项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
728 B

3 years ago
3 years ago
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "github.com/kataras/golog"
  6. "golang.org/x/crypto/bcrypt"
  7. )
  8. // Md5 returns the MD5 checksum string of the data.
  9. func Md5(b []byte) string {
  10. checksum := md5.Sum(b)
  11. return hex.EncodeToString(checksum[:])
  12. }
  13. //加密
  14. func Encrypt(password string) string {
  15. if password == "" {
  16. return ""
  17. }
  18. if hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost); err != nil {
  19. return ""
  20. } else {
  21. password = string(hash)
  22. return password
  23. }
  24. }
  25. func CompareHashAndPassword(e string, p string) (bool, error) {
  26. err := bcrypt.CompareHashAndPassword([]byte(e), []byte(p))
  27. if err != nil {
  28. golog.Println(err.Error())
  29. return false, err
  30. }
  31. return true, nil
  32. }