feat: 更新令牌生成算法

This commit is contained in:
1808837298@qq.com 2024-09-25 16:30:33 +08:00
parent b58b1dc0ec
commit 0f95502b04

View File

@ -2,6 +2,7 @@ package common
import ( import (
crand "crypto/rand" crand "crypto/rand"
"encoding/base64"
"fmt" "fmt"
"github.com/google/uuid" "github.com/google/uuid"
"html/template" "html/template"
@ -144,10 +145,10 @@ func GetUUID() string {
const keyChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" const keyChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func init() { func init() {
rand.Seed(time.Now().UnixNano()) rand.New(rand.NewSource(time.Now().UnixNano()))
} }
func GenerateRandomKey(length int) (string, error) { func GenerateRandomCharsKey(length int) (string, error) {
b := make([]byte, length) b := make([]byte, length)
maxI := big.NewInt(int64(len(keyChars))) maxI := big.NewInt(int64(len(keyChars)))
@ -162,9 +163,17 @@ func GenerateRandomKey(length int) (string, error) {
return string(b), nil return string(b), nil
} }
func GenerateRandomKey(length int) (string, error) {
bytes := make([]byte, length*3/4) // 对于48位的输出这里应该是36
if _, err := crand.Read(bytes); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(bytes), nil
}
func GenerateKey() (string, error) { func GenerateKey() (string, error) {
//rand.Seed(time.Now().UnixNano()) //rand.Seed(time.Now().UnixNano())
return GenerateRandomKey(48) return GenerateRandomCharsKey(48)
} }
func GetRandomInt(max int) int { func GetRandomInt(max int) int {