用于生成和验证 HMAC-SHA256 请求签名的工具库
Project description
api-signature
用于生成和验证基于 HMAC-SHA256 的 API 请求签名的轻量工具库。
安装 ✅
pip install api-signature
简要说明 🔧
- 签名基于 HMAC-SHA256,默认返回 hex(大写) 字符串(通过
hmac_hash可选择返回 Base64)。 - 支持将请求中的
query(字典或字符串)格式化为以?开头的查询字符串。 - 提供生成签名、生成用于
Authorization/自定义 header 的签名头,以及验证签名/签名头的工具。
公共函数(简短说明) 📚
-
generate_signature(appid, secret_key, url, method='GET', body=None, query=None, timestamp=None, nonce=None, ends_with_secret_key=False):生成原始签名字符串与签名(返回raw_str,signature,url)。 -
generate_signature_header(appid, secret_key, url, method='GET', body=None, query=None, ends_with_secret_key=False, with_hash_name=True, pair_value=False):在generate_signature基础上生成用于 HTTP header 的签名字符串(可选带HMAC-SHA256前缀或键值对形式)。 -
verify_signature(appid, secret_key, url, method='GET', body=None, query=None, timestamp=None, nonce=None, ends_with_secret_key=False, verify_timestamp=True, timestamp_valid_time=300, signature=''):验证签名是否匹配并可选校验时间戳,返回code/message等信息。 -
verify_signature_header(url, method='GET', body=None, query=None, timestamp=None, nonce=None, ends_with_secret_key=False, verify_timestamp=True, timestamp_valid_time=300, signature='', verify_hash_name=True, with_hash_name=True, pair_value=False, header_value='', get_secretkey_by_appid=None):解析并验证签名头,get_secretkey_by_appid为异步函数,接收appid并返回对应的secret_key。
示例 ✨
import asyncio
from api_signature import (
generate_signature,
generate_signature_header,
verify_signature,
verify_signature_header,
)
appid = "appid"
secret_key = "secret_key"
async def main():
# 生成签名(GET)
sig = await generate_signature(appid, secret_key, "/path", "GET")
print("generate_signature ->", sig)
# 生成带 header 的签名(POST)
header = await generate_signature_header(
appid, secret_key, "/path", "POST", {"a": "a", "b": 1}, {"_t": "1234567890"}
)
print("generate_signature_header ->", header)
# 验证签名(直接传 signature)
vs = await verify_signature(
url="/path",
appid=appid,
secret_key=secret_key,
nonce="IsJRqILi",
timestamp="1766335905",
signature="FCAA034551D699945D5B6FD44562D977B0E924DDE8DA4732D5E8F53A3B929141",
)
print("verify_signature ->", vs)
# 验证签名头(需提供异步回调根据 appid 获取 secret key)
async def get_secret(appid_):
return secret_key
vsh = await verify_signature_header(
url="/path",
method="POST",
body={"a": "a", "b": 1},
query={"_t": "1234567890"},
header_value="HMAC-SHA256 appid:1766336579:Ob0Fy5Xd:D6F3973B00B7463EDA081284775D4770B60430E620E9C804D8B267290AC5BD4D",
get_secretkey_by_appid=get_secret,
)
print("verify_signature_header ->", vsh)
asyncio.run(main())
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file api_signature-0.0.2.tar.gz.
File metadata
- Download URL: api_signature-0.0.2.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
064c33ef70da80ae157c23f2985cf978eba95368b109c2ed2bb681802155cf13
|
|
| MD5 |
0975893bb1cac2a4eaa21259db251c52
|
|
| BLAKE2b-256 |
f60f9c93a22f20c43016346bcfbcb7528a627af4019456028b9467e899883c62
|
File details
Details for the file api_signature-0.0.2-py3-none-any.whl.
File metadata
- Download URL: api_signature-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9f505269d2845772cc1d82e83e6089284344197d461eb71649c4164a73d1a86
|
|
| MD5 |
88eeda1ec516663675e22cd0ea72db67
|
|
| BLAKE2b-256 |
67ccb364bd3410fad6558940f8a18093f5ac4f9566b930934f5ad8fddab3aa53
|