Files
Ydisks-Xianyu-Helper/internal/xianyu/protocol/sign.go
T
ChristandClaude Opus 4.8 980de41b53 chore: 重构配套改动 + 包文档 + 构建产物清理
automation 重构期间的配套修改:
- browser:订单抓取/密码登录/二维码刷新/stealth 等适配新流程,删 search.go
- db:cookies/orders_ext/extras/sessions/users/password/repository 等调整
- xianyu:mtop client、protocol(sign/decrypt/msgpack/cookies)、qrlogin、ws、ua
- server:admin/analytics/auth/cookie/helpers/item/item_publish_batch handlers
- frontend:App/Dashboard/ItemList/Rules/Sidebar/api/types,删 Keywords/TestPage
- auth、notify、webui、go.mod 等

本次会话补充:
- cmd/server、cmd/init-admin 改为 // Package main 开头的标准包文档
- .gitignore 加 internal/webui/static/assets/(hash 化构建产物不入库)
- internal/webui/static/index.html 重新构建

全量 go build/vet/test + race + 前端 test/build 通过。

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-30 21:41:03 +08:00

70 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package protocol
import (
"crypto/md5"
"crypto/rand"
"encoding/hex"
"fmt"
"math/big"
"sync/atomic"
"time"
)
// SignAppKey 是 mtop 签名使用的 appKey(与 WS 注册用的 appKey 不同)。
const SignAppKey = "34839810"
// GenerateSign 生成 mtop API 签名:MD5(token + "&" + t + "&" + appKey + "&" + data)。
func GenerateSign(t, token, data string) string {
msg := token + "&" + t + "&" + SignAppKey + "&" + data
// #nosec G401 -- MTOP 协议明确要求 MD5,不能替换为其他摘要算法。
sum := md5.Sum([]byte(msg))
return hex.EncodeToString(sum[:])
}
// GenerateMid 生成非密码学用途的消息 ID,形如 "<0-999随机><毫秒时间戳> 0"。
func GenerateMid() string {
randomPart := randomInt(1000)
ts := time.Now().UnixMilli()
return fmt.Sprintf("%d%d 0", randomPart, ts)
}
// GenerateUUID 生成 UUID,形如 "-<毫秒时间戳>1"。
func GenerateUUID() string {
return fmt.Sprintf("-%d1", time.Now().UnixMilli())
}
const deviceIDChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
// GenerateDeviceID 生成设备 ID:36 位 UUID 格式(位置 8/13/18/23 为 "-",14 为 "4",
// 19 取 (rand&0x3)|0x8),末尾追加 "-<userID>"。
func GenerateDeviceID(userID string) string {
result := make([]byte, 36)
for i := 0; i < 36; i++ {
switch {
case i == 8 || i == 13 || i == 18 || i == 23:
result[i] = '-'
case i == 14:
result[i] = '4'
case i == 19:
result[i] = deviceIDChars[(randomInt(16)&0x3)|0x8]
default:
result[i] = deviceIDChars[randomInt(16)]
}
}
return string(result) + "-" + userID
}
var randomFallback atomic.Uint64
// randomInt 返回 [0,max) 的密码学随机数;系统熵源异常时使用进程内单调计数兜底。
func randomInt(max int) int {
if max <= 1 {
return 0
}
n, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err == nil {
return int(n.Int64())
}
return int(randomFallback.Add(1) % uint64(max))
}