npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@otp-lib/core

v0.2.0

Published

TOTP (Time-based) and HOTP (HMAC-based) One-Time Password library

Downloads

545

Readme

@otp-lib/core

TOTP (Time-based) and HOTP (HMAC-based) One-Time Password library

Node.js CI codecov npm License: MIT

About

@otp-lib/core is designed to build strict, compliant implementation of One-Time Passwords (OTP) algorithms.

Built on top of the native Web Crypto API, it ensures cryptographic security and performance across all modern JavaScript environments (Node.js, Deno, Bun, Browsers, Cloudflare Workers).

Features

  • Secure : Uses the native Web Crypto API (crypto.subtle) for cryptographic operations.
  • Isomorphic : Works in Node.js, Bun, Deno, Browsers, and Cloudflare Workers.
  • Type-Safe : Written in strict TypeScript with full type definitions included.
  • Zero Dependencies : No external overhead. Lightweight and fast.
  • Compliant : Strict implementations of the IETF standards :
    • RFC 4226 : HMAC-Based One-Time Password (HOTP).
    • RFC 6238 : Time-Based One-Time Password (TOTP).
    • RFC 4648 : Base32 and Base64 Data Encodings.

Install

Node.js

npm install @otp-lib/core

Bun

bun add @otp-lib/core

Deno

deno install npm:@otp-lib/core

Browser (ESM)

For modern browsers, you can import the ECMAScript Module (ESM) directly from unpkg.

<script type="module">
  import { Secret } from "https://unpkg.com/@otp-lib/core/dist/otp-core.esm.min.js";

  const secret = Secret.create();
  console.log(secret.toBase32());
</script>

Browser (UMD)

For legacy usage without a bundler, you can use the minified script from unpkg.

<script src="https://unpkg.com/@otp-lib/core/dist/otp-core.iife.min.js"></script>

<script>
  const secret = OTPCore.Secret.create();
  console.log(secret.toBase32());
</script>

Quick Start

Secret Management

The Secret class is the entry point. It is immutable and handles secure encoding/decoding of keys.

Supported encodings : Hex, Base32, Base64, Base64URL, Latin1, ASCII, UTF-8.

import { Secret } from "@otp-lib/core";

// Generate a random cryptographically secure secret (20 bytes)
const secret = Secret.create();

// OR import from an existing string (e.g., from your configuration)
const secretFromBase32 = Secret.fromBase32("JBSWY3DPEHPK3PXP");
const secretFromHex = Secret.fromHex("48656c6c6f21deadbeef");

// Export for storage or display
console.log(secret.toBase32()); // "JBSWY3DPEHPK3PXP"
console.log(secret.toHex());    // "4865..."

HOTP (HMAC-Based OTP)

Implements the HMAC-Based algorithm where codes are generated from an incrementing counter.

import { HOTP, Secret } from "@otp-lib/core";

const secret = Secret.create();
const hotp = new HOTP({ 
    secret, 
    counter: 0,      // Current counter state
    lookAhead: 3    // Allow validating 3 steps ahead (resync)
});

// Generate
const token = await hotp.generate(); 

// Verify and Resync
// verifyDelta returns the difference if valid, or null if invalid
const delta = await hotp.verifyDelta("123456"); 

if(delta !== null) {
	// Success! Update your database with the new counter
    const newCounter = hotp.getCounter() + delta + 1;
    // saveToDb(newCounter);
}

TOTP (Time-Based OTP)

Implements the Time-Based algorithm where codes are generated based on the current system time.

import { TOTP, Secret } from "@otp-lib/core";

const secret = Secret.fromBase32("JBSWY3DPEHPK3PXP");
const totp = new TOTP({ secret });

// Generate a token
const token = await totp.generate();
console.log(token); // e.g. "123456"

// Verify a token
// Returns true if the token is valid within the current window
const isValid = await totp.verify("123456");

References

OTP Options

HOTP and TOTP accept the following OTP options.

| Option | Type | Default | Description | | ------ | ---- | ------- | ----------- | | algorithm | HashAlgorithms | SHA-1 | The HMAC hashing algorithm (SHA-1, SHA-256, SHA-384, SHA-512). | | digits | number | 6 | The length of the generated code. | | secret | Secret | Secret.create() | The shared secret key instance. |

HOTP Options

Extends OTP Options.

| Option | Type | Default | Description | | ------ | ---- | ------- | ----------- | | lookAhead | number | 0 | The number of future codes to check during validation (Resynchronization window). | | counter | number | 0 | The initial counter value. |

TOTP Options

Extends OTP Options.

| Option | Type | Default | Description | | ------ | ---- | ------- | ----------- | | window | number | [number, number] | [0, 0] | The validation window to tolerate clock drift or latency.Number : Symmetric window (e.g., 1 = ±1 step).Tuple : [past, future] steps (e.g., [1, 0] allows 1 step back, 0 forward). | | period | number | 30 | The time step in seconds. |

License

MIT © Unknown-Robot