Cryptographic middleware for Express and Fastify: decrypt requests, encrypt responses, verify HMAC signatures and JWTs.
Getting started
The Crypto Service ecosystem
Package reference
Operational
pnpm add @sebastienrousseau/crypto-middleware
# or
npm install @sebastienrousseau/crypto-middleware
# or
yarn add @sebastienrousseau/crypto-middleware
^22.0.0 or >=24.0.0 (active and maintenance LTS releases)pnpm >=9 (recommended) or npm >=10>=5.0 (when compiling with TypeScript)import express from "express";
import { createCryptoMiddleware } from "@sebastienrousseau/crypto-middleware";
const app = express();
app.use(express.json());
app.use(
createCryptoMiddleware({
key: process.env.CRYPTO_KEY, // 256-bit hex key
operations: ["decrypt-request", "encrypt-response"],
}),
);
app.post("/api/data", (req, res) => {
// req.body is already decrypted
res.json({ received: req.body });
// response is automatically encrypted
});
app.listen(3000);
Crypto Service provides a complete cryptography stack across 14 specialized packages:
| Package | Role | Description |
|---|---|---|
@sebastienrousseau/crypto-api |
API docs | Postman collection types and a Postman-to-Markdown converter; the server's API contract is its OpenAPI document. |
@sebastienrousseau/crypto-benchmarks |
Benchmarks | Benchmarks comparing classical and post-quantum cryptography: ops/sec, latency and throughput. |
@sebastienrousseau/crypto-cbom |
CBOM | Cryptographic Bill of Materials generator and auditor (CycloneDX 1.6, SPDX 3.0). |
@sebastienrousseau/crypto-cli |
Terminal CLI | Command-line interface: hashing, key generation, encryption, signing, HPKE, Argon2id and CBOM scans, with an interactive menu. |
@sebastienrousseau/crypto-edge |
Edge Runtime | Edge-runtime cryptographic operations using the Web Crypto API, optimized for Cloudflare Workers, Vercel Edge, and Deno. |
@sebastienrousseau/crypto-kms |
Cloud KMS | Unified KMS interface: AWS KMS, Google Cloud KMS, Vault Transit and local providers; Azure is a stub. |
@sebastienrousseau/crypto-lib |
Core Library | Core library: post-quantum ML-KEM, ML-DSA and SLH-DSA, plus classical, modern and OpenPGP cryptography for TypeScript. |
@sebastienrousseau/crypto-lsp |
IDE Analysis | Language server that flags weak and quantum-vulnerable cryptography in the editor, with quick fixes. |
@sebastienrousseau/crypto-mcp |
AI Agents | Model Context Protocol server exposing cryptography tools to AI assistants and agents. |
@sebastienrousseau/crypto-middleware (this package) |
Middleware | Cryptographic middleware for Express and Fastify: decrypt requests, encrypt responses, verify HMAC signatures and JWTs. |
@sebastienrousseau/crypto-prisma |
ORM Adapter | Transparent field-level encryption extension for Prisma Client, using XChaCha20-Poly1305. |
@sebastienrousseau/crypto-react |
React Hooks | React hooks and context provider for client-side cryptographic operations with zero boilerplate. |
@sebastienrousseau/crypto-sdk |
Client SDK | A zero-dependency, typed HTTP client for the Crypto Service REST API, with full post-quantum support. |
@sebastienrousseau/crypto-server |
HTTP API | A hardened Fastify REST API for cryptographic operations, with rate limiting, OpenAPI schemas, and post-quantum endpoints. |
@sebastienrousseau/crypto-testing |
Test Support | Deterministic keys, fast mocks, and test fixtures for crypto-lib |
@sebastienrousseau/crypto-typeorm |
ORM Adapter | TypeORM column-level encryption with a single decorator, powered by crypto-lib. |
@sebastienrousseau/crypto-vue |
Vue Composables | Vue 3 composables for client-side cryptography |
@sebastienrousseau/crypto-wasm |
Acceleration | Placeholder for a WebAssembly accelerator; no WebAssembly module ships yet. |
crypto-middleware provides transparent cryptographic operations for Express and Fastify applications. It can auto-decrypt incoming request bodies, encrypt outgoing responses, verify HMAC signatures on webhooks, and validate HS256 JWT bearer tokens -- all via a single middleware registration. Route matching via glob patterns lets you scope protection to specific endpoints.
| Option | Type | Required | Description |
|---|---|---|---|
key |
string |
* | Hex-encoded 256-bit key for encryption/decryption |
routes |
string[] |
No | Glob patterns for routes to apply middleware to |
operations |
string[] |
No | Operations to perform (see below) |
hmacKey |
string |
* | Hex-encoded HMAC key for signature verification, at least 16 bytes |
jwtSecret |
string |
* | Secret for HS256 JWT verification, at least 16 bytes |
jwtIssuer |
string |
No | Required iss claim for verify-jwt |
jwtAudience |
string |
No | Required aud value for verify-jwt |
* Required when the corresponding operation is enabled.
verify-jwt accepts only HS256 (any other alg, including none, is
rejected), requires a numeric exp claim, checks nbf when present, and
checks iss and aud when jwtIssuer / jwtAudience are set. Set both
when the secret is shared with any other service.
| Operation | Description |
|---|---|
decrypt-request |
Decrypts incoming JSON bodies |
encrypt-response |
Encrypts outgoing JSON responses |
verify-signature |
Verifies HMAC-SHA256 from x-signature header |
verify-jwt |
Verifies HS256 JWT from Authorization: Bearer header |
| Pattern | Matches |
|---|---|
/api/data |
Exact match only |
/api/* |
One path segment: /api/users, /api/1 |
/api/** |
Any depth: /api/users/1/profile |
When routes is omitted or empty, middleware applies to all routes.
import Fastify from "fastify";
import { cryptoPlugin } from "@sebastienrousseau/crypto-middleware";
const app = Fastify();
app.register(cryptoPlugin, {
key: process.env.CRYPTO_KEY,
operations: ["decrypt-request", "encrypt-response"],
});
app.post("/api/data", async (request) => {
return { received: request.body };
});
app.listen({ port: 3000 });
The cryptoPlugin accepts the same configuration options as
createCryptoMiddleware.
All examples are self-contained TypeScript files in the examples/
directory. Run any example with:
npx ts-node examples/<name>.ts
| Category | Example | Purpose |
|---|---|---|
| Express | express.ts | Full Express setup with encrypt/decrypt |
| Fastify | fastify.ts | Fastify plugin registration |
| Webhook | webhook.ts | HMAC signature verification for webhooks |
| JWT | jwt.ts | JWT Bearer token verification |
| Encrypted | encrypted.ts | Full encrypted request/response pipeline |
pnpm --filter @sebastienrousseau/crypto-middleware run build
pnpm --filter @sebastienrousseau/crypto-middleware run test
pnpm --filter @sebastienrousseau/crypto-middleware run lint
pnpm --filter @sebastienrousseau/crypto-middleware run format
All 18 packages in the Crypto Service workspace maintain a 100% coverage floor across statements, branches, functions, and lines.
Report vulnerabilities privately via GitHub Security Advisories or according to SECURITY.md. Never report security issues publicly.
The middleware verifies JWTs and HMAC signatures but does not rate-limit them. Register a rate limiter in the host application (for example @fastify/rate-limit or express-rate-limit) in front of the routes it protects, as crypto-server does.
Cryptographic operations use the @noble/* libraries, Node.js crypto and OpenPGP.js. @noble/post-quantum has not been independently audited and does not guarantee constant-time execution, and no module in this suite is FIPS 140-3 validated. Key zeroization is limited: JavaScript strings and garbage-collected buffers cannot be reliably wiped. See SECURITY.md.
Versions advance strictly one step at a time on the 0.0.x line (v0.0.1 → v0.0.2 → v0.0.3 ... → v0.0.999 → v0.1.0). Work for every release iteration begins on a dedicated feat/v<version> branch.
All 18 packages in the workspace move in lockstep. Public API signatures, cipher output formats, and serialization schemas are strictly versioned. Breaking changes to serialized formats or algorithm defaults are considered major breaking changes. Minimum toolchain upgrades (e.g. Node.js LTS floor) are governed by POLICIES.md.
Dual-licensed under Apache 2.0 or MIT, at your option.
Copyright (c) 2022-2026 Sebastien Rousseau and The Crypto Service Suite contributors.