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)