@otplib/hotp
RFC 4226 HOTP implementation for otplib.
Installation
npm install @otplib/hotp
pnpm install @otplib/hotp
yarn add @otplib/hotp
Usage
import { generate, verify } from "@otplib/hotp";
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",
counter: 0,
crypto,
base32,
});
const result = await verify({
secret: "JBSWY3DPEHPK3PXP",
token: "123456",
counter: 0,
crypto,
base32,
counterTolerance: 0,
});
Functions
generate
Generate an HOTP code for a specific counter:
import { generate } from '@otplib/hotp';
const token = await generate({
secret: new Uint8Array([...]),
counter: 0,
crypto: new NodeCryptoPlugin(),
base32: new ScureBase32Plugin(),
algorithm: 'sha1',
digits: 6,
});
verify
Verify an HOTP code:
import { verify } from '@otplib/hotp';
const result = await verify({
secret: new Uint8Array([...]),
token: '123456',
counter: 0,
crypto: new NodeCryptoPlugin(),
base32: new ScureBase32Plugin(),
algorithm: 'sha1',
digits: 6,
counterTolerance: 5,
});
Documentation
Full documentation available at otplib.yeojz.dev:
License
MIT © 2026 Gerald Yeo