From ae79b43cdb1fdcec772e9c411bb81243cae1de0a Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Sun, 21 Sep 2025 17:59:17 +0200 Subject: [PATCH] security fix: Use of insufficient randomness as the key of a cryptographic algorithm --- util/random/random.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/util/random/random.go b/util/random/random.go index 9610e26c..c746df63 100644 --- a/util/random/random.go +++ b/util/random/random.go @@ -2,7 +2,8 @@ package random import ( - "math/rand" + "crypto/rand" + "math/big" ) var ( @@ -40,12 +41,21 @@ func init() { func Seq(n int) string { runes := make([]rune, n) for i := 0; i < n; i++ { - runes[i] = allSeq[rand.Intn(len(allSeq))] + idx, err := rand.Int(rand.Reader, big.NewInt(int64(len(allSeq)))) + if err != nil { + panic("crypto/rand failed: " + err.Error()) + } + runes[i] = allSeq[idx.Int64()] } return string(runes) } // Num generates a random integer between 0 and n-1. func Num(n int) int { - return rand.Intn(n) + bn := big.NewInt(int64(n)) + r, err := rand.Int(rand.Reader, bn) + if err != nil { + panic("crypto/rand failed: " + err.Error()) + } + return int(r.Int64()) }