UUID v1 Generator
एम्बेडेड टाइमस्टैम्प के साथ टाइम-बेस्ड UUID v1 जनरेट करें
…
फ़ॉर्मेट करें
UUID v1 क्या है?
UUID v1 original UUID version है, RFC 4122 (2005) में standardised। यह generating host के MAC address के साथ high-precision timestamp combine करके unique identifiers generate करता है, plus sub-timestamp resolution handle करने के लिए short clock sequence।
क्योंकि timestamp embedded है, same host से UUID v1 values time के साथ monotonically increasing हैं — naturally ordered। यह distributed systems के लिए designed था जहाँ हर node बिना coordination के independently UUIDs generate कर सके।
आज UUID v1 largely UUID v7 (sortable, no MAC leakage) और UUID v4 (fully random, private) द्वारा superseded है। यह Apache Cassandra जैसे systems में use में रहता है।
UUID v1 की Anatomy
UUID v1 string जैसे 550e8400-e29b-11d4-a716-446655440000 छह distinct fields encode करती है:
| Field | Size | Description |
|---|---|---|
| time_low | 32 bits | 60-bit Gregorian timestamp का 32-bit low field (Oct 15, 1582 से 100-nanosecond intervals) |
| time_mid | 16 bits | 60-bit timestamp का middle 16-bit field |
| time_hi_and_version | 16 bits | 60-bit timestamp के top 12 bits plus 4-bit version number (हमेशा <code>1</code>) |
| clock_seq_hi_res | 8 bits | 6-bit clock sequence high field combined with 2-bit RFC 4122 variant marker |
| clock_seq_low | 8 bits | Clock sequence के lower 8 bits |
| node | 48 bits | 48-bit node identifier — typically generating network interface का MAC address, या यदि MAC available नहीं तो random 48-bit value |
Clock sequence field (clock_seq_hi_res + clock_seq_low) एक 14-bit counter है। यह तब increment होता है जब system clock backward move करे (जैसे NTP adjustment) या system restart हो। यह prevent करता है कि duplicate UUIDs generate हों।
UUID v1 Timestamp को Decode करना
60-bit timestamp UUID में तीन fields में scattered है। Generation time reconstruct करने के लिए:
time_low(bytes 0–3),time_mid(bytes 4–5), औरtime_hi(bytes 6–7, version nibble minus) extract करें- Reassemble:
(time_hi << 48) | (time_mid << 32) | time_low - Result October 15, 1582 से
100-nanosecond intervalsकी 60-bit count है (Gregorian calendar epoch) - Gregorian-to-Unix offset subtract करें: 122,192,928,000,000,000 (Oct 15 1582 और Jan 1 1970 के बीच 100-ns intervals)
- 100-nanosecond intervals को milliseconds में convert करने के लिए
10,000से divide करें - Date object construct करने के लिए
Unix millisecond timestampके रूप में use करें - Human-readable output के लिए
ISO 8601के रूप में format करें
Timestamp precision 100 nanoseconds है — UUID v7 के millisecond precision से far finer। हालाँकि, practice में ज़्यादातर operating systems sub-millisecond clock resolution expose नहीं करते।
Privacy Concerns
UUID v1 का सबसे significant drawback यह है कि यह node field में generating host का MAC address embed करता है। इसका मतलब है हर UUID v1 machine की permanent, globally unique fingerprint carry करता है।
एक adversary जो UUID v1 obtain करे वो determine कर सकता है: (1) ID generate होने का approximate time, (2) generating host का MAC address, और (3) multiple UUIDs analyze करके IDs generate होने की rate।
इस कारण, UUID v1 को कभी भी public-facing identifier के रूप में use नहीं करना चाहिए जब तक आप यह information disclose करने में comfortable न हों।
UUID v1 कब Still Appropriate है
UUID v1 vs UUID v7
UUID v7 time-ordered identifiers के लिए UUID v1 का modern successor है। यहाँ direct comparison है:
| Aspect | UUID v1 | UUID v7 |
|---|---|---|
| Epoch / Time Base | Gregorian epoch (Oct 15, 1582) | Unix epoch (Jan 1, 1970) |
| Precision | 100 nanoseconds | 1 millisecond |
| Node Identifier | MAC address (host identity leak करता है) | Random (private) |
| Privacy | MAC address और generation timestamp leak करता है | कोई host information embedded नहीं |
| DB index performance | Good — per host sequential | Excellent — सभी generators में k-sortable |
| Standard | RFC 4122 (2005) | RFC 9562 (2024) |
नए projects के लिए, UUID v7 UUID v1 के लिए recommended replacement है। यह similar time-ordering guarantees provide करता है बिना host MAC address embed करने के privacy implications के।
Code Examples
UUID v1 generation browsers या Node.js में natively available नहीं है। uuid npm package use करें:
// Generate a UUID v1 using the Web Crypto API
function generateUuidV1() {
const buf = new Uint8Array(16)
crypto.getRandomValues(buf)
const ms = BigInt(Date.now())
const gregorianOffset = 122192928000000000n
const t = ms * 10000n + gregorianOffset
const tLow = Number(t & 0xFFFFFFFFn)
const tMid = Number((t >> 32n) & 0xFFFFn)
const tHiVer = Number((t >> 48n) & 0x0FFFn) | 0x1000 // version 1
const clockSeq = (buf[8] & 0x3F) | 0x80 // variant 10xxxxxx
const clockSeqLow = buf[9]
const hex = (n, pad) => n.toString(16).padStart(pad, '0')
const node = [...buf.slice(10)].map(b => b.toString(16).padStart(2, '0')).join('')
return `${hex(tLow,8)}-${hex(tMid,4)}-${hex(tHiVer,4)}-${hex(clockSeq,2)}${hex(clockSeqLow,2)}-${node}`
}
// Extract the embedded timestamp from a UUID v1
function extractTimestamp(uuid) {
const parts = uuid.split('-')
const tHex = parts[2].slice(1) + parts[1] + parts[0]
const t = BigInt('0x' + tHex)
const ms = (t - 122192928000000000n) / 10000n
return new Date(Number(ms))
}
const id = generateUuidV1()
console.log(id) // e.g. "1eb5e8b0-6b4d-11ee-9c45-a1f2b3c4d5e6"
console.log(extractTimestamp(id)) // e.g. 2023-10-15T12:34:56.789Zimport uuid from datetime import datetime, timezone # Generate UUID v1 (uses MAC address by default) uid = uuid.uuid1() print(uid) # Extract embedded timestamp # uuid.time is 100-ns intervals since Oct 15, 1582 GREGORIAN_OFFSET = 122192928000000000 # 100-ns intervals ts_100ns = uid.time ts_ms = (ts_100ns - GREGORIAN_OFFSET) // 10000 dt = datetime.fromtimestamp(ts_ms / 1000, tz=timezone.utc) print(dt.isoformat()) # e.g. "2023-10-15T12:34:56.789000+00:00"
package main
import (
"fmt"
"time"
"github.com/google/uuid" // go get github.com/google/uuid
)
func main() {
id, _ := uuid.NewUUID() // UUID v1
fmt.Println(id)
// Extract timestamp from UUID v1
// uuid.Time is 100-ns ticks since Oct 15, 1582
t := id.Time()
sec := int64(t)/1e7 - 12219292800 // convert to Unix seconds
nsec := (int64(t) % 1e7) * 100
ts := time.Unix(sec, nsec).UTC()
fmt.Println(ts.Format(time.RFC3339Nano))
}