CUID2 Generator
सुरक्षित नेक्स्ट-जेनरेशन CUID2 आइडेंटिफायर जनरेट करें
जनरेट किए गए CUID2s
CUID2 क्या है?
CUID2 CUID की second generation है, जो cryptographic security और privacy पर focus करते हुए scratch से rewrite किया गया है।
CUID v1 के विपरीत जो timestamp visibly embed करता है, CUID2 सभी components को SHA3 hash के अंदर hide करता है, जो practically unpredictable strings produce करता है।
IDs एक customizable alphabetic prefix (default c) से शुरू होते हैं जिसके बाद SHA3 hash का base36 string होता है।
CUID2 क्यों?
CUID2 CUID v1 में specific security concerns के response में आया:
- Exposed timestamp: CUID v1 हर ID के शुरू में creation time reveal करता था।
- Predictability: CUID v1 structure knowledge के साथ, internal state का हिस्सा infer किया जा सकता था।
- Cryptographic security की कमी: CUID v1 explicitly cryptographically secure random number generator use नहीं करता था।
CUID2 SHA3, CSPRNG, और timestamp hiding use करके इन concerns को address करता है।
Design Principles
CUID2 vs CUID v1
दोनों generations के key characteristics की comparison:
| Attribute | CUID2 | CUID v1 |
|---|---|---|
| Security | Cryptographic (SHA3+CSPRNG) | Non-cryptographic |
| Predictability | Very low | Low (exposed timestamp) |
| Length | Customizable (default 24) | Fixed 25 characters |
| Prefix | Customizable | Fixed 'c' |
| Distribution | Evenly distributed | Timestamp-biased |
| Status | Active & recommended | Deprecated |
CUID2 vs UUID v4
Common UUID standard के साथ comparison:
| Attribute | CUID2 | UUID v4 |
|---|---|---|
| Default Length | 24 characters | 36 characters |
| URL-safe | हाँ | हाँ (hyphens के साथ) |
| Custom Length | हाँ | नहीं |
| Sortable | नहीं | नहीं |
| Entropy | High (SHA3) | High (CSPRNG) |
| Character Set | Lowercase alphanumeric | Hex + hyphens |
CUID2 UUID v4 के equivalent security के साथ guaranteed alphabetic prefix और flexible length provide करता है।
CUID2 कौन Use करता है?
CUID2 को prominent communities और frameworks ने adopt किया है:
- Prisma
@idfield में@default(cuid())के through ID type के रूप में CUID2 support करता है। - Drizzle ORM community default IDs के लिए CUID2 adopt करती है।
- SvelteKit और Remix projects session IDs के लिए इसे use करते हैं।
- High-volume API servers जिन्हें logs में traceable IDs चाहिए।
- Applications जो multiple databases या regions में sync करती हैं।
Code Examples
Installation और usage: pnpm add @paralleldrive/cuid2
import { createId } from '@paralleldrive/cuid2'
// Generate a single CUID2 (default length: 24)
const id = createId()
console.log(id) // e.g. "tz4a98xxat96iws9zmbrgj3a"
// Custom length
import { init } from '@paralleldrive/cuid2'
const createShortId = init({ length: 16 })
const shortId = createShortId()
console.log(shortId) // e.g. "tz4a98xxat96iws9"Advanced configuration:
model User {
id String @id @default(cuid())
email String @unique
name String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}Database integration:
async function generateCuid2(length = 24) {
const alphabet = 'abcdefghijklmnopqrstuvwxyz'
// Random prefix letter
const firstByte = crypto.getRandomValues(new Uint8Array(1))[0]
const firstChar = alphabet[firstByte % 26]
// Random 32-byte salt
const salt = crypto.getRandomValues(new Uint8Array(32))
const saltHex = [...salt].map(b => b.toString(16).padStart(2, '0')).join('')
// SHA-512 of timestamp + salt
const input = Date.now().toString(36) + saltHex
const hashBuffer = await crypto.subtle.digest(
'SHA-512',
new TextEncoder().encode(input)
)
// Encode hash bytes as base-36 string
const bytes = new Uint8Array(hashBuffer)
let hash = ''
for (let i = 0; i < bytes.length; i += 8) {
let chunk = 0n
for (let j = 0; j < 8 && i + j < bytes.length; j++) {
chunk = (chunk << 8n) | BigInt(bytes[i + j])
}
hash += chunk.toString(36)
}
return (firstChar + hash).slice(0, length)
}
// Usage
const id = await generateCuid2()
console.log(id) // e.g. "m7k3r9p2nxq8zt5a6cwj4bvd"