Created
June 20, 2019 00:24
-
-
Save aadibajpai/dbaf51861008f58d884b2ebda3002d3c to your computer and use it in GitHub Desktop.
Python function to validate GitHub Webhook
This file contains 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
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