49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"crypto/hmac"
|
||
|
"crypto/sha1"
|
||
|
"encoding/base64"
|
||
|
"github.com/google/uuid"
|
||
|
"math/rand"
|
||
|
"strings"
|
||
|
"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
|
||
|
}
|
||
|
|
||
|
func GetUUIDString() string {
|
||
|
u, _ := uuid.NewUUID()
|
||
|
str := strings.Replace(u.String(), "-", "", -1)
|
||
|
return str
|
||
|
}
|