Skip to content

Instantly share code, notes, and snippets.

@Clivern
Created April 27, 2021 12:06
Show Gist options
  • Save Clivern/5d1d7ba295a91a036301d6c99a0b275b to your computer and use it in GitHub Desktop.
Save Clivern/5d1d7ba295a91a036301d6c99a0b275b to your computer and use it in GitHub Desktop.
Validate github webhook signature
import hashlib
import hmac
class Signature(object):
"""Signature Validates Github Webhook Payload"""
def sign_request(self, webhook_secret, data):
"""Generate Payload Signature"""
message = bytes(data, 'utf-8')
secret = bytes(webhook_secret, 'utf-8')
hash = hmac.new(secret, message, hashlib.sha1)
return hash.hexdigest()
def validate_request(self, webhook_secret, data, signature):
"""Validate Payload Signature"""
sha_name, signature = signature.split('=')
if sha_name != 'sha1':
return False
return hmac.compare_digest(self.sign_request(webhook_secret, data), signature)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment