Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created September 14, 2017 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/946ea887e2d21b950e20972c2f4f7139 to your computer and use it in GitHub Desktop.
Save joyrexus/946ea887e2d21b950e20972c2f4f7139 to your computer and use it in GitHub Desktop.
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