CUID Generator
कोलिजन-रेसिस्टेंट यूनिक IDs जनरेट करें (CUID v1)
जनरेट किए गए CUIDs
CUID v1 एक लेगेसी फॉर्मेट है। नए प्रोजेक्ट के लिए CUID2 उपयोग करें।
CUID क्या है?
CUID (Collision-resistant Unique IDentifier) एक globally unique ID है जो horizontally scalable applications के लिए design किया गया है।
यह हमेशा c letter से शुरू होता है, जिसके बाद timestamp, counter, host fingerprint, और random segment होता है — सभी base36 में encode।
IDs URL-safe, compact, और human-readable होते हैं, जो database IDs, URLs, और session generation के लिए उपयुक्त हैं।
CUID Structure
हर CUID पाँच अलग-अलग sections से बना होता है जो machines, processes, और time के across uniqueness सुनिश्चित करते हैं:
यह hierarchical design IDs को chronologically ordered रखता है जबकि distributed systems के लिए adequate entropy maintain करता है।
Collision Resistance
CUID uniqueness guarantee करने के लिए कई mechanisms combine करता है:
CUID vs UUID
CUID और UUID दोनों uniqueness problem solve करते हैं लेकिन अलग-अलग approaches से:
| Feature | CUID v1 | UUID v4 |
|---|---|---|
| Format | Lowercase alphanumeric | Hex with hyphens |
| Sortable | Partially (timestamp first) | No (v4 random) |
| URL-safe | हाँ | हाँ (hyphens के साथ) |
| Collision Resistance | High (structural) | High (statistical) |
| Predictability | Low (visible timestamp) | Very low |
| Length | 25 characters | 36 characters |
| समन्वय की आवश्यकता | नहीं | नहीं |
CUID visible database IDs और URLs के लिए बेहतर है; UUID अधिक widely-adopted और broader support वाला है।
CUID v1 vs CUID v2
CUID v2 CUID v1 की security concerns को address करने वाला complete algorithm rewrite है:
| पहलू | CUID v1 | CUID v2 |
|---|---|---|
| Algorithm | Custom | SHA3 + CSPRNG |
| Cryptographic | नहीं (predictable) | हाँ (cryptographically secure) |
| Timestamp | Clearly visible | Hidden in hash |
| Format | Always starts with 'c' | Customizable prefix |
| npm package | cuid | @paralleldrive/cuid2 |
| Length | Fixed 25 characters | Customizable (default 24) |
New projects के लिए, CUID v2 recommended है जो ease of use maintain करते हुए stronger security guarantees provide करता है।
Common Use Cases
minimal collision के साथ।Code Examples
Installation और basic usage:
// npm install @paralleldrive/cuid2 (recommended — CUID v2)
import { createId } from '@paralleldrive/cuid2'
const id = createId()
// → 'tz4a98xxat96iws9zmbrgj3a'
// Custom length
import { init } from '@paralleldrive/cuid2'
const createShortId = init({ length: 10 })
createShortId() // → 'zxp1l6mf4c'Node.js और common database integration:
// Pure Node.js — CUID v1 style (no dependencies)
let counter = 0
function pad(str, size) {
return str.padStart(size, '0').slice(-size)
}
function fingerprint() {
const os = require('os')
const source = [process.pid, os.hostname().length].join('')
let hash = 0
for (const c of source) {
hash = ((hash << 5) - hash) + c.charCodeAt(0)
hash |= 0
}
return pad(Math.abs(hash).toString(36), 4)
}
function cuid() {
const timestamp = Date.now().toString(36)
const cnt = pad((counter++ & 0xffff).toString(36), 4)
const fp = fingerprint()
const rnd = pad(Math.floor(Math.random() * 0xffffffff).toString(36), 4)
+ pad(Math.floor(Math.random() * 0xffffffff).toString(36), 4)
return 'c' + timestamp + cnt + fp + rnd
}
console.log(cuid()) // → 'clrc4gkwz001ag2hs3k7f9m2q'Database constraint usage:
-- Use CUID as a primary key in PostgreSQL
CREATE TABLE users (
id TEXT PRIMARY KEY DEFAULT gen_cuid(),
name TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
-- Prisma schema (auto-generates CUID by default)
model User {
id String @id @default(cuid())
name String
createdAt DateTime @default(now())
}