Node.js crypto module adapter for otplib
npm install @otplib/plugin-crypto-nodeNode.js crypto plugin for otplib using the built-in crypto module.
``bash`
npm install @otplib/plugin-crypto-node
pnpm add @otplib/plugin-crypto-node
yarn add @otplib/plugin-crypto-node
This plugin provides HMAC and random byte generation using Node.js's built-in crypto module. It supports all hash algorithms available in Node.js:
- sha1sha256
- sha512
-
`typescript
import { generateSecret, generate } from "otplib";
import { crypto } from "@otplib/plugin-crypto-node";
import { base32 } from "@otplib/plugin-base32-scure";
// Generate a secret
const secret = generateSecret({ crypto, base32 });
// Generate a token
const token = await generate({
secret,
crypto,
base32,
});
`
`typescript
import { generate } from "otplib";
import { crypto } from "@otplib/plugin-crypto-node";
import { base32 } from "@otplib/plugin-base32-scure";
const token = await generate({
secret: "GEZDGNBVGY3TQOJQGEZDGNBVGY",
algorithm: "sha256",
crypto,
base32,
});
`
The Node.js crypto plugin supports both synchronous and asynchronous HMAC operations:
`typescript
import { crypto } from "@otplib/plugin-crypto-node";
// Sync HMAC (faster, but blocks event loop)
const digest = crypto.hmacSync("sha1", key, data);
// Async HMAC (doesn't block event loop)
const digest = await crypto.hmac("sha1", key, data);
`
Use this plugin when:
- Running in Node.js environment
- Need maximum performance
- Want to use Node.js built-in crypto (no external dependencies)
- Need synchronous HMAC operations
- Node.js (all versions)
- Not available in browsers (use @otplib/plugin-crypto-web instead)@otplib/plugin-crypto-web` instead)
- Not available in edge runtimes (use
Full documentation available at otplib.yeojz.dev:
- Getting Started Guide
- API Reference
MIT © 2026 Gerald Yeo