import { createHmac, timingSafeEqual } from "node:crypto"; export function expectedSig(body: Buffer | string, secret: string): string { const mac = createHmac("sha256", secret); mac.update(body); return "sha256=" + mac.digest("hex"); } export function verifySignature( rawBody: Buffer, header: string | undefined, secret: string, ): boolean { if (!header) return false; const expected = expectedSig(rawBody, secret); const a = Buffer.from(header, "utf8"); const b = Buffer.from(expected, "utf8"); if (a.length !== b.length) return false; return timingSafeEqual(a, b); }