forked from fzls/djc_helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.py
77 lines (58 loc) · 2.03 KB
/
encrypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from __future__ import annotations
import base64
import hashlib
import hmac
from typing import Any
from urllib.parse import quote_plus
def make_dnf_helper_signature(
http_method: str,
api_path: str,
post_data: str,
secret: str,
) -> str:
data = "&".join([http_method, quote_plus(api_path), quote_plus(post_data)])
hash_bytes = hmac.new(secret.encode(), data.encode(), hashlib.sha1).digest()
signature = base64.b64encode(hash_bytes).decode()
# 对于get请求,由于签名字段要放到query string中,需要做下url编码
if http_method == "GET":
signature = quote_plus(signature)
return signature
def make_dnf_helper_signature_data(data: dict[str, Any]) -> str:
# 取出keys
keys = list(data.keys())
# 升序排序
keys.sort()
# 按顺序拼接成字符串
results = []
for key in keys:
results.append(f"{key}={data[key]}")
final_result = "&".join(results)
return final_result
if __name__ == "__main__":
from urllib.parse import parse_qsl
query_string = "userId=504051073&gameId=1006&sPartition=11&sRoleId=71672841&game_code=dnf&token=wvtY7ern&uin=1054073896&uniqueRoleId=3482436497&openid=&appOpenid=4B92C052573D369D37CF9408B9112DA2&appidTask=1000042&cRand=1660746760049&tghappid=1000045"
query_data = dict(parse_qsl(query_string, keep_blank_values=True))
# query_data = {
# "userId": "504051073",
# "gameId": 1006,
# "sPartition": "11",
# "sRoleId": "71672841",
# "game_code": "dnf",
# "token": "wvtY7ern",
# "uin": "1054073896",
# "uniqueRoleId": "3482436497",
# "openid": "",
# "appOpenid": "4B92C052573D369D37CF9408B9112DA2",
# "appidTask": 1000042,
# "cRand": 1660746760049,
# "tghappid": 1000045
# }
data = make_dnf_helper_signature_data(query_data)
print(
make_dnf_helper_signature(
"GET",
"/peak/list/basic",
data,
"nKJH89hh@8yoHJ98y&IOhIUt9hbOh98ht",
)
)