Created
March 8, 2021 19:04
-
-
Save robindiddams/a0db1051baf7820d0d031cee7e46f24c to your computer and use it in GitHub Desktop.
How to validate a github webhook in pure Javascript using crypto.
This file contains hidden or 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
const crypto = require('crypto') | |
const webhookSecret = 'my-webhook-secret-that-i-put-in-github'; | |
/** | |
* | |
* @param rawBody {Buffer} the raw payload of the request | |
* @param sig {string} the x-hub-signature-256 header value | |
* @returns true if the signature matches | |
*/ | |
const validGithubSignature = (rawBody, sig) => { | |
const hmac = crypto.createHmac('sha256', webhookSecret'); | |
hmac.update(rawBody); | |
const digest = hmac.digest('hex'); | |
return sig === `sha256=${digest}`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment