mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-17 16:06:38 +08:00
feat: 更新令牌生成算法
This commit is contained in:
parent
b58b1dc0ec
commit
0f95502b04
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user