crypto-id-lite
v1.0.0
Published
Ultra-fast, zero-dependency cryptographically secure random ID, UUID v4, and token generator using native Web Crypto.
Maintainers
Readme
🔑 crypto-id-lite
Ultra-fast, lightweight (< 1 KB), zero-dependency cryptographically secure random ID, UUID v4, and token generator using native Web Crypto.
💡 Why crypto-id-lite?
Every web application and backend requires unique identifiers for database records, session tokens, API keys, or verification codes. Often teams install both uuid and nanoid simultaneously, pulling separate dependencies and polyfills.
crypto-id-lite provides:
- ⚡ Zero Dependencies — Minified to less than 1 KB, pure native TypeScript.
- 🔐 Cryptographically Secure — Powered directly by standard
globalThis.crypto.getRandomValues()andrandomUUID(). - 🎛️ All-in-One Generator — Compact NanoIDs, RFC4122 v4 UUIDs, Hex API tokens, and numeric verification PINs in a single package.
- 🔠 Custom Alphabets — Supply custom characters for lowercase-only, alphanumeric, or hex requirements.
- 📦 Dual ESM & CommonJS — Works everywhere: Node.js, Next.js, Vite, Remix, React Native, and edge runtimes (Cloudflare Workers, Deno, Bun).
📥 Installation
# npm
npm install crypto-id-lite
# pnpm
pnpm add crypto-id-lite
# yarn
yarn add crypto-id-lite
# bun
bun add crypto-id-lite🚀 Usage & Real-World Examples
1. NanoID-Style Compact Random IDs
import { generateId } from 'crypto-id-lite';
// Default 21-character URL-safe string:
const userId = generateId();
console.log(userId);
// "V1StGXR8_Z5jdHi6B-myT"
// Custom length:
const shortId = generateId(10);
console.log(shortId);
// "75pxJACKVE"
// Custom alphabet (e.g. lowercase alphanumeric):
const slug = generateId(8, '0123456789abcdefghijklmnopqrstuvwxyz');
console.log(slug);
// "k8v2q9m1"2. Standard RFC4122 Version 4 UUIDs
import { generateUuid } from 'crypto-id-lite';
const transactionId = generateUuid();
console.log(transactionId);
// "f47ac10b-58cc-4372-a567-0e02b2c3d479"3. Secure Hex Tokens for Password Resets & API Keys
import { generateHexToken } from 'crypto-id-lite';
// Generates 32 bytes (64 hex characters) secure secret token:
const resetPasswordToken = generateHexToken(32);
console.log(resetPasswordToken);
// "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"4. Numeric OTP / Verification PINs
import { generatePin } from 'crypto-id-lite';
// Generates 6-digit numeric SMS / Email verification code:
const smsOtp = generatePin(6);
console.log(smsOtp);
// "491823"📖 API Reference
generateId(size?, alphabet?)
Generates a cryptographically secure random string with uniform character distribution.
- Parameters:
size(number, default:21): Desired string length.alphabet(string, default: URL-safe 64-char alphabet): Set of characters to choose from.
- Returns:
string
generateUuid()
Generates a standard 36-character RFC4122 compliant version 4 UUID.
- Returns:
string(e.g.xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx)
generateHexToken(byteLength?)
Generates a cryptographically random hexadecimal string.
- Parameters:
byteLength(number, default:32): Entropy byte count (resulting string length will bebyteLength * 2).
- Returns:
string
generatePin(length?)
Generates a random numeric string (digits 0-9).
- Parameters:
length(number, default:6): Number of digits.
- Returns:
string
💖 Support the Project
If crypto-id-lite replaced bloated ID dependencies in your projects, please consider sponsoring:
👉 Sponsor @Piratha97 on GitHub Sponsors
📄 License
MIT © 2026 Pirapaakaran Pirathapan
