Zero-Dependency RFC 6238 TOTP (2FA) Engine

Tech stack:
TS
Node.js
Next.js

Zero-dependency RFC 6238 TOTP 2FA engine for Node.js using native crypto. Features Base32 encoding, otpauth URIs, clock drift tolerance, replay protec...
read more
AI Contributions:0
Published At:October 23, 2026
Files Included:2 file(s)

Read Me

# Zero-Dependency RFC 6238 TOTP (2FA) Engine

A lightweight, zero-dependency TypeScript implementation of RFC 6238 / RFC 4226 Time-based One-Time Passwords (TOTP) built using only Node.js native `node:crypto`.

## ✨ Features
- **Zero External Dependencies:** Built entirely with native `node:crypto`. No external npm bloat.
- **RFC 6238 & RFC 4226 Compliant:** Compatible with Google Authenticator, Microsoft Authenticator, Authy, and 1Password.
- **Built-in Base32:** Includes pure TypeScript Base32 encoder and decoder.
- **Timing-Safe Verification:** Uses `timingSafeEqual` to prevent timing attacks.
- **Clock Drift Tolerance:** Automatically checks ±1 step (±30 seconds) to tolerate client-server clock drift.
- **Replay Attack Protection:** Rejects replayed codes at or before `lastUsedStep`.

## 🚀 Setup & Installation
1. Copy `totp.ts` into your project (e.g. `src/lib/totp.ts`).
2. No packages to install — standard Node.js runtime supported.

## 💡 Quick Usage Example

```typescript
import { generateTotpSecret, otpauthUri, verifyTotp } from "./totp";

// Step 1: Generate user secret
const secret = generateTotpSecret();

// Step 2: Generate URI for QR code
const uri = otpauthUri("user@example.com", secret, "MyApp");

// Step 3: Verify user's code on login
const userEnteredCode = "123456";
const matchedStep = verifyTotp(secret, userEnteredCode, Date.now());

if (matchedStep !== null) {
  console.log("Authentication successful! Step:", matchedStep);
} else {
  console.error("Invalid or expired 2FA code.");
}

Loading

Loading

Loading

More like this View all

Roman Urdu Order Parser: Text, Screenshot, Voice

Turn WhatsApp/Instagram order chats, chat screenshots and voice notes (English, Roman Urdu or mixed) into validated order JSON with per-field confidence. Gemini + zod, retries, fallback.

  • 0

    Ratings

  • 5

    Files

    • TS
    • Next.js
    • +2

    Tech stack

SHA-256 Incident Fingerprinting & Dedup Engine — F

Deduplicates repeated incidents in real time by generating a SHA-256 fingerprint from normalized error signatures — so the same crash occurring 1,000 times creates one tracked incident with an occurre

  • 0

    Ratings

  • 1

    Files

    • TS
    • Node.js
    • +1

    Tech stack

AI Root-Cause Diagnosis Engine — Fixly

Analyzes incident logs and stack traces to diagnose root cause, severity, and confidence score using AI (Groq Llama 3.3) — with a deterministic rule-based fallback when no AI key is present.

  • 0

    Ratings

  • 1

    Files

    • TS
    • Node.js
    • +1

    Tech stack