encrypt.go 475 B

123456789101112131415161718192021222324
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "math/rand"
  6. "time"
  7. )
  8. // EncryptPassword 密码加密
  9. func EncryptPassword(password, salt string) string {
  10. h := md5.New()
  11. h.Write([]byte(password + salt))
  12. return hex.EncodeToString(h.Sum(nil))
  13. }
  14. // GenerateSalt 生成随机盐值
  15. func GenerateSalt() string {
  16. bytes := make([]byte, 16)
  17. if _, err := rand.Read(bytes); err != nil {
  18. return time.Now().Format("20060102150405")
  19. }
  20. return hex.EncodeToString(bytes)
  21. }