@otplib/totp
RFC 6238 TOTP implementation for otplib.
Installation
npm install @otplib/totp
pnpm install @otplib/totp
yarn add @otplib/totp
Usage
import { generate, verify } from "@otplib/totp";
import { crypto } from "@otplib/plugin-crypto-node";
import { base32 } from "@otplib/plugin-base32-scure";
const token = await generate({
secret: "GEZDGNBVGY3TQOJQGEZDGNBVGY",
crypto,
base32,
});
const result = await verify({
secret: "GEZDGNBVGY3TQOJQGEZDGNBVGY",
token: "123456",
crypto,
base32,
});
Functions
generate
Generate a TOTP code:
import { generate } from '@otplib/totp';
import { crypto } from '@otplib/plugin-crypto-node';
import { base32 } from '@otplib/plugin-base32-scure';
const token = await generate({
secret: new Uint8Array([...]),
crypto,
base32,
algorithm: 'sha1',
digits: 6,
period: 30,
epoch: Math.floor(Date.now() / 1000),
});
verify
Verify a TOTP code:
import { verify } from '@otplib/totp';
import { crypto } from '@otplib/plugin-crypto-node';
import { base32 } from '@otplib/plugin-base32-scure';
const result = await verify({
secret: new Uint8Array([...]),
token: '123456',
crypto,
base32,
algorithm: 'sha1',
digits: 6,
period: 30,
epoch: Math.floor(Date.now() / 1000),
epochTolerance: 30,
});
getRemainingTime
Get the remaining time before the next TOTP period:
import { getRemainingTime } from "@otplib/totp";
const seconds = getRemainingTime(
Math.floor(Date.now() / 1000),
30,
0,
);
getTimeStepUsed
Get the time step used for a specific time:
import { getTimeStepUsed } from "@otplib/totp";
const counter = getTimeStepUsed(
Math.floor(Date.now() / 1000),
30,
0,
);
Documentation
Full documentation available at otplib.yeojz.dev:
License
MIT