@otplib/hotp
RFC 4226 HOTP implementation for otplib.
Installation
npm install @otplib/hotp
pnpm install @otplib/hotp
yarn add @otplib/hotp
Usage
generate
Generate an HOTP code for a specific counter:
import { generate } from "@otplib/hotp";
import { crypto } from "@otplib/plugin-crypto-node";
const secret = new Uint8Array([
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
0x37, 0x38, 0x39, 0x30,
]);
const token = await generate({
secret,
counter: 0,
crypto,
algorithm: "sha1",
digits: 6,
});
With Base32 secrets
If your secret is a Base32 string (e.g., from Google Authenticator), provide a base32 plugin to decode it:
import { generate } from "@otplib/hotp";
import { crypto } from "@otplib/plugin-crypto-node";
import { base32 } from "@otplib/plugin-base32-scure";
const token = await generate({
secret: "GEZDGNBVGY3TQOJQGEZDGNBVGY",
counter: 0,
crypto,
base32,
});
verify
Verify an HOTP code:
import { verify } from "@otplib/hotp";
import { crypto } from "@otplib/plugin-crypto-node";
const result = await verify({
secret,
token: "123456",
counter: 0,
crypto,
algorithm: "sha1",
digits: 6,
counterTolerance: 5,
});
counterTolerance accepts a plain number (creates a look-ahead-only window [0, n], the secure default per RFC 4226) or a [past, future] tuple for explicit control (e.g., [2, 5]).
Sync Variants
generateSync and verifySync are synchronous alternatives with the same signatures. They require a crypto plugin that supports sync HMAC operations, such as @otplib/plugin-crypto-node or @otplib/plugin-crypto-noble. Using them with @otplib/plugin-crypto-web will throw.
import { generateSync, verifySync } from "@otplib/hotp";
import { crypto } from "@otplib/plugin-crypto-node";
const secret = new Uint8Array([
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
0x37, 0x38, 0x39, 0x30,
]);
const token = generateSync({ secret, counter: 0, crypto });
const result = verifySync({ secret, token, counter: 0, crypto });
Documentation
Full documentation available at otplib.yeojz.dev:
License
MIT