Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
make hmac cli
#!/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
You can’t perform that action at this time.