import hashlib
import hmac
from urllib.parse import urlparse
def calculate_request_signature(url: str, request_json: str, secret: str) -> str:
parsed_url = urlparse(url)
signature_string = request_json + parsed_url.path + parsed_url.query
signature = hmac.new(secret.encode('utf-8'), signature_string.encode('utf-8'), hashlib.sha256).hexdigest()
return signature
def calculate_response_signature(response_json: str, secret: str) -> str:
signature = hmac.new(secret.encode('utf-8'), response_json.encode('utf-8'), hashlib.sha256).hexdigest()
return signature
request = """{
"hmId":"0c3a5f71-8fc1-4dde-8f75-38d04730680f",
"amount":"1500.00",
"currency":"RUB",
"currencyRate":"97.34"
}"""
url = "https://example.com/hm/v1/payments/card"
secret = "secret-key"
print(calculate_request_signature(url, request, secret))
response = """{
"id": "payment-id-123",
"cardNumber": "123123123",
"owner": "John Doe",
"bankName": "BankOfAmerica"
}"""
print(calculate_response_signature(response, secret))