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…e9a2t is the Unix time in seconds the attempt was signed. Each v1 is a hex HMAC-SHA256.
The rule
Section titled “The rule”- Read the raw body bytes, before any JSON parsing. A parsed and re-serialised body is a different string and will not match.
- Parse the header: split on
,, then on=. Taketand everyv1. Not, a non-integertor nov1means the header is malformed: reject. - Reject if
|now - t| > 300seconds. This stops replays of a captured request. - Compute
HMAC-SHA256(secret, "<t>.<raw body>")as hex. - Accept if any
v1equals 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);// });import hashlib, hmac, time
def verify(secret: str, header: str, raw_body: bytes, tolerance: int = 300) -> str: parts = [p.strip().split("=", 1) for p in header.split(",")] t = next((v for k, v in parts if k == "t"), None) given = [v for k, v in parts if k == "v1" and v] if t is None or not t.lstrip("-").isdigit() or not given: return "malformed" if abs(time.time() - int(t)) > tolerance: return "stale" expected = hmac.new(secret.encode(), f"{t}.".encode() + raw_body, hashlib.sha256).hexdigest() ok = any(hmac.compare_digest(g, expected) for g in given) return "valid" if ok else "mismatch"
# Flask: verify(SECRET, request.headers.get("CountViews-Signature", ""), request.get_data())package hooks
import ( "crypto/hmac" "crypto/sha256" "encoding/hex" "math" "strconv" "strings" "time")
func Verify(secret, header string, rawBody []byte, tolerance float64) string { var t int64 var haveT bool var given []string for _, part := range strings.Split(header, ",") { kv := strings.SplitN(strings.TrimSpace(part), "=", 2) if len(kv) != 2 { continue } switch kv[0] { case "t": if n, err := strconv.ParseInt(kv[1], 10, 64); err == nil { t, haveT = n, true } case "v1": given = append(given, kv[1]) } } if !haveT || len(given) == 0 { return "malformed" } if math.Abs(float64(time.Now().Unix()-t)) > tolerance { return "stale" } mac := hmac.New(sha256.New, []byte(secret)) mac.Write([]byte(strconv.FormatInt(t, 10) + ".")) mac.Write(rawBody) expected := mac.Sum(nil) for _, g := range given { if b, err := hex.DecodeString(g); err == nil && hmac.Equal(b, expected) { return "valid" } } return "mismatch"}Answer 400 for anything but valid. A stale verdict on a fresh request usually means your server clock is off.
Try it
Section titled “Try it”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.