Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
55ddd363
authored
Sep 25, 2024
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 更新令牌生成算法
parent
5686e7b7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
3 deletions
+12
-3
common/utils.go
+12
-3
No files found.
common/utils.go
View file @
55ddd363
...
@@ -2,6 +2,7 @@ package common
...
@@ -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 {
...
@@ -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
GenerateRandom
Chars
Key
(
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) {
...
@@ -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
GenerateRandom
Chars
Key
(
48
)
}
}
func
GetRandomInt
(
max
int
)
int
{
func
GetRandomInt
(
max
int
)
int
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment