@litert/otp
v3.0.0
Published
The TOTP & HOTP implement for Node.JS
Downloads
1,418
Readme
LiteRT/OTP
The TOTP & HOTP implement for Node.JS.
Features
- [x] HOTP (HMAC-based One-Time Password) algorithm implementation.
- [x] TOTP (Time-based One-Time Password) algorithm implementation.
- [x] OTP URL encoding and decoding.
- [x] Command line tool:
- [x] Generating OTP codes.
- [x] Generating OTP URLs.
- [x] Inspecting OTP URLs.
- [x] Customization:
- [x] Digits length from 4 - 10
- [x] TOTP time-step (period)
- [x] Digest algorithms including SHA-1, SHA-256, and SHA-512
Requirements
- 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.
Installation
npm i @litert/otp --saveUsage
Use in code
Namespace import (recommended)
import * 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: '[email protected]',
});
const info = LibOTP.URL.parse(url);Subpath imports (tree-shakable)
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.
Use as a command line tool
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 --helpInstall 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 environmentThen you can use the
otpcommand directly:npx otp -k 'raw:1234567890'
Click here for more details about the CLI usage.
Documents
License
This library is published under Apache-2.0 license.
