Crypto Middleware
    Preparing search index...

    Crypto Middleware

    crypto-middleware logo

    @sebastienrousseau/crypto-middleware

    Framework-agnostic cryptographic middleware for Express, Fastify, and Koa applications.

    Build Coverage Registry Docs OpenSSF Scorecard License: Apache-2.0 OR MIT Node.js 22 or newer


    Getting started

    • Install — installation via pnpm, npm, or yarn
    • Requirements — runtime floor and environment prerequisites
    • Quick Start — minimal working usage sample

    The Crypto Service ecosystem

    Package reference

    Operational


    pnpm add @sebastienrousseau/crypto-middleware
    # or
    npm install @sebastienrousseau/crypto-middleware
    # or
    yarn add @sebastienrousseau/crypto-middleware

    Back to Top


    • Node.js: ^22.0.0 or >=24.0.0 (active and maintenance LTS releases)
    • Package Manager: pnpm >=9 (recommended) or npm >=10
    • TypeScript: >=5.0 (when compiling with TypeScript)

    Back to Top


    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);

    Back to Top


    Crypto Service provides a complete cryptography stack across 14 specialized packages:

    Package Role Description
    @sebastienrousseau/crypto-api API Schemas Shared TypeScript types and utilities for the Crypto Service Suite, defining the canonical API surface.
    @sebastienrousseau/crypto-cli Terminal CLI An interactive command-line interface for cryptographic operations, supporting both legacy OpenPGP and modern post-quantum algorithms.
    @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 Key Management Service interface for AWS KMS, GCP Cloud KMS, Azure Key Vault, and HashiCorp Vault.
    @sebastienrousseau/crypto-lib Core Library A modern cryptographic library for TypeScript, with post-quantum support, zero unsafe dependencies, and 100% test coverage.
    @sebastienrousseau/crypto-middleware (this package) Middleware Framework-agnostic cryptographic middleware for Express, Fastify, and Koa applications.
    @sebastienrousseau/crypto-prisma ORM Adapter Transparent field-level encryption extension for Prisma Client, powered by AES-256-GCM.
    @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 WebAssembly performance accelerator for crypto-lib

    Back to Top


    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.

    Back to Top

    ## Express
    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
    jwtSecret string * Secret for HS256 JWT verification

    * Required when the corresponding operation is enabled.

    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.

    Back to Top

    ## Fastify
    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.

    Back to Top

    ## Examples

    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

    Back to Top

    Back to Top


    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 14 packages in the Crypto Service workspace maintain a 100% coverage floor across statements, branches, functions, and lines.

    Back to Top


    Report vulnerabilities privately via GitHub Security Advisories or according to SECURITY.md. Never report security issues publicly.

    All cryptographic operations leverage audited primitives, enforce constant-time execution where applicable, and zero sensitive key material upon disposal.

    Back to Top


    Back to Top


    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 14 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.

    Back to Top


    Dual-licensed under Apache 2.0 or MIT, at your option.

    Copyright (c) 2022-2026 Sebastien Rousseau and The Crypto Service Suite contributors.

    Back to Top