UUID 生成器 — 在线创建随机 UUID
这个 UUID 生成器使用 Web Crypto API 创建随机 UUID v4 值。可单个或批量生成 UUID,支持多种格式(带短横线、不带短横线、大括号包裹),适用于数据库主键、会话令牌和唯一标识符。
UUID Generator
Generate random UUID v4 values or validate existing UUIDs
Frequently Asked Questions
什么是UUID?
UUID(通用唯一标识符)是RFC 4122标准化的128位标识符。它以用连字符分隔的五组32个十六进制数字(8-4-4-4-12)表示。UUID设计为无需中央注册机构即可全局唯一,使其成为分布式系统、数据库和软件开发的理想选择。
UUID v4和v1有什么区别?
UUID v1由时间戳和主机MAC地址生成,提供时间排序但暴露主机身份。UUID v4使用随机或伪随机数生成,提供122位随机性。V4是使用最广泛的版本,因为它易于生成,没有隐私顾虑,并提供出色的唯一性保证。
UUID v4发生碰撞的概率是多少?
UUID v4具有122个随机位,产生约5.3×10³⁶个可能值。生成重复值的概率极小:您需要生成约2.71×10¹⁸(27.1京)个UUID才有50%的机会产生单次碰撞。实际上,以每秒10亿个UUID的速度生成85年,只有50%的概率产生一次重复。
UUID和GUID有什么区别?
UUID(通用唯一标识符)和GUID(全局唯一标识符)指的是相同的概念——128位唯一标识符。UUID是RFC 4122和大多数开源生态系统中使用的术语,而GUID是Microsoft在Windows和.NET中使用的术语。两种格式在功能上相同,尽管GUID有时以花括号显示。
我应该将UUID用作数据库主键吗?
UUID在多个节点独立生成ID的分布式数据库中效果良好。它们消除了对集中式自增序列的需求。但标准UUID v4值是随机的,可能导致B树索引碎片化,与顺序ID相比插入性能较慢。考虑使用UUID v7(时间排序)或ULID以获得更好的数据库性能。
UUID占多少字节?
UUID正好是16字节(128位)。以标准格式(8-4-4-4-12,带连字符)存储为字符串时,需要36个字符(32个十六进制数字+4个连字符)。为了存储效率,PostgreSQL等数据库具有原生UUID类型,将UUID存储为16字节而不是36字节字符串。
UUID适合用作令牌和密钥吗?
使用密码学安全随机数生成器(如crypto.randomUUID())生成的UUID v4提供122位随机性,足以用于会话令牌和CSRF令牌。但UUID并非设计为密钥——它们旨在唯一,而非不可预测。对于API密钥或认证令牌,使用专门构建的库来生成密码学安全的随机字符串。
应该使用哪种UUID格式?
标准连字符格式(8-4-4-4-12)是最常见且最具互操作性的。当存储空间很重要或需要URL友好标识符时,使用无连字符格式。对于Microsoft COM/DCOM兼容性,使用花括号格式({...})。在URN命名空间或XML文档中嵌入UUID时,使用URN格式(urn:uuid:...)。
我可以离线生成UUID吗?
可以。UUID v4只需要一个随机数来源,所有现代设备都可通过操作系统或浏览器获得。本工具使用Web Crypto API(crypto.randomUUID())完全在您的浏览器中生成UUID。不发出服务器请求,您的设备上的数据不会离开。
有哪些不同的UUID变体?
UUID中的变体字段(UUID的第64-65位)标识UUID的布局。最常见的是RFC 4122(变体1,以"10"的两个最高有效位表示)。其他变体包括NCS向后兼容(变体0)、Microsoft COM/DCOM(变体2)和供将来使用的保留变体。本工具生成的所有UUID都使用RFC 4122变体。
About UUID / GUID
A UUID (Universally Unique Identifier) is a 128-bit identifier standardized by RFC 4122. UUIDs are designed to be unique across all systems without requiring a central registration authority. They are commonly referred to as GUIDs (Globally Unique Identifiers) in the Microsoft ecosystem. UUIDs are used extensively in databases, distributed systems, APIs, and software development to identify resources without coordination between parties.
A standard UUID is represented as 32 hexadecimal digits displayed in five groups separated by hyphens: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx, where M indicates the version and N indicates the variant.
UUID Versions Explained
The UUID specification defines several versions, each with a different generation method:
| Version | Name | Description |
|---|---|---|
| v1 | Time-based | Generated from a timestamp and the MAC address of the host machine. Provides temporal ordering but exposes the host identity. |
| v2 | DCE Security | Similar to v1 but incorporates POSIX UID/GID. Rarely used in practice outside DCE environments. |
| v3 | Name-based (MD5) | Deterministic UUID generated by hashing a namespace and name with MD5. Same inputs always produce the same UUID. |
| v4 | Random | Generated using random or pseudo-random numbers. The most widely used version due to its simplicity and strong uniqueness guarantees. This tool generates v4 UUIDs. |
| v5 | Name-based (SHA-1) | Like v3 but uses SHA-1 instead of MD5. Preferred over v3 for new applications requiring deterministic UUIDs. |
UUID Format & Structure
A UUID consists of 128 bits (16 bytes) represented as 32 hexadecimal digits in the canonical form 8-4-4-4-12. The fields according to RFC 4122 are:
| Field | Hex Digits | Description |
|---|---|---|
| time_low | 8 | Low 32 bits of the timestamp |
| time_mid | 4 | Middle 16 bits of the timestamp |
| time_hi_and_version | 4 | High 4 bits are the version, lower 12 bits are the timestamp |
| clock_seq | 4 | Variant (2-3 bits) and clock sequence (13-14 bits) |
| node | 12 | 48-bit node identifier (typically MAC address for v1) |
For UUID v4, all fields except the version and variant bits are filled with random data, providing 122 bits of randomness.
UUID vs GUID
UUID and GUID refer to the same concept — a 128-bit unique identifier. The term "UUID" comes from the RFC 4122 standard and is used across Linux, macOS, and most programming languages. The term "GUID" (Globally Unique Identifier) originated at Microsoft and is prevalent in Windows, .NET, and COM development. The only practical differences are:
- Naming convention — RFC standards and most open-source projects use "UUID"; Microsoft documentation uses "GUID"
- Format representation — GUIDs are sometimes displayed with braces:
{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} - Byte ordering — Microsoft's GUID structure uses mixed-endian byte order internally, while RFC 4122 UUIDs use network byte order (big-endian)
Common Use Cases
- Database Primary Keys — UUIDs serve as globally unique primary keys that can be generated independently by any node, eliminating the need for auto-increment sequences and central ID servers
- Distributed Systems — Microservices, message queues, and event-driven architectures use UUIDs to identify messages, transactions, and correlation IDs without coordination
- Session Tokens — Web applications use UUIDs for session identifiers, CSRF tokens, and temporary access keys because their randomness makes them hard to predict
- API Resource Identifiers — RESTful APIs expose UUIDs in URLs (e.g.,
/api/users/550e8400-e29b-41d4-a716-446655440000) to avoid sequential ID enumeration - File & Object Storage — Cloud storage services use UUIDs to name blobs, files, and objects to prevent naming collisions across tenants
- Device & Component Identification — Bluetooth, USB, and other hardware protocols use UUIDs to identify services and characteristics
Related Tools
- Password Generator — Generate secure random passwords with customizable length and character sets
- Random Number Generator — Generate random integers and floating-point numbers within any range
- Base64 Encoder & Decoder — Encode and decode Base64 strings for data transmission