Skip to content

Verify signatures

After this page you can verify every delivery in Node, Python or Go. Do it before you trust the body.

Each request carries one header:

CountViews-Signature: t=1760954400,v1=3c1f…e9a2

t is the Unix time in seconds the attempt was signed. Each v1 is a hex HMAC-SHA256.

  1. Read the raw body bytes, before any JSON parsing. A parsed and re-serialised body is a different string and will not match.
  2. Parse the header: split on ,, then on =. Take t and every v1. No t, a non-integer t or no v1 means the header is malformed: reject.
  3. Reject if |now - t| > 300 seconds. This stops replays of a captured request.
  4. Compute HMAC-SHA256(secret, "<t>.<raw body>") as hex.
  5. Accept if any v1 equals it, compared in constant time. During a secret rotation a delivery has two.
import { createHmac, timingSafeEqual } from "node:crypto";
export function verify(secret, header, rawBody, nowSeconds = Date.now() / 1000, tolerance = 300) {
const parts = header.split(",").map((p) => p.trim().split("="));
const t = Number(parts.find(([k]) => k === "t")?.[1]);
const given = parts.filter(([k, v]) => k === "v1" && v).map(([, v]) => v);
if (!Number.isInteger(t) || given.length === 0) return "malformed";
if (Math.abs(nowSeconds - t) > tolerance) return "stale";
const expected = createHmac("sha256", secret).update(`${t}.${rawBody}`).digest();
const ok = given.some((hex) => {
const buf = Buffer.from(hex, "hex");
return buf.length === expected.length && timingSafeEqual(buf, expected);
});
return ok ? "valid" : "mismatch";
}
// Express: app.post("/hooks", express.raw({ type: "application/json" }), (req, res) => {
// const verdict = verify(process.env.WHSEC, req.get("countviews-signature") ?? "", req.body.toString("utf8"));
// if (verdict !== "valid") return res.sendStatus(400);
// res.sendStatus(200);
// });

Answer 400 for anything but valid. A stale verdict on a fresh request usually means your server clock is off.

Paste a secret, the raw body and the header to see which step fails. It runs in your browser and sends nothing.

Verdict

Paste the three values, or generate a sample.

Everything runs in this browser tab with WebCrypto. The secret, the body and the header are not sent anywhere.