npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@p-vbordei/hmac-sign

v0.2.1

Published

HMAC-SHA256 signing and timing-safe verification for webhooks, with optional replay protection. Zero dependencies (Node crypto only).

Readme

hmac-sign

ci

npm downloads bundle

HMAC-SHA256 signing and timing-safe verification for webhooks. Two flavors — with timestamp (Stripe-style, replay-protected) and bare (GitHub-style). Zero dependencies; uses Node's built-in crypto.

import { sign, verify, signSimple, verifySimple } from "@p-vbordei/hmac-sign";

// Stripe-style: timestamp inside the signed input
const { header } = sign(body, SECRET);
// → "t=1716120000000,v1=c0fee..."

const r = verify(body, header, SECRET);
if (!r.valid) throw new Error(r.reason);

// GitHub-style: bare signature
const sigHeader = signSimple(body, SECRET);
// → "sha256=c0fee..."

if (!verifySimple(body, SECRET, sigHeader)) throw new Error("bad signature");

Install

npm install @p-vbordei/hmac-sign

Node 20+. Uses node:crypto.

Why

Most webhook-signing snippets you find online skip critical details:

  • They use === for signature comparison → leaks timing info, attacker can brute-force byte by byte.
  • They forget to include a timestamp in the signed input → susceptible to replay attacks.
  • They Buffer-handle incorrectly → comparison fails for legitimate signatures on certain inputs.

hmac-sign is the minimum correct version: timing-safe comparison, replay protection via timestamp tolerance, Buffer-handling done right.

Recipes

Signing an outgoing webhook (sender)

import { sign } from "@p-vbordei/hmac-sign";

async function sendWebhook(url: string, event: object) {
  const body = JSON.stringify(event);
  const { header } = sign(body, process.env.WEBHOOK_SECRET!);

  await fetch(url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-Signature": header,
    },
    body,
  });
}

Verifying an incoming webhook (receiver)

import { verify } from "@p-vbordei/hmac-sign";

async function handleWebhook(req: Request) {
  const sig = req.headers.get("x-signature");
  const body = await req.text();

  if (!sig) return new Response("missing signature", { status: 400 });

  const r = verify(body, sig, process.env.WEBHOOK_SECRET!);
  if (!r.valid) {
    console.warn(`webhook rejected: ${r.reason}`);
    return new Response("invalid signature", { status: 401 });
  }

  const event = JSON.parse(body);
  await processEvent(event);
  return new Response("ok");
}

GitHub-style verification (no timestamp)

import { verifySimple } from "@p-vbordei/hmac-sign";

const sig = req.headers.get("x-hub-signature-256");  // "sha256=<hex>"
if (!verifySimple(rawBody, GITHUB_SECRET, sig!)) {
  return new Response("forbidden", { status: 403 });
}

Custom tolerance window for clock skew

import { verify } from "@p-vbordei/hmac-sign";

const r = verify(body, header, SECRET, {
  toleranceMs: 30_000,  // tighter window — 30 seconds (default is 5 min)
});

Inject a clock for tests

import { verify } from "@p-vbordei/hmac-sign";

const fixedNow = 1716120000_000;
verify(body, header, SECRET, { now: () => fixedNow });

API

Timestamped (recommended for outbound webhooks)

sign(payload: string | Buffer, secret: string, opts?: { timestamp?: number })
  → { header: "t=<ms>,v1=<hex>", timestamp, signature }

verify(payload, header, secret, opts?: { toleranceMs?: number; now?: () => number })
  → { valid: true, timestamp } | { valid: false, reason }
  • The signed input is <unix-ms>.<payload>, so altering the timestamp invalidates the signature.
  • verify rejects anything outside toleranceMs (default 5 min) — replay protection.
  • Comparison is timing-safe (crypto.timingSafeEqual).

Bare (compatible with GitHub-style sha256=… headers)

signSimple(payload, secret) → "sha256=<hex>"
verifySimple(payload, secret, signature) → boolean

Use this when integrating with a service that already specifies its own signature format and no timestamp is desired.

Caveats

  • Buffer vs string. Both work, but the input must be byte-identical to what was signed. If your framework parses JSON automatically, you may need to re-serialize — or better, sign/verify against the raw bytes received over the wire. Use a body-parser that exposes the raw buffer.
  • HMAC-SHA256 only. No SHA-1, no MD5. If you're integrating with a legacy service that uses these, you'll need a different library — they shouldn't be used for new signatures.

License

Apache-2.0 © Vlad Bordei