otplib API Documentation / @otplib/plugin-crypto-node
@otplib/plugin-crypto-node
Node.js crypto plugin for otplib using the built-in crypto module.
Installation
bash
npm install @otplib/plugin-crypto-node
pnpm add @otplib/plugin-crypto-node
yarn add @otplib/plugin-crypto-nodeOverview
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:
sha1sha256sha512
Usage
Basic Usage
typescript
import { generateSecret, generate } from "otplib";
import { NodeCryptoPlugin } from "@otplib/plugin-crypto-node";
import { ScureBase32Plugin } from "@otplib/plugin-base32-scure";
const crypto = new NodeCryptoPlugin();
const base32 = new ScureBase32Plugin();
// Generate a secret
const secret = generateSecret({ crypto, base32 });
// Generate a token
const token = await generate({
secret,
crypto,
base32,
});With Custom Algorithm
typescript
import { generate } from "otplib";
import { NodeCryptoPlugin } from "@otplib/plugin-crypto-node";
import { ScureBase32Plugin } from "@otplib/plugin-base32-scure";
const crypto = new NodeCryptoPlugin();
const base32 = new ScureBase32Plugin();
const token = await generate({
secret: "JBSWY3DPEHPK3PXP",
algorithm: "sha256",
crypto,
base32,
});Synchronous HMAC
The Node.js crypto plugin supports both synchronous and asynchronous HMAC operations:
typescript
import { NodeCryptoPlugin } from "@otplib/plugin-crypto-node";
const crypto = new NodeCryptoPlugin();
// 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);When to Use
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
Platform Support
- Node.js (all versions)
- Not available in browsers (use
@otplib/plugin-crypto-webinstead) - Not available in edge runtimes (use
@otplib/plugin-crypto-webinstead)
Documentation
Full documentation available at otplib.yeojz.dev:
License
MIT © 2026 Gerald Yeo
Classes
Variables
References
default
Renames and re-exports NodeCryptoPlugin