hpds_jkw_web/pkg/utils/utils.go

41 lines
929 B
Go
Raw Normal View History

2023-01-06 10:09:23 +08:00
package utils
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"math/rand"
"time"
)
/*
RandomString 产生随机数
- size 随机码的位数
- kind 0 // 纯数字
1 // 小写字母
2 // 大写字母
3 // 数字、大小写字母
*/
func RandomString(size int, kind int) string {
iKind, kinds, rsBytes := kind, [][]int{[]int{10, 48}, []int{26, 97}, []int{26, 65}}, make([]byte, size)
isAll := kind > 2 || kind < 0
rand.Seed(time.Now().UnixNano())
for i := 0; i < size; i++ {
if isAll { // random iKind
iKind = rand.Intn(3)
}
scope, base := kinds[iKind][0], kinds[iKind][1]
rsBytes[i] = uint8(base + rand.Intn(scope))
}
return string(rsBytes)
}
func GetUserSha1Pass(pass, salt string) string {
key := []byte(salt)
mac := hmac.New(sha1.New, key)
mac.Write([]byte(pass))
//进行base64编码
res := base64.StdEncoding.EncodeToString(mac.Sum(nil))
return res
}