| 123456789101112131415161718192021222324 |
- package utils
- import (
- "crypto/md5"
- "encoding/hex"
- "math/rand"
- "time"
- )
- // EncryptPassword 密码加密
- func EncryptPassword(password, salt string) string {
- h := md5.New()
- h.Write([]byte(password + salt))
- return hex.EncodeToString(h.Sum(nil))
- }
- // GenerateSalt 生成随机盐值
- func GenerateSalt() string {
- bytes := make([]byte, 16)
- if _, err := rand.Read(bytes); err != nil {
- return time.Now().Format("20060102150405")
- }
- return hex.EncodeToString(bytes)
- }
|