Create generate_signature.py
Browse files- generate_signature.py +28 -0
generate_signature.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import hashlib
|
3 |
+
import time
|
4 |
+
from cryptography.hazmat.primitives import hashes, serialization
|
5 |
+
from cryptography.hazmat.backends import default_backend
|
6 |
+
from cryptography.hazmat.primitives.asymmetric import padding
|
7 |
+
|
8 |
+
def generate_signature(method, url, body, app_id, private_key_path):
|
9 |
+
method_str = method.upper()
|
10 |
+
url_str = url
|
11 |
+
timestamp = str(int(time.time()))
|
12 |
+
nonce_str = hashlib.md5(timestamp.encode()).hexdigest()
|
13 |
+
body_str = body
|
14 |
+
to_sign = f"{method_str}\n{url_str}\n{timestamp}\n{nonce_str}\n{body_str}"
|
15 |
+
with open(private_key_path, "rb") as key_file:
|
16 |
+
private_key = serialization.load_pem_private_key(
|
17 |
+
key_file.read(),
|
18 |
+
password=None,
|
19 |
+
backend=default_backend()
|
20 |
+
)
|
21 |
+
signature = private_key.sign(
|
22 |
+
to_sign.encode(),
|
23 |
+
padding.PKCS1v15(),
|
24 |
+
hashes.SHA256()
|
25 |
+
)
|
26 |
+
signature_base64 = base64.b64encode(signature).decode()
|
27 |
+
auth_header = f"TAMS-SHA256-RSA app_id={app_id},nonce_str={nonce_str},timestamp={timestamp},signature={signature_base64}"
|
28 |
+
return auth_header
|