NanoID Generator
कस्टमाइज़ेबल अल्फाबेट के साथ छोटे URL-सेफ यूनिक IDs जनरेट करें
अल्फाबेट
साइज़
संख्या
NanoIDs बनाने के लिए जनरेट पर क्लिक करें
NanoID क्या है?
NanoID एक tiny, fast, URL-safe random ID generator है। Default में यह 64-character alphabet (A-Za-z0-9_-) use करके 21-character strings produce करता है, approximately 126 bits of entropy provide करता है — UUID v4 के 122 bits के comparable लेकिन shorter string में।
NanoID timestamp या कोई structured data embed नहीं करता। हर ID purely random है, operating system के cryptographically secure random number generator से generate होता है।
NanoID vs UUID v4
NanoID और UUID v4 दोनों CSPRNG-backed random ID generators हैं। वे format, length, और ecosystem support में differ करते हैं:
| Property | NanoID (default) | UUID v4 |
|---|---|---|
| Format | URL-safe alphanumeric + _- | Hyphenated hexadecimal |
| Length | 21 characters (default) | 36 characters |
| Entropy | ~126 bits | 122 bits |
| URL-safe | हाँ — कोई encoding needed नहीं | हाँ (with hyphens) |
| Alphabet | 64 characters (A-Za-z0-9_-) | 16 characters (0-9a-f) |
| Dependencies | npm package required | Native (crypto.randomUUID) |
| Customizable | हाँ — length और alphabet | नहीं |
| Standard | कोई नहीं (community library) | RFC 4122 / RFC 9562 |
UUID v4 चुनें जब external systems के साथ interoperability matter करे — native UUID columns वाले databases, UUID format expect करने वाले APIs, या UUIDs parse करने वाला logging infrastructure। NanoID चुनें जब shorter IDs चाहिए और full stack control में है।
Size द्वारा Collision Probability
NanoID की collision probability ID length और generation rate पर depend करती है। निम्न table default 64-character alphabet use करती है:
| Size (chars) | Possible IDs | Collision safety |
|---|---|---|
| 6 | 64 | ~1 in 4.5B — कुछ thousand IDs के लिए safe |
| 8 | 64 | ~1 in 4.5T — millions of IDs के लिए safe |
| 11 | 64 | ~1 in 2.8 quadrillion — billions of IDs के लिए safe |
| 16 | 64 | ~1 in 1.2 × 10^19 — trillions of IDs के लिए safe |
| 21 | 64 | ~1 in 10^30 — centuries के लिए per day 100 billion IDs के लिए safe |
| 32 | 64 | UUID v4 (122 bits) के comparable |
| 36 | 36 | UUID v4 से exceeds |
Default 21-character size UUID v4 की collision resistance (~126 bits) match करने के लिए chosen है जबकि 41% shorter है। ज़्यादातर applications के लिए, 21 characters right choice है।
Custom Alphabets
NanoID का alphabet fully customizable है। Library किसी भी unique characters की string accept करती है alphabet के रूप में:
A-Za-z0-9_-A-Za-z0-90-9a-f0-9Important: nanoid/non-secure केवल non-security-sensitive applications के लिए use करें। किसी भी ID के लिए जो unguessable होनी चाहिए, हमेशा default secure import use करें।
NanoID Randomness कैसे Generate करता है
NanoID operating system के cryptographically secure pseudo-random number generator (CSPRNG) use करता है। Browsers में यह crypto.getRandomValues() है; Node.js में यह crypto.randomFillSync() है। यह TLS session keys के लिए use किया जाने वाला same entropy source है — Math.random() से far stronger।
Rejection Sampling (Modulo Bias से बचना)
Random characters generate करने का naive approach: random byte (0–255) लें और byte % alphabetSize compute करें। यह modulo bias introduce करता है — कुछ characters दूसरों से slightly अधिक often appear होते हैं।
NanoID rejection sampling use करके इस bias को eliminate करता है:
- Alphabet size cover करने वाला smallest power-of-two mask determine करें (जैसे 64-char alphabet के लिए, mask 63 = 0b00111111 है)
- Random bytes generate करें और mask apply करें:
byte & mask - यदि masked value alphabet range में है, use करें। नहीं तो, discard और फिर try करें।
इसका मतलब कुछ random bytes discarded होते हैं, लेकिन result alphabet पर perfectly uniform distribution है — कोई character दूसरे से अधिक likely नहीं।
// Pure browser — no npm package needed
function generateNanoid(alphabet, size) {
const mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
const step = Math.ceil((1.6 * mask * size) / alphabet.length)
let id = ''
while (id.length < size) {
const bytes = crypto.getRandomValues(new Uint8Array(step))
for (const byte of bytes) {
const idx = byte & mask
if (idx < alphabet.length) {
id += alphabet[idx]
if (id.length === size) break
}
}
}
return id
}
const URL_SAFE = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
generateNanoid(URL_SAFE, 21) // → "V1StGXR8_Z5jdHi6B-myT"Environment Support
Code Examples
JavaScript / TypeScript
// npm i nanoid
import { nanoid } from 'nanoid'
nanoid() // → "V1StGXR8_Z5jdHi6B-myT" (21 chars, URL-safe)
nanoid(8) // → "Uakgb_J5" (custom size)
// Custom alphabet
import { customAlphabet } from 'nanoid'
const hexId = customAlphabet('0123456789abcdef', 16)
hexId() // → "4f3a1b8c9d2e0f7a"
const numId = customAlphabet('0123456789', 8)
numId() // → "30812894"Browser (CDN)
NanoID को CDN import के माध्यम से browser में directly use किया जा सकता है। Quick prototyping के लिए कोई build step required नहीं।
// Pure browser — no npm package needed
function generateNanoid(alphabet, size) {
const mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
const step = Math.ceil((1.6 * mask * size) / alphabet.length)
let id = ''
while (id.length < size) {
const bytes = crypto.getRandomValues(new Uint8Array(step))
for (const byte of bytes) {
const idx = byte & mask
if (idx < alphabet.length) {
id += alphabet[idx]
if (id.length === size) break
}
}
}
return id
}
const URL_SAFE = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
generateNanoid(URL_SAFE, 21) // → "V1StGXR8_Z5jdHi6B-myT"Python
# pip install nanoid
from nanoid import generate
generate() # → "V1StGXR8_Z5jdHi6B-myT"
generate(size=8) # → "Uakgb_J5"
generate('0123456789abcdef', 16) # custom alphabet + sizeNode.js (CommonJS)
// Node.js — stdlib only, no npm needed
const { randomFillSync } = require('crypto')
function nanoid(alphabet, size) {
const mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
const step = Math.ceil((1.6 * mask * size) / alphabet.length)
let id = ''
while (id.length < size) {
const bytes = randomFillSync(Buffer.alloc(step))
for (const byte of bytes) {
const idx = byte & mask
if (idx < alphabet.length) { id += alphabet[idx]; if (id.length === size) break }
}
}
return id
}