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