feat: 更新令牌生成算法

This commit is contained in:
1808837298@qq.com
2024-09-24 20:19:18 +08:00
parent 05d9aa61df
commit b58b1dc0ec
3 changed files with 50 additions and 16 deletions

View File

@@ -1,10 +1,12 @@
package common
import (
crand "crypto/rand"
"fmt"
"github.com/google/uuid"
"html/template"
"log"
"math/big"
"math/rand"
"net"
"os/exec"
@@ -145,21 +147,24 @@ func init() {
rand.Seed(time.Now().UnixNano())
}
func GenerateKey() string {
//rand.Seed(time.Now().UnixNano())
key := make([]byte, 48)
for i := 0; i < 16; i++ {
key[i] = keyChars[rand.Intn(len(keyChars))]
}
uuid_ := GetUUID()
for i := 0; i < 32; i++ {
c := uuid_[i]
if i%2 == 0 && c >= 'a' && c <= 'z' {
c = c - 'a' + 'A'
func GenerateRandomKey(length int) (string, error) {
b := make([]byte, length)
maxI := big.NewInt(int64(len(keyChars)))
for i := range b {
n, err := crand.Int(crand.Reader, maxI)
if err != nil {
return "", err
}
key[i+16] = c
b[i] = keyChars[n.Int64()]
}
return string(key)
return string(b), nil
}
func GenerateKey() (string, error) {
//rand.Seed(time.Now().UnixNano())
return GenerateRandomKey(48)
}
func GetRandomInt(max int) int {