Skip to content

Instantly share code, notes, and snippets.

@robindiddams
Created March 8, 2021 19:04
Show Gist options
  • Save robindiddams/a0db1051baf7820d0d031cee7e46f24c to your computer and use it in GitHub Desktop.
Save robindiddams/a0db1051baf7820d0d031cee7e46f24c to your computer and use it in GitHub Desktop.
How to validate a github webhook in pure Javascript using crypto.
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