Files
MusicServer/app/sdk/platforms/tx/utils/crypto.py
T
snowy-yang dfc06bf4de feat(python): 完成 Python 全量迁移——剩余平台、登录通道与公共层修复
- 新增 5sing/apple/xm/me/qt/lr 平台包与取流实现(含 xm-sign、MissEvan 签名、蜻蜓 HMAC、Apple developer token)
- 新增 wy/kg/tx/lr 登录:扫码、手机号、账密(kg 20028 风控指纹)、cookie 绑定、kg/tx 续期
- request 层:保留多值 Set-Cookie;JSON body 紧凑化(修复 kg 网关验签);直连不经系统代理(对齐 Node)
- 登录路由统一 Body(embed=True),修复扫码轮询 422;lifespan 接入登录续期调度
- ruff/format/pyright 全绿;14 平台真实联网与 HTTP 路由验证通过
2026-10-23 21:55:45 +08:00

57 lines
1.6 KiB
Python
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.
"""QQ音乐 musics.fcg 签名(移植自 sdk/tx/utils/crypto.ts)。
算法:SHA1 摘要的十六进制串 → 按固定下标取两段字符 → 第三段用固定混淆表
与摘要每字节异或后 base64(剔除 + / = 与反斜杠)→ 拼成 `zzc{part1}{b64}{part2}` 转小写。
"""
from __future__ import annotations
import base64
import hashlib
import re
PART_1_INDEXES = (23, 14, 6, 36, 16, 40, 7, 19)
PART_2_INDEXES = (16, 1, 32, 12, 19, 27, 8, 5)
SCRAMBLE_VALUES = (
89,
39,
179,
150,
218,
82,
58,
252,
177,
52,
186,
123,
120,
64,
242,
133,
143,
161,
121,
179,
)
_B64_STRIP = re.compile(r"[\\/+=]")
def _pick_by_indexes(hash_hex: str, indexes: tuple[int, ...]) -> str:
"""按固定下标取字符。
PART_1_INDEXES 含下标 40,而 SHA1 十六进制串正好 40 字符(合法下标 0–39)。
TS 侧 `hash[40]` 取到 undefined,`join('')` 会把它并成空串;Python 直接下标会抛
IndexError,因此这里显式跳过越界下标,保持与上游签名逐字节一致。
"""
return "".join(hash_hex[index] for index in indexes if 0 <= index < len(hash_hex))
def zzc_sign(text: str) -> str:
hash_hex = hashlib.sha1(text.encode()).hexdigest()
part1 = _pick_by_indexes(hash_hex, PART_1_INDEXES)
part2 = _pick_by_indexes(hash_hex, PART_2_INDEXES)
scrambled = bytes(value ^ int(hash_hex[i * 2 : i * 2 + 2], 16) for i, value in enumerate(SCRAMBLE_VALUES))
part3 = _B64_STRIP.sub("", base64.b64encode(scrambled).decode())
return f"zzc{part1}{part3}{part2}".lower()