Vestmore_GO/utils/auth/valid_code.go

41 lines
779 B
Go
Raw Normal View History

2024-04-09 10:17:08 +00:00
package auth
import (
2024-04-15 08:08:17 +00:00
"crypto/md5"
2024-04-09 10:17:08 +00:00
"fmt"
"math/rand"
2024-04-15 08:08:17 +00:00
"strconv"
"time"
2024-04-09 10:17:08 +00:00
)
2024-04-15 08:08:17 +00:00
var myRand = rand.New(rand.NewSource(time.Now().UnixNano()))
2024-04-09 10:17:08 +00:00
func GenerateVerificationCode() string {
var code string
for i := 0; i < 3; i++ {
// 生成两个 10-99 之间的随机数
2024-04-15 08:08:17 +00:00
a := myRand.Intn(90-10+1) + 10
b := myRand.Intn(90-10+1) + 10
c := myRand.Intn(90-10+1) + 10
2024-04-09 10:17:08 +00:00
// 将随机数拼接到验证码字符串
code += fmt.Sprintf("%d%d%d", a, b, c)
}
return code
}
2024-04-15 08:08:17 +00:00
func GenerateMD5() string {
// 将时间戳转换为字节slice
timestampBytes := []byte(strconv.FormatInt(time.Now().UnixNano(), 10))
// 计算MD5哈希
hashBytes := md5.Sum(timestampBytes)
// 将哈希值转换为十六进制字符串
hashHex := fmt.Sprintf("%x", hashBytes)
return hashHex
}