@octabits-io/signing
A reply address that routes back to the right conversation, an unsubscribe
link nobody can forge for someone else, a webhook whose secret sits in its URL,
a short-lived token for a magic link: each needs a key, and keys for different
purposes and different tenants must never be interchangeable. This package
gives every purpose its own 256-bit key under an opaque scope,
HKDF-derived from one master secret or read from a key store you own, and signs
with it — full HMAC, short hex tags for identifiers with a length limit, and
HS256 tokens — plus a constant-time comparison for secrets a caller submits
that does not leak their length. Pure JavaScript on @noble/hashes: no
node:*, so it runs on Node, Bun, Deno and edge runtimes. jose is an
optional peer, loaded only by the JWT half.
npm i @octabits-io/signing # add jose if you sign or verify tokens
import { constantTimeEquals, createScopedSigningService } from '@octabits-io/signing';
const signing = createScopedSigningService({
infoPrefix: 'acme', // HKDF info `4:acme|5:reply|signing-key-v1`
scopeKey: workspaceId, // opaque salt: one key space per scope
keyStore: { read, write }, // your `purpose → base64 key` persistence
masterSecret: process.env.SIGNING_MASTER_SECRET, // optional: derive keys and sign tokens
});
const tag = await signing.shortTag('reply', conversationId); // Result<string>, 24 hex chars
await signing.verifyShortTag('reply', conversationId, tag.value); // Result<boolean>
const sig = await signing.hmac('unsubscribe', recipientId); // Result<string>, base64url
const jwt = await signing.signJwt('magic-link', { sub }, { expiresAt });
await signing.verifyJwt('magic-link', jwt.value); // Result<JWTPayload>
constantTimeEquals(request.params.secret, storedSecret); // boolean
Exports
| Export | What it is |
|---|---|
createScopedSigningService(config) |
deriveKey, ensureProvisioned, getKey, retireOldKeys, hmac / verifyHmac, shortTag / verifyShortTag (1–32 bytes, default 12), signJwt / verifyJwt (exp required, clockToleranceSeconds option) |
signingKeyId(key) |
a key’s id — the first 8 bytes of its SHA-256, hex: a token’s kid, and the name of a replaced key (purpose#<kid>) |
constantTimeEquals(a, b) |
secret comparison over SHA-256 digests: fixed width, no early exit, no length leak |
SigningKeyStore |
read() / write(map) — the one seam; a config row, a KV table, a vault path. purpose holds the key in force, purpose#<kid> a key it replaced |
ScopedSigningError |
scoped_signing_key_not_found, scoped_signing_signature_invalid, scoped_signing_invalid_bytes |
How it works
One key per purpose, per scope. With a masterSecret, a key is
HKDF-SHA256(masterSecret, salt = scopeKey, info) — derived on the fly, so a
signature verifies before any store read. The default info length-prefixes
both variable parts, so ('a', 'b-c') and ('a-b', 'c') can never derive the
same key; deriveInfo replaces the format only to adopt an existing key space
without a rotation. Without a master secret the service reads keys from the
store: it verifies, and signs under keys provisioned earlier, and an
unprovisioned purpose is scoped_signing_key_not_found, not a throw.
Provisioning writes what derivation computes. signJwt with a master
secret stores the derived key, so a verify-only process without the secret can
check the token. When a different key stands under the purpose — the master
secret or the info’s version changed — provisioning replaces it and keeps the
old one as purpose#<kid>: a verify-only process checks HMACs and tags under
every stored key of the purpose, and picks a token’s key by its kid header,
so what was signed before the rotation still verifies until retireOldKeys.
Provisioning is serialised within a process; a cross-process race that drops a
purpose is repaired by the next call, since derivation is deterministic.
A token is checked whole. verifyJwt pins HS256 (an alg: none or any
other algorithm is refused), requires exp, honours nbf, and allows
clockToleranceSeconds of skew (default 0); every refusal is
scoped_signing_signature_invalid.
Comparison does not leak. Verification compares encoded signatures without
an early exit. constantTimeEquals hashes both inputs first, so the comparison
runs over 32 bytes every time and a wrong-length candidate takes as long as a
right-length one.
Rotation is versioning. A new master secret changes every key at once —
right after a compromise. For a routine rotation, change the version in the
info, sign only with the new service, verify under both during the overlap
(a verify-only process does that from the store on its own), and call
retireOldKeys(purpose) after the longest token lifetime. The runbook is
skills/rotate-a-secret-or-key § 4.
Where it comes from
Extracted from @octabits-io/framework/signing (2026-09-15), where a
multi-tenant booking platform signs reply addresses, unsubscribe links and
webhook paths per tenant. The move replaced node:crypto with @noble/hashes
and @scure/base; the tests pin the default derivation, the HMAC and the short
tag to known answers and to the bytes node:crypto computes, and a
deriveInfo key space against node:crypto, so every signature issued before
the move still verifies.
From @octabits-io/signing@0.2.1 in the platform repository.