workspace— step2

This commit is contained in:
2026-06-09 17:44:07 +08:00
parent 7eb3647539
commit 4e591b07eb
7 changed files with 29 additions and 28 deletions
-8
View File
@@ -1,8 +0,0 @@
def is_valid_str(s):
"""判断字符串是否有效,即非空且非空格。"""
return isinstance(s, str) and len(s.strip()) > 0
def is_empty_str(s):
"""判断字符串是否为空,即空或仅包含空格。"""
return isinstance(s, str) and len(s.strip()) == 0
-38
View File
@@ -1,38 +0,0 @@
import json
import time
import hmac
import secrets
import hashlib
from typing import Dict, Any
def generate_signed_headers(app_id: str, app_secret: str, data: Dict[str, Any], method: str = "POST") -> Dict[str, str]:
timestamp = str(int(time.time() * 1000))
nonce = secrets.token_hex(16)
payload_str = json.dumps(data, ensure_ascii=False)
payload_bytes = payload_str.encode("utf-8")
actual_body_hash = hashlib.sha256(payload_bytes).hexdigest()
string_to_sign = "\n".join([
method.upper(),
app_id,
timestamp,
nonce,
actual_body_hash
])
signature = hmac.new(
app_secret.encode("utf-8"),
string_to_sign.encode("utf-8"),
hashlib.sha256
).hexdigest()
headers = {
"X-Ca-Appid": app_id,
"X-Ca-Timestamp": timestamp,
"X-Ca-Nonce": nonce,
"X-Ca-Signature": signature,
"Content-Type": "application/json; charset=utf-8"
}
return headers