-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.py
63 lines (50 loc) · 1.68 KB
/
utils.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
import string
import rsa
import base64
from urllib.parse import quote_plus, unquote
SIGN_TYPE = "SHA-256"
def order_data(payload):
lst = []
for key, value in payload.items():
lst.append("{}={}".format(key, value))
lst.sort()
order_payload = "&".join(lst)
return order_payload
def remove_order_data(payload):
lst = []
for key, value in payload.items():
if key == 'sign' or key == 'sign_type':
pass
else:
lst.append("{}={}".format(key, value))
lst.sort()
order_payload = "&".join(lst)
return order_payload
def sign(payload, private_key=None):
private_key = rsa.PrivateKey._load_pkcs1_pem(private_key)
sign = rsa.sign(payload.encode('utf-8'), private_key, SIGN_TYPE)
b64sing = base64.encodebytes(sign).decode("utf8").replace("\n", "")
return b64sing
def urlencode_data(payload, sign):
lst = []
for key, value in payload.items():
lst.append("{}={}".format(
key, quote_plus(value, encoding='utf-8')))
lst.sort()
order_payload = "&".join(lst)
if sign:
order_payload += "&sign=%s" % quote_plus(sign, encoding='utf-8')
return order_payload
def check_sign(payload, sign, public_key=None):
sign = base64.b64decode(sign)
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(public_key)
return rsa.verify(payload.encode('utf-8'), sign, pubkey)
def check_ali_sign(payload, sign, alipay_public_key=None):
sign = base64.b64decode(sign)
pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(alipay_public_key)
res = False
try:
res = rsa.verify(payload.encode('utf-8'), sign, pubkey)
except Exception as e:
res = False
return res