refactor(login): 使用gf/grand替代math/rand生成会话ID随机数
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user