From 4cffc3d51090723bd582e31eef92d2d7ecaf99f9 Mon Sep 17 00:00:00 2001 From: 1 <1@72wo.cn> Date: Sun, 21 Dec 2025 17:22:43 +0000 Subject: [PATCH] =?UTF-8?q?refactor(login):=20=E4=BD=BF=E7=94=A8gf/grand?= =?UTF-8?q?=E6=9B=BF=E4=BB=A3math/rand=E7=94=9F=E6=88=90=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?ID=E9=9A=8F=E6=9C=BA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/blazing/service/login.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/modules/blazing/service/login.go b/modules/blazing/service/login.go index 978aa3583..fff092c7a 100644 --- a/modules/blazing/service/login.go +++ b/modules/blazing/service/login.go @@ -6,9 +6,9 @@ import ( "encoding/binary" "encoding/hex" - "math/rand" "strings" + "github.com/gogf/gf/v2/util/grand" "github.com/google/uuid" ) @@ -44,12 +44,6 @@ func (s *LoginService) GetSessionId(accountID uint) (string, string, error) { return "", "", err } - // 生成4字节安全随机数(使用crypto/rand提高随机性,适合会话ID场景) - randomBytes := make([]byte, 4) - if _, err := rand.Read(randomBytes); err != nil { - return "", "", err - } - // 将accountID转换为4字节大端序字节数组 accountBytes := make([]byte, 4) binary.BigEndian.PutUint32(accountBytes, uint32(accountID)) @@ -58,7 +52,7 @@ func (s *LoginService) GetSessionId(accountID uint) (string, string, error) { sessionBytes := make([]byte, 0, 24) sessionBytes = append(sessionBytes, accountBytes...) sessionBytes = append(sessionBytes, uuidBytes...) - sessionBytes = append(sessionBytes, randomBytes...) + sessionBytes = append(sessionBytes, grand.B(4)...) // 编码为十六进制字符串作为最终会话ID sessionID := hex.EncodeToString(sessionBytes)