ksjsb/core/sig.py
2026-07-30 20:25:56 +08:00

38 lines
1.1 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.

"""`sig` and related MD5 helpers."""
from __future__ import annotations
import hashlib
# libcore.so 解码出的静态 app salt同 app 版本固定。
SIG_SALT = "772867c19925"
def build_sig_plaintext(params: dict) -> bytes:
"""构造 sig 明文。
参数集是请求的全部非签名参数。FormBody 请求会把 body 参数一起纳入
URL `sig`;业务字段 `sign` 不属于 URL 签名参数,必须保留参与拼接。
"""
skip = {"sig", "sig2"}
parts = []
for key in sorted(params.keys()):
if key in skip or key.startswith("__NS"):
continue
parts.append(f"{key}={params[key]}")
return "".join(parts).encode("utf-8")
def sig(params: dict, salt: str = SIG_SALT) -> str:
"""`sig = MD5(build_sig_plaintext(params) + salt)`."""
return hashlib.md5(build_sig_plaintext(params) + salt.encode()).hexdigest()
def body_md5(body: bytes) -> str:
"""`bodyMd5 = MD5(body)`,用于 `sig2` 输入。"""
return hashlib.md5(body).hexdigest()
__all__ = ["SIG_SALT", "body_md5", "build_sig_plaintext", "sig"]