Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(security): generating random string with crypto rand #7525

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions pkg/utils/random/random.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package random

import (
"math/rand"
"crypto/rand"
"math/big"
mathRand "math/rand"
"time"

"github.com/google/uuid"
)

var Rand *rand.Rand
var Rand *mathRand.Rand

const letterBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

func String(n int) string {
b := make([]byte, n)
letterLen := big.NewInt(int64(len(letterBytes)))
for i := range b {
b[i] = letterBytes[Rand.Intn(len(letterBytes))]
idx, err := rand.Int(rand.Reader, letterLen)
if err != nil {
panic(err)
}
b[i] = letterBytes[idx.Int64()]
}
return string(b)
}
Expand All @@ -24,10 +31,10 @@ func Token() string {
}

func RangeInt64(left, right int64) int64 {
return rand.Int63n(left+right) - left
return mathRand.Int63n(left+right) - left
}

func init() {
s := rand.NewSource(time.Now().UnixNano())
Rand = rand.New(s)
s := mathRand.NewSource(time.Now().UnixNano())
Rand = mathRand.New(s)
}
Loading