Created
September 14, 2017 15:26
-
-
Save joyrexus/946ea887e2d21b950e20972c2f4f7139 to your computer and use it in GitHub Desktop.
make hmac cli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
''' | |
A little script for generating HMACs. | |
> ./make_hmac.py SECRET BODY | |
HMAC | |
For example ... | |
> ./make_hmac.py secret foo | |
dzukRpPHVT1u4g9h6l0nV6mk9KRNKEGuTpW1LkzWLbQ= | |
''' | |
import sys, hashlib, base64, hmac | |
def make_hmac(secret, body): | |
""" | |
Calculate the HMAC value of the given request body and secret | |
as per Shopify's documentation for Webhook requests. | |
http://docs.shopify.com/api/tutorials/using-webhooks#verify-webhook | |
""" | |
digest = hmac.new(secret, body, hashlib.sha256).digest() | |
return base64.b64encode(digest) | |
def is_valid(HMAC, secret, body): | |
""" | |
Check if the HMAC matches that calculated from the given request | |
body and secret. | |
""" | |
return HMAC == make_hmac(secret, body) | |
if __name__ == '__main__': | |
[secret, body] = sys.argv[1:3] | |
print make_hmac(secret, body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment