Skip to content

Instantly share code, notes, and snippets.

@mahdyar
Created August 13, 2021 10:09
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahdyar/711beee9fec9cab6bb2f6e48d061d077 to your computer and use it in GitHub Desktop.
Save mahdyar/711beee9fec9cab6bb2f6e48d061d077 to your computer and use it in GitHub Desktop.
Verify GitHub webhook request sha256 in PHP
<?php
// It's better to be exported as an environment variable or in a .env file.
define("SECRET", "<SECRET>");
$body = file_get_contents("php://input");
// $decodedBody = json_decode(urldecode($body));
if (verifySignature($body) !== false) {
// verified
} else {
// unverified
http_response_code(403);
echo "unauthorized";
}
function verifySignature($body){
$headers = getallheaders();
return hash_equals('sha256='.hash_hmac('sha256', $body, SECRET), $headers['x-hub-signature-256']);
}
@utilmind
Copy link

utilmind commented Oct 9, 2022

Thank you!

@mahdyar
Copy link
Author

mahdyar commented Oct 9, 2022

Thank you!

You're welcome! You can thank me better by starring the gist though. ;)

@gaborszita
Copy link

Thank you!

For me, it only worked if the header was in the correct case: X-Hub-Signature-256. So, if anyone is having issues, try this.

@cgaldiolo-splashlight
Copy link

For me, it only worked if the header was in the correct case: X-Hub-Signature-256. So, if anyone is having issues, try this.

Thank you @gaborszita!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment