23 lines
608 B
Python
23 lines
608 B
Python
# -*- coding: utf-8 -*-
|
||
"""OpenAPI 签名:空值不参与,字典序,MD5 小写。"""
|
||
from __future__ import annotations
|
||
|
||
import hashlib
|
||
from typing import Any, Mapping
|
||
|
||
|
||
def build_sign(params: Mapping[str, Any], secret_key: str) -> str:
|
||
items = []
|
||
for k in sorted(params.keys()):
|
||
if k == "Sign":
|
||
continue
|
||
v = params[k]
|
||
if v is None:
|
||
continue
|
||
s = str(v)
|
||
if s == "":
|
||
continue
|
||
items.append(f"{k}={s}")
|
||
raw = "&".join(items) + f"&SecretKey={secret_key}"
|
||
return hashlib.md5(raw.encode("utf-8")).hexdigest()
|