The TOTP & HOTP implement for Node.JS.
- HOTP (HMAC-based One-Time Password) algorithm implementation.
- TOTP (Time-based One-Time Password) algorithm implementation.
- OTP URL encoding and decoding.
- Command line tool:
- Generating OTP codes.
- Generating OTP URLs.
- Inspecting OTP URLs.
- Customization:
- Digits length from 4 - 10
- TOTP time-step (period)
- Digest algorithms including SHA-1, SHA-256, and SHA-512
- Node.js >= 20.0.0
- TypeScript v5.0.x (Or newer)
This package is an ES module (ESM). Your project must use ESM (
"type": "module") or be configured for ESM compatibility.
npm i @litert/otp --saveimport * as LibOTP from '@litert/otp';
// TOTP
const code = LibOTP.TOTP.generate(Buffer.from('12345678901234567890'));
const gen = LibOTP.TOTP.createGenerator(Buffer.from('12345678901234567890'));
console.log(gen());
// HOTP
const hotpCode = LibOTP.HOTP.generate(Buffer.from('12345678901234567890'), 0);
// URL
const url = LibOTP.URL.stringify({
type: 'totp',
key: Buffer.from('12345678901234567890'),
label: 'user@example.com',
});
const info = LibOTP.URL.parse(url);You can also import individual modules via subpath exports:
import { generate } from '@litert/otp/totp';
import { generate as generateHOTP, createGenerator } from '@litert/otp/hotp';
import { stringify, parse } from '@litert/otp/url';
import { EDigest, DEFAULT_DIGITS } from '@litert/otp/constants';
import { generate as generateVerboseTOTP } from '@litert/otp/totp/verbose';
import { generate as generateVerboseHOTP } from '@litert/otp/hotp/verbose';See full examples:
Click here for a quick start guide.
You can use this library as a command line tool to do OTP operations.
There are two ways to use the CLI:
-
Use
npxto run the CLI without installing it manually.npx @litert/otp -k 'raw:1234567890' # Check the help message npx @litert/otp --help
-
Install it globally or locally in your project.
npm i -g @litert/otp # or install in local project only npm i @litert/otp # -D # if only used in dev environment
Then you can use the
otpcommand directly:npx otp -k 'raw:1234567890'
Click here for more details about the CLI usage.
This library is published under Apache-2.0 license.