Python SDK for DERO Merchant REST API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.2KB

  1. import hmac
  2. import hashlib
  3. import binascii
  4. def sign_message(message: str, key: str) -> str:
  5. """Signs a message with a key.
  6. Args:
  7. message:
  8. String of the message we want to sign.
  9. key:
  10. String of the SHA256 hex encoded key we want to use to sign the message.
  11. Returns:
  12. A string containing the SHA256 hex encoded signature of the message.
  13. """
  14. key_bytes = binascii.unhexlify(key)
  15. message_bytes = message.encode()
  16. return hmac.new(key_bytes, message_bytes, hashlib.sha256).hexdigest()
  17. def valid_mac(message: str, message_mac: str, key: str) -> bool:
  18. """Verifies if the signature of a message is valid.
  19. Args:
  20. message:
  21. String of the message we want to verify.
  22. message_mac:
  23. String of the SHA256 hex encoded signature of the message we want to verify.
  24. key:
  25. String of the SHA256 hex encoded key used to sign the message.
  26. Returns:
  27. A bool with the validity of the signature.
  28. """
  29. signed_message = sign_message(message, key)
  30. return hmac.compare_digest(bytearray.fromhex(message_mac), bytearray.fromhex(signed_message))