Verify GitHub webhook signatures: X-Hub-Signature-256
GitHub hex-encodes the HMAC-SHA256 of the raw request body and sends it as X-Hub-Signature-256: sha256=<hex>.
Header and signed content
A GitHub webhook request can carry up to two signature headers. New hooks send X-Hub-Signature-256 with sha256= followed by 64 hex characters. Older hooks may send the SHA-1 X-Hub-Signature.
| Header | Algorithm | Value |
|---|---|---|
| X-Hub-Signature-256 | HMAC-SHA256 | sha256=<64 hex> |
| X-Hub-Signature | HMAC-SHA1 (legacy) | sha1=<40 hex> |
The signed content is the body alone. No timestamp or event name is included, so the same body always yields the same signature and a signature alone cannot stop a replay.
Node.js verification
const crypto = require("crypto");
app.post("/webhooks/github", express.raw({ type: "*/*" }), (req, res) => {
const raw = req.body; // Buffer, not parsed JSON
const header = req.get("X-Hub-Signature-256") || "";
const expected = "sha256=" + crypto
.createHmac("sha256", process.env.GITHUB_WEBHOOK_SECRET)
.update(raw)
.digest("hex");
const ok = header.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(header), Buffer.from(expected));
if (!ok) return res.status(401).send("invalid signature");
res.status(200).send("ok"); // queue the work, respond fast
});Python verification
import hmac, hashlib
from flask import request, abort
@app.post("/webhooks/github")
def github_webhook():
raw = request.get_data() # raw bytes, before JSON parsing
header = request.headers.get("X-Hub-Signature-256", "")
expected = "sha256=" + hmac.new(
SECRET, raw, hashlib.sha256
).hexdigest()
if not hmac.compare_digest(expected, header):
abort(401)
return "ok", 200Why you get a 401
- A global
express.json()consumed the body first. - Verification rebuilt the body with
JSON.stringify(req.body). - A newline or space was copied with the secret.
- A proxy changed bytes while decompressing the body.
- The request came from a test tool, not from GitHub.
The body diagnostics here report byte count and CRLF, and the variant matcher tells you which mutation would make the signature match.
Rotating the secret
Changing the secret invalidates every signature made with the old one. GitHub starts signing with the new secret, so allow both briefly during deployment. Keep returning 401 rather than 5xx for verification failures so GitHub's retry policy behaves correctly.
Enter the secret and body to compute the signature GitHub must have sent.
Open the signature verifierFrequently asked questions
What else should I check besides the signature?
Check X-GitHub-Event to decide whether the event is relevant, and deduplicate with X-GitHub-Delivery. The signature does not prevent replays.
Is the ping event signed?
Yes. The ping sent when a hook is created is signed the same way. Verify it even if you do not process it.
Does GitHub Enterprise Server work the same way?
Yes, the signing scheme is identical. Just confirm your internal proxy is not rewriting the body.
This guide follows the signing schemes published by GitHub, Stripe, Slack, Shopify and Toss Payments.
← Guides