Skip to content

Instantly share code, notes, and snippets.

@aadibajpai
Created June 20, 2019 00:24
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 aadibajpai/dbaf51861008f58d884b2ebda3002d3c to your computer and use it in GitHub Desktop.
Save aadibajpai/dbaf51861008f58d884b2ebda3002d3c to your computer and use it in GitHub Desktop.
Python function to validate GitHub Webhook
import hmac
import hashlib
def is_valid_signature(x_hub_signature, data, private_key):
# x_hub_signature and data are from the webhook payload
# private key is your webhook secret
hash_algorithm, github_signature = x_hub_signature.split('=', 1)
algorithm = hashlib.__dict__.get(hash_algorithm)
encoded_key = bytes(private_key, 'latin-1')
mac = hmac.new(encoded_key, msg=data, digestmod=algorithm)
return hmac.compare_digest(mac.hexdigest(), github_signature)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment