open-pay-demo/python/sign.py
echo 653a5e4a23 init: multi-language OpenAPI demos (java/php/nodejs/python)
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-20 13:40:28 +08:00

23 lines
608 B
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.

# -*- 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()