data2minio/pkg/utils/utils.go

60 lines
1.2 KiB
Go

package utils
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"github.com/gofrs/uuid"
"math/rand"
"path"
"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 GetUUID() string {
u2, err := uuid.NewV4()
if err != nil {
fmt.Printf("Something went wrong: %s", err)
return ""
}
str := u2.String()
str = strings.Replace(str, "-", "", -1)
return str
}
func GetFileExt(fn string) string {
return path.Ext(fn)
}