-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_utils.go
48 lines (36 loc) · 1.09 KB
/
base_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package model
import (
"crypto/md5"
"encoding/hex"
"fmt"
"math/rand"
"time"
)
var seed = time.Now().Unix()
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func RandString(size int) string {
randGen := rand.New(rand.NewSource(seed))
// Define the characters that can be used in the random string
// Create a byte slice to hold the random characters
randomBytes := make([]byte, size)
// Fill the byte slice with random characters from the charset
for i := range randomBytes {
randomBytes[i] = charset[randGen.Intn(len(charset))]
}
// Convert the byte slice to a string
randomString := string(randomBytes)
// Print the random string
return randomString
}
func GetMD5Hash(text string) string {
hash := md5.Sum([]byte(text))
return hex.EncodeToString(hash[:])
}
const encryptSalt = "uclipboard:%s"
func TokenEncrypt(token string) string {
// encrypt token with 3 pheases md5
md5_1 := GetMD5Hash(fmt.Sprintf(encryptSalt, token))
md5_2 := GetMD5Hash(fmt.Sprintf(encryptSalt, md5_1))
md5_3 := GetMD5Hash(fmt.Sprintf(encryptSalt, md5_2))
return md5_3
}