Network Working Group                                           P. Leach
Request for Comments: 4122                                     Microsoft
Category: Standards Track                                    M. Mealling
                                                Refactored Networks, LLC
                                                                 R. Salz
                                              DataPower Technology, Inc.
                                                               July 2005
        
Network Working Group                                           P. Leach
Request for Comments: 4122                                     Microsoft
Category: Standards Track                                    M. Mealling
                                                Refactored Networks, LLC
                                                                 R. Salz
                                              DataPower Technology, Inc.
                                                               July 2005
        

A Universally Unique IDentifier (UUID) URN Namespace

通用唯一标识符(UUID)URN命名空间

Status of This Memo

关于下段备忘

This document specifies an Internet standards track protocol for the Internet community, and requests discussion and suggestions for improvements. Please refer to the current edition of the "Internet Official Protocol Standards" (STD 1) for the standardization state and status of this protocol. Distribution of this memo is unlimited.

本文件规定了互联网社区的互联网标准跟踪协议,并要求进行讨论和提出改进建议。有关本协议的标准化状态和状态,请参考当前版本的“互联网官方协议标准”(STD 1)。本备忘录的分发不受限制。

Copyright Notice

版权公告

Copyright (C) The Internet Society (2005).

版权所有(C)互联网协会(2005年)。

Abstract

摘要

This specification defines a Uniform Resource Name namespace for UUIDs (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDentifier). A UUID is 128 bits long, and can guarantee uniqueness across space and time. UUIDs were originally used in the Apollo Network Computing System and later in the Open Software Foundation's (OSF) Distributed Computing Environment (DCE), and then in Microsoft Windows platforms.

该规范为UUID(通用唯一标识符)定义了统一的资源名称空间,也称为GUID(全局唯一标识符)。UUID的长度为128位,可以保证跨空间和时间的唯一性。UUID最初用于阿波罗网络计算系统,后来用于开放软件基金会(OSF)的分布式计算环境(DCE),然后用于Microsoft Windows平台。

This specification is derived from the DCE specification with the kind permission of the OSF (now known as The Open Group). Information from earlier versions of the DCE specification have been incorporated into this document.

此规范源自DCE规范,具有OSF(现在称为开放组)的种类权限。DCE规范早期版本的信息已纳入本文件。

Table of Contents

目录

   1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . .  2
   2. Motivation . . . . . . . . . . . . . . . . . . . . . . . . . .  3
   3. Namespace Registration Template  . . . . . . . . . . . . . . .  3
   4. Specification  . . . . . . . . . . . . . . . . . . . . . . . .  5
      4.1. Format. . . . . . . . . . . . . . . . . . . . . . . . . .  5
           4.1.1. Variant. . . . . . . . . . . . . . . . . . . . . .  6
           4.1.2. Layout and Byte Order. . . . . . . . . . . . . . .  6
           4.1.3. Version. . . . . . . . . . . . . . . . . . . . . .  7
           4.1.4. Timestamp. . . . . . . . . . . . . . . . . . . . .  8
           4.1.5. Clock Sequence . . . . . . . . . . . . . . . . . .  8
           4.1.6. Node . . . . . . . . . . . . . . . . . . . . . . .  9
           4.1.7. Nil UUID . . . . . . . . . . . . . . . . . . . . .  9
      4.2. Algorithms for Creating a Time-Based UUID . . . . . . . .  9
           4.2.1. Basic Algorithm. . . . . . . . . . . . . . . . . . 10
           4.2.2. Generation Details . . . . . . . . . . . . . . . . 12
      4.3. Algorithm for Creating a Name-Based UUID. . . . . . . . . 13
      4.4. Algorithms for Creating a UUID from Truly Random or
           Pseudo-Random Numbers . . . . . . . . . . . . . . . . . . 14
      4.5. Node IDs that Do Not Identify the Host. . . . . . . . . . 15
   5. Community Considerations . . . . . . . . . . . . . . . . . . . 15
   6. Security Considerations  . . . . . . . . . . . . . . . . . . . 16
   7. Acknowledgments  . . . . . . . . . . . . . . . . . . . . . . . 16
   8. Normative References . . . . . . . . . . . . . . . . . . . . . 16
   A. Appendix A - Sample Implementation . . . . . . . . . . . . . . 18
   B. Appendix B - Sample Output of utest  . . . . . . . . . . . . . 29
   C. Appendix C - Some Name Space IDs . . . . . . . . . . . . . . . 30
        
   1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . .  2
   2. Motivation . . . . . . . . . . . . . . . . . . . . . . . . . .  3
   3. Namespace Registration Template  . . . . . . . . . . . . . . .  3
   4. Specification  . . . . . . . . . . . . . . . . . . . . . . . .  5
      4.1. Format. . . . . . . . . . . . . . . . . . . . . . . . . .  5
           4.1.1. Variant. . . . . . . . . . . . . . . . . . . . . .  6
           4.1.2. Layout and Byte Order. . . . . . . . . . . . . . .  6
           4.1.3. Version. . . . . . . . . . . . . . . . . . . . . .  7
           4.1.4. Timestamp. . . . . . . . . . . . . . . . . . . . .  8
           4.1.5. Clock Sequence . . . . . . . . . . . . . . . . . .  8
           4.1.6. Node . . . . . . . . . . . . . . . . . . . . . . .  9
           4.1.7. Nil UUID . . . . . . . . . . . . . . . . . . . . .  9
      4.2. Algorithms for Creating a Time-Based UUID . . . . . . . .  9
           4.2.1. Basic Algorithm. . . . . . . . . . . . . . . . . . 10
           4.2.2. Generation Details . . . . . . . . . . . . . . . . 12
      4.3. Algorithm for Creating a Name-Based UUID. . . . . . . . . 13
      4.4. Algorithms for Creating a UUID from Truly Random or
           Pseudo-Random Numbers . . . . . . . . . . . . . . . . . . 14
      4.5. Node IDs that Do Not Identify the Host. . . . . . . . . . 15
   5. Community Considerations . . . . . . . . . . . . . . . . . . . 15
   6. Security Considerations  . . . . . . . . . . . . . . . . . . . 16
   7. Acknowledgments  . . . . . . . . . . . . . . . . . . . . . . . 16
   8. Normative References . . . . . . . . . . . . . . . . . . . . . 16
   A. Appendix A - Sample Implementation . . . . . . . . . . . . . . 18
   B. Appendix B - Sample Output of utest  . . . . . . . . . . . . . 29
   C. Appendix C - Some Name Space IDs . . . . . . . . . . . . . . . 30
        
1. Introduction
1. 介绍

This specification defines a Uniform Resource Name namespace for UUIDs (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDentifier). A UUID is 128 bits long, and requires no central registration process.

该规范为UUID(通用唯一标识符)定义了统一的资源名称空间,也称为GUID(全局唯一标识符)。UUID的长度为128位,不需要中央注册过程。

The information here is meant to be a concise guide for those wishing to implement services using UUIDs as URNs. Nothing in this document should be construed to override the DCE standards that defined UUIDs.

这里的信息旨在为那些希望使用UUID作为URN实现服务的人提供一个简明的指南。本文档中的任何内容都不应解释为覆盖定义UUID的DCE标准。

There is an ITU-T Recommendation and ISO/IEC Standard [3] that are derived from earlier versions of this document. Both sets of specifications have been aligned, and are fully technically compatible. In addition, a global registration function is being provided by the Telecommunications Standardisation Bureau of ITU-T; for details see <http://www.itu.int/ITU-T/asn1/uuid.html>.

ITU-T建议和ISO/IEC标准[3]源自本文件的早期版本。这两套规范都经过了调整,在技术上完全兼容。此外,ITU-T的电信标准化局正在提供全球注册功能;详情请参阅<http://www.itu.int/ITU-T/asn1/uuid.html>.

2. Motivation
2. 动机

One of the main reasons for using UUIDs is that no centralized authority is required to administer them (although one format uses IEEE 802 node identifiers, others do not). As a result, generation on demand can be completely automated, and used for a variety of purposes. The UUID generation algorithm described here supports very high allocation rates of up to 10 million per second per machine if necessary, so that they could even be used as transaction IDs.

使用UUID的一个主要原因是不需要集中的权限来管理它们(尽管一种格式使用IEEE 802节点标识符,但其他格式不使用)。因此,按需发电可以完全自动化,并用于各种目的。这里描述的UUID生成算法支持非常高的分配速率,如有必要,每台机器每秒最多可分配1000万个UUID,因此它们甚至可以用作事务ID。

UUIDs are of a fixed size (128 bits) which is reasonably small compared to other alternatives. This lends itself well to sorting, ordering, and hashing of all sorts, storing in databases, simple allocation, and ease of programming in general.

UUID具有固定的大小(128位),与其他替代方案相比,该大小相当小。这非常适合于各种排序、排序和散列,存储在数据库中,分配简单,编程简单。

Since UUIDs are unique and persistent, they make excellent Uniform Resource Names. The unique ability to generate a new UUID without a registration process allows for UUIDs to be one of the URNs with the lowest minting cost.

由于UUID是唯一的和持久的,所以它们是优秀的统一资源名称。无需注册过程即可生成新UUID的独特能力使UUID成为铸造成本最低的URN之一。

3. Namespace Registration Template
3. 命名空间注册模板

Namespace ID: UUID Registration Information: Registration date: 2003-10-01

命名空间ID:UUID注册信息:注册日期:2003-10-01

Declared registrant of the namespace: JTC 1/SC6 (ASN.1 Rapporteur Group)

名称空间的声明注册人:JTC 1/SC6(ASN.1报告员组)

Declaration of syntactic structure: A UUID is an identifier that is unique across both space and time, with respect to the space of all UUIDs. Since a UUID is a fixed size and contains a time field, it is possible for values to rollover (around A.D. 3400, depending on the specific algorithm used). A UUID can be used for multiple purposes, from tagging objects with an extremely short lifetime, to reliably identifying very persistent objects across a network.

语法结构声明:UUID是一个在空间和时间上都是唯一的标识符,相对于所有UUID的空间而言。由于UUID的大小是固定的,并且包含一个时间字段,因此值可以滚动(大约公元3400年,取决于使用的特定算法)。UUID可以用于多种用途,从标记生命周期极短的对象,到可靠地识别网络中非常持久的对象。

The internal representation of a UUID is a specific sequence of bits in memory, as described in Section 4. To accurately represent a UUID as a URN, it is necessary to convert the bit sequence to a string representation.

UUID的内部表示是内存中的特定位序列,如第4节所述。要将UUID准确地表示为URN,必须将位序列转换为字符串表示。

Each field is treated as an integer and has its value printed as a zero-filled hexadecimal digit string with the most significant digit first. The hexadecimal values "a" through "f" are output as lower case characters and are case insensitive on input.

每个字段都被视为一个整数,并将其值打印为一个以最高有效位开头的零填充十六进制数字字符串。十六进制值“a”到“f”作为小写字符输出,输入时不区分大小写。

The formal definition of the UUID string representation is provided by the following ABNF [7]:

UUID字符串表示形式的正式定义由以下ABNF[7]提供:

      UUID                   = time-low "-" time-mid "-"
                               time-high-and-version "-"
                               clock-seq-and-reserved
                               clock-seq-low "-" node
      time-low               = 4hexOctet
      time-mid               = 2hexOctet
      time-high-and-version  = 2hexOctet
      clock-seq-and-reserved = hexOctet
      clock-seq-low          = hexOctet
      node                   = 6hexOctet
      hexOctet               = hexDigit hexDigit
      hexDigit =
            "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
            "a" / "b" / "c" / "d" / "e" / "f" /
            "A" / "B" / "C" / "D" / "E" / "F"
        
      UUID                   = time-low "-" time-mid "-"
                               time-high-and-version "-"
                               clock-seq-and-reserved
                               clock-seq-low "-" node
      time-low               = 4hexOctet
      time-mid               = 2hexOctet
      time-high-and-version  = 2hexOctet
      clock-seq-and-reserved = hexOctet
      clock-seq-low          = hexOctet
      node                   = 6hexOctet
      hexOctet               = hexDigit hexDigit
      hexDigit =
            "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
            "a" / "b" / "c" / "d" / "e" / "f" /
            "A" / "B" / "C" / "D" / "E" / "F"
        

The following is an example of the string representation of a UUID as a URN:

以下是UUID作为URN的字符串表示的示例:

urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6

urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6

Relevant ancillary documentation: [1][2] Identifier uniqueness considerations: This document specifies three algorithms to generate UUIDs: the first leverages the unique values of 802 MAC addresses to guarantee uniqueness, the second uses pseudo-random number generators, and the third uses cryptographic hashing and application-provided text strings. As a result, the UUIDs generated according to the mechanisms here will be unique from all other UUIDs that have been or will be assigned.

相关辅助文档:[1][2]标识符唯一性注意事项:本文档指定了三种生成UUID的算法:第一种算法利用802 MAC地址的唯一值来保证唯一性,第二种算法使用伪随机数生成器,第三种使用加密哈希和应用程序提供的文本字符串。因此,根据此处的机制生成的UUID将与已分配或将分配的所有其他UUID不同。

Identifier persistence considerations: UUIDs are inherently very difficult to resolve in a global sense. This, coupled with the fact that UUIDs are temporally unique within their spatial context, ensures that UUIDs will remain as persistent as possible.

标识符持久性注意事项:UUID本质上很难在全局意义上解决。这一点,再加上UUID在其空间上下文中在时间上是唯一的,确保UUID将尽可能保持持久性。

Process of identifier assignment: Generating a UUID does not require that a registration authority be contacted. One algorithm requires a unique value over space for each generator. This value is typically an IEEE 802 MAC address, usually already available on network-connected hosts. The address can be assigned from an address block obtained from the IEEE registration authority. If no such address is available,

标识符分配过程:生成UUID不需要联系注册机构。一种算法要求每个生成器在空间上具有唯一的值。该值通常是IEEE 802 MAC地址,通常在网络连接的主机上已经可用。地址可以从IEEE注册机构获得的地址块分配。如果没有这样的地址,

or privacy concerns make its use undesirable, Section 4.5 specifies two alternatives. Another approach is to use version 3 or version 4 UUIDs as defined below.

或者隐私问题使其使用不受欢迎,第4.5节规定了两种选择。另一种方法是使用下面定义的版本3或版本4 UUID。

Process for identifier resolution: Since UUIDs are not globally resolvable, this is not applicable.

标识符解析过程:由于UUID不可全局解析,因此不适用。

Rules for Lexical Equivalence: Consider each field of the UUID to be an unsigned integer as shown in the table in section Section 4.1.2. Then, to compare a pair of UUIDs, arithmetically compare the corresponding fields from each UUID in order of significance and according to their data type. Two UUIDs are equal if and only if all the corresponding fields are equal.

词汇等价规则:将UUID的每个字段视为无符号整数,如第4.1.2节的表所示。然后,为了比较一对UUID,根据重要性顺序和数据类型,对每个UUID的相应字段进行算术比较。当且仅当所有对应字段相等时,两个UUID相等。

As an implementation note, equality comparison can be performed on many systems by doing the appropriate byte-order canonicalization, and then treating the two UUIDs as 128-bit unsigned integers.

作为一个实现说明,通过执行适当的字节顺序规范化,然后将两个UUID视为128位无符号整数,可以在许多系统上执行相等性比较。

UUIDs, as defined in this document, can also be ordered lexicographically. For a pair of UUIDs, the first one follows the second if the most significant field in which the UUIDs differ is greater for the first UUID. The second precedes the first if the most significant field in which the UUIDs differ is greater for the second UUID.

本文档中定义的UUID也可以按字典顺序排列。对于一对UUID,如果UUID之间差异最大的字段对于第一个UUID更大,则第一个UUID紧跟第二个UUID。如果第二个UUID中UUID差异最大的字段大于第一个UUID,则第二个UUID优先于第一个UUID。

Conformance with URN Syntax: The string representation of a UUID is fully compatible with the URN syntax. When converting from a bit-oriented, in-memory representation of a UUID into a URN, care must be taken to strictly adhere to the byte order issues mentioned in the string representation section.

与URN语法的一致性:UUID的字符串表示形式与URN语法完全兼容。当从UUID的面向位内存表示转换为URN时,必须注意严格遵守字符串表示部分中提到的字节顺序问题。

Validation mechanism: Apart from determining whether the timestamp portion of the UUID is in the future and therefore not yet assignable, there is no mechanism for determining whether a UUID is 'valid'.

验证机制:除了确定UUID的时间戳部分是否在将来,因此不可分配之外,没有确定UUID是否“有效”的机制。

Scope: UUIDs are global in scope.

作用域:UUID在作用域中是全局的。

4. Specification
4. 规格
4.1. Format
4.1. 总体安排

The UUID format is 16 octets; some bits of the eight octet variant field specified below determine finer structure.

UUID格式为16个八位字节;下面指定的八个八位组变量字段的某些位决定了更精细的结构。

4.1.1. Variant
4.1.1. 变种

The variant field determines the layout of the UUID. That is, the interpretation of all other bits in the UUID depends on the setting of the bits in the variant field. As such, it could more accurately be called a type field; we retain the original term for compatibility. The variant field consists of a variable number of the most significant bits of octet 8 of the UUID.

变量字段确定UUID的布局。也就是说,UUID中所有其他位的解释取决于变量字段中位的设置。因此,它可以更准确地称为类型字段;我们保留最初的兼容性术语。变量字段由UUID的八位字节8的可变最高有效位组成。

The following table lists the contents of the variant field, where the letter "x" indicates a "don't-care" value.

下表列出了变量字段的内容,其中字母“x”表示“不关心”值。

Msb0 Msb1 Msb2 Description

Msb0 Msb1 Msb2说明

0 x x Reserved, NCS backward compatibility.

0 x x保留,NCS向后兼容。

1 0 x The variant specified in this document.

1 0 x本文件中指定的变量。

1 1 0 Reserved, Microsoft Corporation backward compatibility

1 10保留,Microsoft Corporation向后兼容

1 1 1 Reserved for future definition.

1保留供将来定义。

Interoperability, in any form, with variants other than the one defined here is not guaranteed, and is not likely to be an issue in practice.

与此处定义的变体以外的变体的任何形式的互操作性都不保证,在实践中也不可能成为问题。

4.1.2. Layout and Byte Order
4.1.2. 布局和字节顺序

To minimize confusion about bit assignments within octets, the UUID record definition is defined only in terms of fields that are integral numbers of octets. The fields are presented with the most significant one first.

为了最大限度地减少对八位字节内位分配的混淆,UUID记录定义仅根据八位字节整数字段定义。字段首先显示最重要的字段。

Field Data Type Octet Note #

字段数据类型八位字节注释#

time_low unsigned 32 0-3 The low field of the bit integer timestamp

time_low无符号32 0-3位整数时间戳的低位字段

time_mid unsigned 16 4-5 The middle field of the bit integer timestamp

time_mid unsigned 16 4-5位整数时间戳的中间字段

time_hi_and_version unsigned 16 6-7 The high field of the bit integer timestamp multiplexed with the version number

time_hi_和_version unsigned 16 6-7与版本号多路复用的位整数时间戳的高位字段

clock_seq_hi_and_rese unsigned 8 8 The high field of the rved bit integer clock sequence multiplexed with the variant

clock_seq_hi_和_rese unsigned 8与变量多路复用的rved位整数时钟序列的高位字段

clock_seq_low unsigned 8 9 The low field of the bit integer clock sequence

时钟顺序低无符号8 9位整数时钟序列的低位字段

node unsigned 48 10-15 The spatially unique bit integer node identifier

节点无符号48 10-15空间唯一位整数节点标识符

In the absence of explicit application or presentation protocol specification to the contrary, a UUID is encoded as a 128-bit object, as follows:

相反,在没有明确的应用程序或表示协议规范的情况下,UUID被编码为128位对象,如下所示:

The fields are encoded as 16 octets, with the sizes and order of the fields defined above, and with each field encoded with the Most Significant Byte first (known as network byte order). Note that the field names, particularly for multiplexed fields, follow historical practice.

字段被编码为16个八位字节,字段的大小和顺序如上所述,每个字段首先以最高有效字节编码(称为网络字节顺序)。请注意,字段名称,特别是多路复用字段的名称,遵循历史惯例。

   0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                          time_low                             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |       time_mid                |         time_hi_and_version   |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                         node (2-5)                            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        
   0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                          time_low                             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |       time_mid                |         time_hi_and_version   |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                         node (2-5)                            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        
4.1.3. Version
4.1.3. 版本

The version number is in the most significant 4 bits of the time stamp (bits 4 through 7 of the time_hi_and_version field).

版本号位于时间戳的最高有效4位(时间\高\和\版本字段的第4位到第7位)。

The following table lists the currently-defined versions for this UUID variant.

下表列出了此UUID变体的当前定义版本。

Msb0 Msb1 Msb2 Msb3 Version Description

Msb0 Msb1 Msb2 Msb3版本说明

0 0 0 1 1 The time-based version specified in this document.

0 1此文档中指定的基于时间的版本。

0 0 1 0 2 DCE Security version, with embedded POSIX UIDs.

0 1 0 2 DCE安全版本,带有嵌入式POSIX UID。

0 0 1 1 3 The name-based version specified in this document that uses MD5 hashing.

0 0 1 3此文档中指定的使用MD5哈希的基于名称的版本。

0 1 0 0 4 The randomly or pseudo-randomly generated version specified in this document.

0 1 0 4此文档中指定的随机或伪随机生成的版本。

0 1 0 1 5 The name-based version specified in this document that uses SHA-1 hashing.

0 1 0 1 5此文档中指定的使用SHA-1哈希的基于名称的版本。

The version is more accurately a sub-type; again, we retain the term for compatibility.

版本更准确地说是一个子类型;同样,我们保留了兼容性这个术语。

4.1.4. Timestamp
4.1.4. 时间戳

The timestamp is a 60-bit value. For UUID version 1, this is represented by Coordinated Universal Time (UTC) as a count of 100- nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to the Christian calendar).

时间戳是一个60位的值。对于UUID版本1,这由协调世界时(UTC)表示为自1582年10月15日00:00.00(公历改为基督教日历的日期)起的100纳秒间隔计数。

For systems that do not have UTC available, but do have the local time, they may use that instead of UTC, as long as they do so consistently throughout the system. However, this is not recommended since generating the UTC from local time only needs a time zone offset.

对于没有UTC可用,但有本地时间的系统,只要在整个系统中保持一致,就可以使用该时间代替UTC。但是,不建议这样做,因为从本地时间生成UTC只需要时区偏移。

For UUID version 3 or 5, the timestamp is a 60-bit value constructed from a name as described in Section 4.3.

对于UUID版本3或5,时间戳是根据第4.3节中描述的名称构造的60位值。

For UUID version 4, the timestamp is a randomly or pseudo-randomly generated 60-bit value, as described in Section 4.4.

对于UUID版本4,时间戳是随机或伪随机生成的60位值,如第4.4节所述。

4.1.5. Clock Sequence
4.1.5. 时钟序列

For UUID version 1, the clock sequence is used to help avoid duplicates that could arise when the clock is set backwards in time or if the node ID changes.

对于UUID版本1,时钟序列用于帮助避免在时钟向后设置或节点ID更改时可能出现的重复。

If the clock is set backwards, or might have been set backwards (e.g., while the system was powered off), and the UUID generator can not be sure that no UUIDs were generated with timestamps larger than the value to which the clock was set, then the clock sequence has to be changed. If the previous value of the clock sequence is known, it can just be incremented; otherwise it should be set to a random or high-quality pseudo-random value.

如果时钟设置为向后,或者可能设置为向后(例如,当系统断电时),并且UUID生成器无法确保没有生成时间戳大于时钟设置值的UUID,则必须更改时钟序列。如果已知时钟序列的前一个值,则可以增加该值;否则,应将其设置为随机或高质量伪随机值。

Similarly, if the node ID changes (e.g., because a network card has been moved between machines), setting the clock sequence to a random number minimizes the probability of a duplicate due to slight differences in the clock settings of the machines. If the value of clock sequence associated with the changed node ID were known, then the clock sequence could just be incremented, but that is unlikely.

类似地,如果节点ID改变(例如,因为网卡已经在机器之间移动),则将时钟序列设置为随机数将最小化由于机器的时钟设置中的微小差异而产生重复的概率。如果已知与更改的节点ID相关联的时钟序列的值,则时钟序列可以只是递增,但这是不可能的。

The clock sequence MUST be originally (i.e., once in the lifetime of a system) initialized to a random number to minimize the correlation across systems. This provides maximum protection against node identifiers that may move or switch from system to system rapidly. The initial value MUST NOT be correlated to the node identifier.

时钟序列最初必须初始化为随机数(即在系统生命周期内初始化一次),以最小化系统间的相关性。这对可能在系统之间快速移动或切换的节点标识符提供了最大程度的保护。初始值不得与节点标识符关联。

For UUID version 3 or 5, the clock sequence is a 14-bit value constructed from a name as described in Section 4.3.

对于UUID版本3或5,时钟序列是由第4.3节中描述的名称构成的14位值。

For UUID version 4, clock sequence is a randomly or pseudo-randomly generated 14-bit value as described in Section 4.4.

对于UUID版本4,时钟序列是一个随机或伪随机生成的14位值,如第4.4节所述。

4.1.6. Node
4.1.6. 节点

For UUID version 1, the node field consists of an IEEE 802 MAC address, usually the host address. For systems with multiple IEEE 802 addresses, any available one can be used. The lowest addressed octet (octet number 10) contains the global/local bit and the unicast/multicast bit, and is the first octet of the address transmitted on an 802.3 LAN.

对于UUID版本1,节点字段由IEEE 802 MAC地址组成,通常是主机地址。对于具有多个IEEE 802地址的系统,可以使用任何可用的地址。最低寻址八位组(八位组编号10)包含全局/本地位和单播/多播位,是802.3 LAN上传输的地址的第一个八位组。

For systems with no IEEE address, a randomly or pseudo-randomly generated value may be used; see Section 4.5. The multicast bit must be set in such addresses, in order that they will never conflict with addresses obtained from network cards.

对于没有IEEE地址的系统,可以使用随机或伪随机生成的值;见第4.5节。必须在这些地址中设置多播位,以便它们永远不会与从网卡获得的地址冲突。

For UUID version 3 or 5, the node field is a 48-bit value constructed from a name as described in Section 4.3.

对于UUID版本3或5,节点字段是由第4.3节中描述的名称构造的48位值。

For UUID version 4, the node field is a randomly or pseudo-randomly generated 48-bit value as described in Section 4.4.

对于UUID版本4,节点字段是一个随机或伪随机生成的48位值,如第4.4节所述。

4.1.7. Nil UUID
4.1.7. 无UUID

The nil UUID is special form of UUID that is specified to have all 128 bits set to zero.

nil UUID是UUID的一种特殊形式,指定将所有128位设置为零。

4.2. Algorithms for Creating a Time-Based UUID
4.2. 创建基于时间的UUID的算法

Various aspects of the algorithm for creating a version 1 UUID are discussed in the following sections.

以下各节将讨论用于创建版本1 UUID的算法的各个方面。

4.2.1. Basic Algorithm
4.2.1. 基本算法

The following algorithm is simple, correct, and inefficient:

以下算法简单、正确且效率低下:

o Obtain a system-wide global lock

o 获取系统范围的全局锁

o From a system-wide shared stable store (e.g., a file), read the UUID generator state: the values of the timestamp, clock sequence, and node ID used to generate the last UUID.

o 从系统范围的共享稳定存储(例如,文件)读取UUID生成器状态:用于生成最后一个UUID的时间戳、时钟序列和节点ID的值。

o Get the current time as a 60-bit count of 100-nanosecond intervals since 00:00:00.00, 15 October 1582.

o 从1582年10月15日00:00:00.00开始,以100纳秒间隔的60位计数获取当前时间。

o Get the current node ID.

o 获取当前节点ID。

o If the state was unavailable (e.g., non-existent or corrupted), or the saved node ID is different than the current node ID, generate a random clock sequence value.

o 如果状态不可用(例如,不存在或损坏),或者保存的节点ID与当前节点ID不同,则生成随机时钟序列值。

o If the state was available, but the saved timestamp is later than the current timestamp, increment the clock sequence value.

o 如果状态可用,但保存的时间戳晚于当前时间戳,则增加时钟序列值。

o Save the state (current timestamp, clock sequence, and node ID) back to the stable store.

o 将状态(当前时间戳、时钟序列和节点ID)保存回稳定存储。

o Release the global lock.

o 释放全局锁。

o Format a UUID from the current timestamp, clock sequence, and node ID values according to the steps in Section 4.2.2.

o 根据第4.2.2节中的步骤,根据当前时间戳、时钟序列和节点ID值格式化UUID。

If UUIDs do not need to be frequently generated, the above algorithm may be perfectly adequate. For higher performance requirements, however, issues with the basic algorithm include:

如果UUID不需要频繁生成,那么上述算法可能就足够了。但是,对于更高的性能要求,基本算法的问题包括:

o Reading the state from stable storage each time is inefficient.

o 每次从稳定存储器读取状态都是低效的。

o The resolution of the system clock may not be 100-nanoseconds.

o 系统时钟的分辨率可能不是100纳秒。

o Writing the state to stable storage each time is inefficient.

o 每次将状态写入稳定存储都是低效的。

o Sharing the state across process boundaries may be inefficient.

o 跨进程边界共享状态可能效率低下。

Each of these issues can be addressed in a modular fashion by local improvements in the functions that read and write the state and read the clock. We address each of them in turn in the following sections.

通过对读写状态和读时钟功能的局部改进,可以以模块化的方式解决这些问题。我们将在以下各节中依次介绍它们。

4.2.1.1. Reading Stable Storage
4.2.1.1. 读取稳定存储器

The state only needs to be read from stable storage once at boot time, if it is read into a system-wide shared volatile store (and updated whenever the stable store is updated).

如果状态被读取到系统范围的共享易失性存储中(并在稳定存储更新时更新),则在启动时只需要从稳定存储中读取一次状态。

If an implementation does not have any stable store available, then it can always say that the values were unavailable. This is the least desirable implementation because it will increase the frequency of creation of new clock sequence numbers, which increases the probability of duplicates.

如果一个实现没有任何可用的稳定存储,那么它总是可以说这些值不可用。这是最不理想的实现,因为它将增加创建新时钟序列号的频率,从而增加重复的概率。

If the node ID can never change (e.g., the net card is inseparable from the system), or if any change also reinitializes the clock sequence to a random value, then instead of keeping it in stable store, the current node ID may be returned.

如果节点ID永远无法更改(例如,网卡与系统不可分离),或者如果任何更改也会将时钟序列重新初始化为随机值,则可以返回当前节点ID,而不是将其保持在稳定存储中。

4.2.1.2. System Clock Resolution
4.2.1.2. 系统时钟分辨率

The timestamp is generated from the system time, whose resolution may be less than the resolution of the UUID timestamp.

时间戳由系统时间生成,其分辨率可能小于UUID时间戳的分辨率。

If UUIDs do not need to be frequently generated, the timestamp can simply be the system time multiplied by the number of 100-nanosecond intervals per system time interval.

如果UUID不需要频繁生成,则时间戳可以是系统时间乘以每个系统时间间隔的100纳秒间隔数。

If a system overruns the generator by requesting too many UUIDs within a single system time interval, the UUID service MUST either return an error, or stall the UUID generator until the system clock catches up.

如果系统在单个系统时间间隔内请求过多UUID,从而使生成器超限,UUID服务必须返回错误,或暂停UUID生成器,直到系统时钟赶上。

A high resolution timestamp can be simulated by keeping a count of the number of UUIDs that have been generated with the same value of the system time, and using it to construct the low order bits of the timestamp. The count will range between zero and the number of 100-nanosecond intervals per system time interval.

高分辨率时间戳可以通过保持使用相同系统时间值生成的uuid数量的计数来模拟,并使用它来构造时间戳的低阶位。计数范围为0到每个系统时间间隔100纳秒的间隔数。

Note: If the processors overrun the UUID generation frequently, additional node identifiers can be allocated to the system, which will permit higher speed allocation by making multiple UUIDs potentially available for each time stamp value.

注意:如果处理器经常超出UUID生成,则可以向系统分配额外的节点标识符,这将允许通过为每个时间戳值提供多个UUID来实现更高的速度分配。

4.2.1.3. Writing Stable Storage
4.2.1.3. 写稳定存储器

The state does not always need to be written to stable store every time a UUID is generated. The timestamp in the stable store can be periodically set to a value larger than any yet used in a UUID. As long as the generated UUIDs have timestamps less than that value, and

并不总是需要在每次生成UUID时将状态写入稳定存储。稳定存储中的时间戳可以周期性地设置为大于UUID中使用的任何时间戳的值。只要生成的UUID的时间戳小于该值

the clock sequence and node ID remain unchanged, only the shared volatile copy of the state needs to be updated. Furthermore, if the timestamp value in stable store is in the future by less than the typical time it takes the system to reboot, a crash will not cause a reinitialization of the clock sequence.

时钟序列和节点ID保持不变,只需更新状态的共享易失性副本。此外,如果stable store中的时间戳值在将来小于系统重新启动所需的典型时间,则崩溃不会导致时钟序列的重新初始化。

4.2.1.4. Sharing State Across Processes
4.2.1.4. 跨进程共享状态

If it is too expensive to access shared state each time a UUID is generated, then the system-wide generator can be implemented to allocate a block of time stamps each time it is called; a per-process generator can allocate from that block until it is exhausted.

如果每次生成UUID时访问共享状态的成本太高,那么可以实现系统范围的生成器,以便在每次调用它时分配时间戳块;每个进程生成器可以从该块进行分配,直到其耗尽。

4.2.2. Generation Details
4.2.2. 生成详细信息

Version 1 UUIDs are generated according to the following algorithm:

版本1 UUID根据以下算法生成:

o Determine the values for the UTC-based timestamp and clock sequence to be used in the UUID, as described in Section 4.2.1.

o 确定UUID中使用的基于UTC的时间戳和时钟序列的值,如第4.2.1节所述。

o For the purposes of this algorithm, consider the timestamp to be a 60-bit unsigned integer and the clock sequence to be a 14-bit unsigned integer. Sequentially number the bits in a field, starting with zero for the least significant bit.

o 为了这个算法的目的,考虑时间戳是一个60位无符号整数,时钟序列是14位无符号整数。按顺序对字段中的位进行编号,最低有效位从零开始。

o Set the time_low field equal to the least significant 32 bits (bits zero through 31) of the timestamp in the same order of significance.

o 将time_low字段设置为时间戳的最低有效32位(位0到31),其重要性顺序相同。

o Set the time_mid field equal to bits 32 through 47 from the timestamp in the same order of significance.

o 将time_mid field设置为时间戳中的位32到47,其重要性顺序相同。

o Set the 12 least significant bits (bits zero through 11) of the time_hi_and_version field equal to bits 48 through 59 from the timestamp in the same order of significance.

o 将time_hi_和_version字段的12个最低有效位(位0到11)设置为与时间戳中的位48到59相同的重要顺序。

o Set the four most significant bits (bits 12 through 15) of the time_hi_and_version field to the 4-bit version number corresponding to the UUID version being created, as shown in the table above.

o 将time_hi_和_version字段的四个最高有效位(位12到15)设置为与正在创建的UUID版本相对应的4位版本号,如上表所示。

o Set the clock_seq_low field to the eight least significant bits (bits zero through 7) of the clock sequence in the same order of significance.

o 将时钟顺序低字段设置为时钟序列的八个最低有效位(位0到7),其重要性顺序相同。

o Set the 6 least significant bits (bits zero through 5) of the clock_seq_hi_and_reserved field to the 6 most significant bits (bits 8 through 13) of the clock sequence in the same order of significance.

o 按照相同的重要性顺序,将时钟序列的6个最低有效位(位0到5)设置为时钟序列的6个最高有效位(位8到13)。

o Set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively.

o 将时钟_seq_hi_和_保留的两个最高有效位(位6和7)分别设置为0和1。

o Set the node field to the 48-bit IEEE address in the same order of significance as the address.

o 将节点字段设置为48位IEEE地址,其重要性顺序与地址相同。

4.3. Algorithm for Creating a Name-Based UUID
4.3. 创建基于名称的UUID的算法

The version 3 or 5 UUID is meant for generating UUIDs from "names" that are drawn from, and unique within, some "name space". The concept of name and name space should be broadly construed, and not limited to textual names. For example, some name spaces are the domain name system, URLs, ISO Object IDs (OIDs), X.500 Distinguished Names (DNs), and reserved words in a programming language. The mechanisms or conventions used for allocating names and ensuring their uniqueness within their name spaces are beyond the scope of this specification.

版本3或版本5 UUID用于从某些“名称空间”中提取并在其中唯一的“名称”生成UUID。名称和名称空间的概念应作广义解释,而不限于文本名称。例如,一些名称空间是域名系统、URL、ISO对象ID(OID)、X.500可分辨名称(DNs)和编程语言中的保留字。用于分配名称并确保其在名称空间中的唯一性的机制或约定超出了本规范的范围。

The requirements for these types of UUIDs are as follows:

这些类型UUID的要求如下:

o The UUIDs generated at different times from the same name in the same namespace MUST be equal.

o 在不同时间从相同名称空间中的相同名称生成的UUID必须相等。

o The UUIDs generated from two different names in the same namespace should be different (with very high probability).

o 由同一名称空间中的两个不同名称生成的UUID应该不同(概率非常高)。

o The UUIDs generated from the same name in two different namespaces should be different with (very high probability).

o 从两个不同名称空间中的相同名称生成的UUID应该不同(概率非常高)。

o If two UUIDs that were generated from names are equal, then they were generated from the same name in the same namespace (with very high probability).

o 如果从名称生成的两个UUID相等,则它们是从相同名称空间中的相同名称生成的(概率非常高)。

The algorithm for generating a UUID from a name and a name space are as follows:

从名称和名称空间生成UUID的算法如下:

o Allocate a UUID to use as a "name space ID" for all UUIDs generated from names in that name space; see Appendix C for some pre-defined values.

o 分配一个UUID,用作该名称空间中名称生成的所有UUID的“名称空间ID”;有关一些预定义值,请参见附录C。

o Choose either MD5 [4] or SHA-1 [8] as the hash algorithm; If backward compatibility is not an issue, SHA-1 is preferred.

o 选择MD5[4]或SHA-1[8]作为哈希算法;如果向后兼容性不是问题,则首选SHA-1。

o Convert the name to a canonical sequence of octets (as defined by the standards or conventions of its name space); put the name space ID in network byte order.

o 将名称转换为八位字节的规范序列(由其名称空间的标准或约定定义);将名称空间ID按网络字节顺序排列。

o Compute the hash of the name space ID concatenated with the name.

o 计算与名称连接的名称空间ID的哈希值。

o Set octets zero through 3 of the time_low field to octets zero through 3 of the hash.

o 将time_low字段的0到3个八位字节设置为哈希值的0到3个八位字节。

o Set octets zero and one of the time_mid field to octets 4 and 5 of the hash.

o 将时间_中间字段的八位位组0和一设置为哈希的八位位组4和5。

o Set octets zero and one of the time_hi_and_version field to octets 6 and 7 of the hash.

o 将时间_hi_和_version字段的八位字节设置为0,并将其中一个设置为哈希的八位字节6和7。

o Set the four most significant bits (bits 12 through 15) of the time_hi_and_version field to the appropriate 4-bit version number from Section 4.1.3.

o 将时间\u hi\u和\u版本字段的四个最高有效位(位12至15)设置为第4.1.3节中的相应4位版本号。

o Set the clock_seq_hi_and_reserved field to octet 8 of the hash.

o 将clock_seq_hi_和_reserved字段设置为哈希的八位字节8。

o Set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively.

o 将时钟_seq_hi_和_保留的两个最高有效位(位6和7)分别设置为0和1。

o Set the clock_seq_low field to octet 9 of the hash.

o 将clock_seq_low字段设置为哈希的八位字节9。

o Set octets zero through five of the node field to octets 10 through 15 of the hash.

o 将节点字段的八位字节0到5设置为哈希值的八位字节10到15。

o Convert the resulting UUID to local byte order.

o 将生成的UUID转换为本地字节顺序。

4.4. Algorithms for Creating a UUID from Truly Random or Pseudo-Random Numbers

4.4. 从真随机数或伪随机数创建UUID的算法

The version 4 UUID is meant for generating UUIDs from truly-random or pseudo-random numbers.

版本4 UUID用于从真正的随机数或伪随机数生成UUID。

The algorithm is as follows:

算法如下:

o Set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively.

o 将时钟_seq_hi_和_保留的两个最高有效位(位6和7)分别设置为0和1。

o Set the four most significant bits (bits 12 through 15) of the time_hi_and_version field to the 4-bit version number from Section 4.1.3.

o 将时间\u hi\u和\u版本字段的四个最高有效位(位12至15)设置为第4.1.3节中的4位版本号。

o Set all the other bits to randomly (or pseudo-randomly) chosen values.

o 将所有其他位设置为随机(或伪随机)选择的值。

See Section 4.5 for a discussion on random numbers.

有关随机数的讨论,请参见第4.5节。

4.5. Node IDs that Do Not Identify the Host
4.5. 不标识主机的节点ID

This section describes how to generate a version 1 UUID if an IEEE 802 address is not available, or its use is not desired.

本节介绍在IEEE 802地址不可用或不需要使用时如何生成版本1 UUID。

One approach is to contact the IEEE and get a separate block of addresses. At the time of writing, the application could be found at <http://standards.ieee.org/regauth/oui/pilot-ind.html>, and the cost was US$550.

一种方法是联系IEEE并获得单独的地址块。在撰写本文时,可在以下网址找到该应用程序:<http://standards.ieee.org/regauth/oui/pilot-ind.html>,费用为550美元。

A better solution is to obtain a 47-bit cryptographic quality random number and use it as the low 47 bits of the node ID, with the least significant bit of the first octet of the node ID set to one. This bit is the unicast/multicast bit, which will never be set in IEEE 802 addresses obtained from network cards. Hence, there can never be a conflict between UUIDs generated by machines with and without network cards. (Recall that the IEEE 802 spec talks about transmission order, which is the opposite of the in-memory representation that is discussed in this document.)

更好的解决方案是获得47位加密质量随机数,并将其用作节点ID的低47位,节点ID的第一个八位组的最低有效位设置为1。该位是单播/多播位,它永远不会在从网卡获得的IEEE 802地址中设置。因此,由具有和不具有网卡的机器生成的UUID之间永远不会存在冲突。(请记住,IEEE 802规范讨论了传输顺序,这与本文档中讨论的内存表示相反。)

For compatibility with earlier specifications, note that this document uses the unicast/multicast bit, instead of the arguably more correct local/global bit.

为了与早期规范兼容,请注意,本文档使用单播/多播位,而不是更正确的本地/全局位。

Advice on generating cryptographic-quality random numbers can be found in RFC1750 [5].

有关生成加密质量随机数的建议,请参见RFC1750[5]。

In addition, items such as the computer's name and the name of the operating system, while not strictly speaking random, will help differentiate the results from those obtained by other systems.

此外,诸如计算机名称和操作系统名称等项目虽然严格来说不是随机的,但有助于将结果与其他系统获得的结果区分开来。

The exact algorithm to generate a node ID using these data is system specific, because both the data available and the functions to obtain them are often very system specific. A generic approach, however, is to accumulate as many sources as possible into a buffer, use a message digest such as MD5 [4] or SHA-1 [8], take an arbitrary 6 bytes from the hash value, and set the multicast bit as described above.

使用这些数据生成节点ID的精确算法是特定于系统的,因为可用数据和获取它们的函数通常都是特定于系统的。然而,一种通用方法是将尽可能多的源累积到缓冲区中,使用消息摘要(如MD5[4]或SHA-1[8]),从哈希值中提取任意6个字节,并如上所述设置多播位。

5. Community Considerations
5. 社区考虑

The use of UUIDs is extremely pervasive in computing. They comprise the core identifier infrastructure for many operating systems (Microsoft Windows) and applications (the Mozilla browser) and in many cases, become exposed to the Web in many non-standard ways.

UUID的使用在计算中非常普遍。它们构成了许多操作系统(Microsoft Windows)和应用程序(Mozilla浏览器)的核心标识符基础结构,在许多情况下,它们以许多非标准方式暴露于Web。

This specification attempts to standardize that practice as openly as possible and in a way that attempts to benefit the entire Internet.

本规范试图以一种使整个互联网受益的方式,尽可能公开地标准化这种做法。

6. Security Considerations
6. 安全考虑

Do not assume that UUIDs are hard to guess; they should not be used as security capabilities (identifiers whose mere possession grants access), for example. A predictable random number source will exacerbate the situation.

不要认为UUID很难猜测;例如,它们不应被用作安全功能(仅拥有就可以访问的标识符)。一个可预测的随机数源会加剧这种情况。

Do not assume that it is easy to determine if a UUID has been slightly transposed in order to redirect a reference to another object. Humans do not have the ability to easily check the integrity of a UUID by simply glancing at it.

不要假设很容易确定UUID是否被轻微转置,以便将引用重定向到另一个对象。人类无法通过简单地瞥一眼来轻松检查UUID的完整性。

Distributed applications generating UUIDs at a variety of hosts must be willing to rely on the random number source at all hosts. If this is not feasible, the namespace variant should be used.

在各种主机上生成UUID的分布式应用程序必须愿意依赖所有主机上的随机数源。如果这不可行,则应使用名称空间变量。

7. Acknowledgments
7. 致谢

This document draws heavily on the OSF DCE specification for UUIDs. Ted Ts'o provided helpful comments, especially on the byte ordering section which we mostly plagiarized from a proposed wording he supplied (all errors in that section are our responsibility, however).

本文档大量借鉴了UUID的OSF DCE规范。Ted Ts'o提供了有用的评论,特别是在字节排序部分,我们大部分是从他提供的建议措辞中剽窃的(但是,该部分中的所有错误都是我们的责任)。

We are also grateful to the careful reading and bit-twiddling of Ralf S. Engelschall, John Larmouth, and Paul Thorpe. Professor Larmouth was also invaluable in achieving coordination with ISO/IEC.

我们还感谢拉尔夫·S·恩格斯查尔、约翰·拉茅斯和保罗·索普的仔细阅读和点滴玩弄。拉茅斯教授在实现与ISO/IEC的协调方面也发挥了不可估量的作用。

8. Normative References
8. 规范性引用文件

[1] Zahn, L., Dineen, T., and P. Leach, "Network Computing Architecture", ISBN 0-13-611674-4, January 1990.

[1] Zahn,L.,Dineen,T.,和P.Leach,“网络计算架构”,ISBN 0-13-611674-41990年1月。

[2] "DCE: Remote Procedure Call", Open Group CAE Specification C309, ISBN 1-85912-041-5, August 1994.

[2] “DCE:远程过程调用”,开放组CAE规范C309,ISBN 1-85912-041-51994年8月。

[3] ISO/IEC 9834-8:2004 Information Technology, "Procedures for the operation of OSI Registration Authorities: Generation and registration of Universally Unique Identifiers (UUIDs) and their use as ASN.1 Object Identifier components" ITU-T Rec. X.667, 2004.

[3] ISO/IEC 9834-8:2004信息技术,“OSI注册机构的操作程序:通用唯一标识符(UUID)的生成和注册及其作为ASN.1对象标识符组件的使用”,ITU-T Rec.X.667,2004。

[4] Rivest, R., "The MD5 Message-Digest Algorithm ", RFC 1321, April 1992.

[4] Rivest,R.,“MD5消息摘要算法”,RFC1321,1992年4月。

[5] Eastlake, D., 3rd, Schiller, J., and S. Crocker, "Randomness Requirements for Security", BCP 106, RFC 4086, June 2005.

[5] Eastlake,D.,3rd,Schiller,J.和S.Crocker,“安全的随机性要求”,BCP 106,RFC 4086,2005年6月。

[6] Moats, R., "URN Syntax", RFC 2141, May 1997.

[6] 护城河,R.,“瓮语法”,RFC 21411997年5月。

[7] Crocker, D. and P. Overell, "Augmented BNF for Syntax Specifications: ABNF", RFC 2234, November 1997.

[7] Crocker,D.和P.Overell,“语法规范的扩充BNF:ABNF”,RFC 2234,1997年11月。

[8] National Institute of Standards and Technology, "Secure Hash Standard", FIPS PUB 180-1, April 1995, <http://www.itl.nist.gov/fipspubs/fip180-1.htm>.

[8] 国家标准与技术研究所,“安全哈希标准”,FIPS PUB 180-11995年4月<http://www.itl.nist.gov/fipspubs/fip180-1.htm>.

Appendix A. Appendix A - Sample Implementation
附录A.附录A-实施示例

This implementation consists of 5 files: uuid.h, uuid.c, sysdep.h, sysdep.c and utest.c. The uuid.* files are the system independent implementation of the UUID generation algorithms described above, with all the optimizations described above except efficient state sharing across processes included. The code has been tested on Linux (Red Hat 4.0) with GCC (2.7.2), and Windows NT 4.0 with VC++ 5.0. The code assumes 64-bit integer support, which makes it much clearer.

这个实现由5个文件组成:uuid.h、uuid.c、sysdep.h、sysdep.c和utest.c。uuid.*文件是上述uuid生成算法的独立于系统的实现,包括上述所有优化,但进程间的有效状态共享除外。该代码已经在Linux(Red Hat 4.0)和GCC(2.7.2)上进行了测试,并在Windows NT 4.0和VC++5.0上进行了测试。该代码假定支持64位整数,这使它更加清晰。

All the following source files should have the following copyright notice included:

以下所有源文件应包含以下版权声明:

copyrt.h

copyrt.h

/*
** Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
** Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
** Digital Equipment Corporation, Maynard, Mass.
** Copyright (c) 1998 Microsoft.
** To anyone who acknowledges that this file is provided "AS IS"
** without any express or implied warranty: permission to use, copy,
** modify, and distribute this file for any purpose is hereby
** granted without fee, provided that the above copyright notices and
** this notice appears in all source code copies, and that none of
** the names of Open Software Foundation, Inc., Hewlett-Packard
** Company, Microsoft, or Digital Equipment Corporation be used in
** advertising or publicity pertaining to distribution of the software
** without specific, written prior permission. Neither Open Software
** Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital
** Equipment Corporation makes any representations about the
** suitability of this software for any purpose.
*/
        
/*
** Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
** Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
** Digital Equipment Corporation, Maynard, Mass.
** Copyright (c) 1998 Microsoft.
** To anyone who acknowledges that this file is provided "AS IS"
** without any express or implied warranty: permission to use, copy,
** modify, and distribute this file for any purpose is hereby
** granted without fee, provided that the above copyright notices and
** this notice appears in all source code copies, and that none of
** the names of Open Software Foundation, Inc., Hewlett-Packard
** Company, Microsoft, or Digital Equipment Corporation be used in
** advertising or publicity pertaining to distribution of the software
** without specific, written prior permission. Neither Open Software
** Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital
** Equipment Corporation makes any representations about the
** suitability of this software for any purpose.
*/
        

uuid.h

uuid.h

#include "copyrt.h"
#undef uuid_t
typedef struct {
    unsigned32  time_low;
    unsigned16  time_mid;
    unsigned16  time_hi_and_version;
    unsigned8   clock_seq_hi_and_reserved;
    unsigned8   clock_seq_low;
    byte        node[6];
} uuid_t;
        
#include "copyrt.h"
#undef uuid_t
typedef struct {
    unsigned32  time_low;
    unsigned16  time_mid;
    unsigned16  time_hi_and_version;
    unsigned8   clock_seq_hi_and_reserved;
    unsigned8   clock_seq_low;
    byte        node[6];
} uuid_t;
        
/* uuid_create -- generate a UUID */
int uuid_create(uuid_t * uuid);
        
/* uuid_create -- generate a UUID */
int uuid_create(uuid_t * uuid);
        
/* uuid_create_md5_from_name -- create a version 3 (MD5) UUID using a
   "name" from a "name space" */
void uuid_create_md5_from_name(
    uuid_t *uuid,         /* resulting UUID */
    uuid_t nsid,          /* UUID of the namespace */
    void *name,           /* the name from which to generate a UUID */
    int namelen           /* the length of the name */
);
        
/* uuid_create_md5_from_name -- create a version 3 (MD5) UUID using a
   "name" from a "name space" */
void uuid_create_md5_from_name(
    uuid_t *uuid,         /* resulting UUID */
    uuid_t nsid,          /* UUID of the namespace */
    void *name,           /* the name from which to generate a UUID */
    int namelen           /* the length of the name */
);
        
/* uuid_create_sha1_from_name -- create a version 5 (SHA-1) UUID
   using a "name" from a "name space" */
void uuid_create_sha1_from_name(
        
/* uuid_create_sha1_from_name -- create a version 5 (SHA-1) UUID
   using a "name" from a "name space" */
void uuid_create_sha1_from_name(
        
    uuid_t *uuid,         /* resulting UUID */
    uuid_t nsid,          /* UUID of the namespace */
    void *name,           /* the name from which to generate a UUID */
    int namelen           /* the length of the name */
);
        
    uuid_t *uuid,         /* resulting UUID */
    uuid_t nsid,          /* UUID of the namespace */
    void *name,           /* the name from which to generate a UUID */
    int namelen           /* the length of the name */
);
        
/* uuid_compare --  Compare two UUID's "lexically" and return
        -1   u1 is lexically before u2
         0   u1 is equal to u2
         1   u1 is lexically after u2
   Note that lexical ordering is not temporal ordering!
*/
int uuid_compare(uuid_t *u1, uuid_t *u2);
        
/* uuid_compare --  Compare two UUID's "lexically" and return
        -1   u1 is lexically before u2
         0   u1 is equal to u2
         1   u1 is lexically after u2
   Note that lexical ordering is not temporal ordering!
*/
int uuid_compare(uuid_t *u1, uuid_t *u2);
        

uuid.c

uuid.c

#include "copyrt.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "sysdep.h"
#include "uuid.h"
        
#include "copyrt.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "sysdep.h"
#include "uuid.h"
        
/* various forward declarations */
static int read_state(unsigned16 *clockseq, uuid_time_t *timestamp,
    uuid_node_t *node);
static void write_state(unsigned16 clockseq, uuid_time_t timestamp,
    uuid_node_t node);
static void format_uuid_v1(uuid_t *uuid, unsigned16 clockseq,
    uuid_time_t timestamp, uuid_node_t node);
        
/* various forward declarations */
static int read_state(unsigned16 *clockseq, uuid_time_t *timestamp,
    uuid_node_t *node);
static void write_state(unsigned16 clockseq, uuid_time_t timestamp,
    uuid_node_t node);
static void format_uuid_v1(uuid_t *uuid, unsigned16 clockseq,
    uuid_time_t timestamp, uuid_node_t node);
        
static void format_uuid_v3or5(uuid_t *uuid, unsigned char hash[16],
    int v);
static void get_current_time(uuid_time_t *timestamp);
static unsigned16 true_random(void);
        
static void format_uuid_v3or5(uuid_t *uuid, unsigned char hash[16],
    int v);
static void get_current_time(uuid_time_t *timestamp);
static unsigned16 true_random(void);
        
/* uuid_create -- generator a UUID */
int uuid_create(uuid_t *uuid)
{
     uuid_time_t timestamp, last_time;
     unsigned16 clockseq;
     uuid_node_t node;
     uuid_node_t last_node;
     int f;
        
/* uuid_create -- generator a UUID */
int uuid_create(uuid_t *uuid)
{
     uuid_time_t timestamp, last_time;
     unsigned16 clockseq;
     uuid_node_t node;
     uuid_node_t last_node;
     int f;
        
     /* acquire system-wide lock so we're alone */
     LOCK;
     /* get time, node ID, saved state from non-volatile storage */
     get_current_time(&timestamp);
     get_ieee_node_identifier(&node);
     f = read_state(&clockseq, &last_time, &last_node);
        
     /* acquire system-wide lock so we're alone */
     LOCK;
     /* get time, node ID, saved state from non-volatile storage */
     get_current_time(&timestamp);
     get_ieee_node_identifier(&node);
     f = read_state(&clockseq, &last_time, &last_node);
        
     /* if no NV state, or if clock went backwards, or node ID
        changed (e.g., new network card) change clockseq */
     if (!f || memcmp(&node, &last_node, sizeof node))
         clockseq = true_random();
     else if (timestamp < last_time)
         clockseq++;
        
     /* if no NV state, or if clock went backwards, or node ID
        changed (e.g., new network card) change clockseq */
     if (!f || memcmp(&node, &last_node, sizeof node))
         clockseq = true_random();
     else if (timestamp < last_time)
         clockseq++;
        
     /* save the state for next time */
     write_state(clockseq, timestamp, node);
        
     /* save the state for next time */
     write_state(clockseq, timestamp, node);
        

UNLOCK;

解锁;

     /* stuff fields into the UUID */
     format_uuid_v1(uuid, clockseq, timestamp, node);
     return 1;
}
        
     /* stuff fields into the UUID */
     format_uuid_v1(uuid, clockseq, timestamp, node);
     return 1;
}
        
/* format_uuid_v1 -- make a UUID from the timestamp, clockseq,
                     and node ID */
void format_uuid_v1(uuid_t* uuid, unsigned16 clock_seq,
                    uuid_time_t timestamp, uuid_node_t node)
{
    /* Construct a version 1 uuid with the information we've gathered
       plus a few constants. */
    uuid->time_low = (unsigned long)(timestamp & 0xFFFFFFFF);
    uuid->time_mid = (unsigned short)((timestamp >> 32) & 0xFFFF);
    uuid->time_hi_and_version =
        
/* format_uuid_v1 -- make a UUID from the timestamp, clockseq,
                     and node ID */
void format_uuid_v1(uuid_t* uuid, unsigned16 clock_seq,
                    uuid_time_t timestamp, uuid_node_t node)
{
    /* Construct a version 1 uuid with the information we've gathered
       plus a few constants. */
    uuid->time_low = (unsigned long)(timestamp & 0xFFFFFFFF);
    uuid->time_mid = (unsigned short)((timestamp >> 32) & 0xFFFF);
    uuid->time_hi_and_version =
        
        (unsigned short)((timestamp >> 48) & 0x0FFF);
    uuid->time_hi_and_version |= (1 << 12);
    uuid->clock_seq_low = clock_seq & 0xFF;
    uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3F00) >> 8;
    uuid->clock_seq_hi_and_reserved |= 0x80;
    memcpy(&uuid->node, &node, sizeof uuid->node);
}
        
        (unsigned short)((timestamp >> 48) & 0x0FFF);
    uuid->time_hi_and_version |= (1 << 12);
    uuid->clock_seq_low = clock_seq & 0xFF;
    uuid->clock_seq_hi_and_reserved = (clock_seq & 0x3F00) >> 8;
    uuid->clock_seq_hi_and_reserved |= 0x80;
    memcpy(&uuid->node, &node, sizeof uuid->node);
}
        
/* data type for UUID generator persistent state */
typedef struct {
    uuid_time_t  ts;       /* saved timestamp */
    uuid_node_t  node;     /* saved node ID */
    unsigned16   cs;       /* saved clock sequence */
} uuid_state;
        
/* data type for UUID generator persistent state */
typedef struct {
    uuid_time_t  ts;       /* saved timestamp */
    uuid_node_t  node;     /* saved node ID */
    unsigned16   cs;       /* saved clock sequence */
} uuid_state;
        

static uuid_state st;

静态uuid_状态st;

/* read_state -- read UUID generator state from non-volatile store */
int read_state(unsigned16 *clockseq, uuid_time_t *timestamp,
               uuid_node_t *node)
{
    static int inited = 0;
    FILE *fp;
        
/* read_state -- read UUID generator state from non-volatile store */
int read_state(unsigned16 *clockseq, uuid_time_t *timestamp,
               uuid_node_t *node)
{
    static int inited = 0;
    FILE *fp;
        
    /* only need to read state once per boot */
    if (!inited) {
        fp = fopen("state", "rb");
        if (fp == NULL)
            return 0;
        fread(&st, sizeof st, 1, fp);
        fclose(fp);
        inited = 1;
    }
    *clockseq = st.cs;
    *timestamp = st.ts;
    *node = st.node;
    return 1;
}
        
    /* only need to read state once per boot */
    if (!inited) {
        fp = fopen("state", "rb");
        if (fp == NULL)
            return 0;
        fread(&st, sizeof st, 1, fp);
        fclose(fp);
        inited = 1;
    }
    *clockseq = st.cs;
    *timestamp = st.ts;
    *node = st.node;
    return 1;
}
        
/* write_state -- save UUID generator state back to non-volatile
   storage */
void write_state(unsigned16 clockseq, uuid_time_t timestamp,
                 uuid_node_t node)
{
    static int inited = 0;
    static uuid_time_t next_save;
    FILE* fp;
        
/* write_state -- save UUID generator state back to non-volatile
   storage */
void write_state(unsigned16 clockseq, uuid_time_t timestamp,
                 uuid_node_t node)
{
    static int inited = 0;
    static uuid_time_t next_save;
    FILE* fp;
        
    if (!inited) {
        next_save = timestamp;
        inited = 1;
    }
        
    if (!inited) {
        next_save = timestamp;
        inited = 1;
    }
        
    /* always save state to volatile shared state */
    st.cs = clockseq;
    st.ts = timestamp;
    st.node = node;
    if (timestamp >= next_save) {
        fp = fopen("state", "wb");
        fwrite(&st, sizeof st, 1, fp);
        fclose(fp);
        /* schedule next save for 10 seconds from now */
        next_save = timestamp + (10 * 10 * 1000 * 1000);
    }
}
        
    /* always save state to volatile shared state */
    st.cs = clockseq;
    st.ts = timestamp;
    st.node = node;
    if (timestamp >= next_save) {
        fp = fopen("state", "wb");
        fwrite(&st, sizeof st, 1, fp);
        fclose(fp);
        /* schedule next save for 10 seconds from now */
        next_save = timestamp + (10 * 10 * 1000 * 1000);
    }
}
        
/* get-current_time -- get time as 60-bit 100ns ticks since UUID epoch.
   Compensate for the fact that real clock resolution is
   less than 100ns. */
void get_current_time(uuid_time_t *timestamp)
{
    static int inited = 0;
    static uuid_time_t time_last;
    static unsigned16 uuids_this_tick;
    uuid_time_t time_now;
        
/* get-current_time -- get time as 60-bit 100ns ticks since UUID epoch.
   Compensate for the fact that real clock resolution is
   less than 100ns. */
void get_current_time(uuid_time_t *timestamp)
{
    static int inited = 0;
    static uuid_time_t time_last;
    static unsigned16 uuids_this_tick;
    uuid_time_t time_now;
        
    if (!inited) {
        get_system_time(&time_now);
        uuids_this_tick = UUIDS_PER_TICK;
        inited = 1;
    }
        
    if (!inited) {
        get_system_time(&time_now);
        uuids_this_tick = UUIDS_PER_TICK;
        inited = 1;
    }
        
    for ( ; ; ) {
        get_system_time(&time_now);
        
    for ( ; ; ) {
        get_system_time(&time_now);
        
        /* if clock reading changed since last UUID generated, */
        if (time_last != time_now) {
            /* reset count of uuids gen'd with this clock reading */
            uuids_this_tick = 0;
            time_last = time_now;
            break;
        }
        if (uuids_this_tick < UUIDS_PER_TICK) {
            uuids_this_tick++;
            break;
        }
        
        /* if clock reading changed since last UUID generated, */
        if (time_last != time_now) {
            /* reset count of uuids gen'd with this clock reading */
            uuids_this_tick = 0;
            time_last = time_now;
            break;
        }
        if (uuids_this_tick < UUIDS_PER_TICK) {
            uuids_this_tick++;
            break;
        }
        
        /* going too fast for our clock; spin */
    }
    /* add the count of uuids to low order bits of the clock reading */
    *timestamp = time_now + uuids_this_tick;
}
        
        /* going too fast for our clock; spin */
    }
    /* add the count of uuids to low order bits of the clock reading */
    *timestamp = time_now + uuids_this_tick;
}
        
/* true_random -- generate a crypto-quality random number.
   **This sample doesn't do that.** */
static unsigned16 true_random(void)
{
    static int inited = 0;
    uuid_time_t time_now;
        
/* true_random -- generate a crypto-quality random number.
   **This sample doesn't do that.** */
static unsigned16 true_random(void)
{
    static int inited = 0;
    uuid_time_t time_now;
        
    if (!inited) {
        get_system_time(&time_now);
        time_now = time_now / UUIDS_PER_TICK;
        srand((unsigned int)
               (((time_now >> 32) ^ time_now) & 0xffffffff));
        inited = 1;
    }
        
    if (!inited) {
        get_system_time(&time_now);
        time_now = time_now / UUIDS_PER_TICK;
        srand((unsigned int)
               (((time_now >> 32) ^ time_now) & 0xffffffff));
        inited = 1;
    }
        
    return rand();
}
        
    return rand();
}
        
/* uuid_create_md5_from_name -- create a version 3 (MD5) UUID using a
   "name" from a "name space" */
void uuid_create_md5_from_name(uuid_t *uuid, uuid_t nsid, void *name,
                               int namelen)
{
    MD5_CTX c;
    unsigned char hash[16];
    uuid_t net_nsid;
        
/* uuid_create_md5_from_name -- create a version 3 (MD5) UUID using a
   "name" from a "name space" */
void uuid_create_md5_from_name(uuid_t *uuid, uuid_t nsid, void *name,
                               int namelen)
{
    MD5_CTX c;
    unsigned char hash[16];
    uuid_t net_nsid;
        
    /* put name space ID in network byte order so it hashes the same
       no matter what endian machine we're on */
    net_nsid = nsid;
    net_nsid.time_low = htonl(net_nsid.time_low);
    net_nsid.time_mid = htons(net_nsid.time_mid);
    net_nsid.time_hi_and_version = htons(net_nsid.time_hi_and_version);
        
    /* put name space ID in network byte order so it hashes the same
       no matter what endian machine we're on */
    net_nsid = nsid;
    net_nsid.time_low = htonl(net_nsid.time_low);
    net_nsid.time_mid = htons(net_nsid.time_mid);
    net_nsid.time_hi_and_version = htons(net_nsid.time_hi_and_version);
        
    MD5Init(&c);
    MD5Update(&c, &net_nsid, sizeof net_nsid);
    MD5Update(&c, name, namelen);
    MD5Final(hash, &c);
        
    MD5Init(&c);
    MD5Update(&c, &net_nsid, sizeof net_nsid);
    MD5Update(&c, name, namelen);
    MD5Final(hash, &c);
        
    /* the hash is in network byte order at this point */
    format_uuid_v3or5(uuid, hash, 3);
}
        
    /* the hash is in network byte order at this point */
    format_uuid_v3or5(uuid, hash, 3);
}
        
void uuid_create_sha1_from_name(uuid_t *uuid, uuid_t nsid, void *name,
                                int namelen)
{
    SHA_CTX c;
    unsigned char hash[20];
    uuid_t net_nsid;
        
void uuid_create_sha1_from_name(uuid_t *uuid, uuid_t nsid, void *name,
                                int namelen)
{
    SHA_CTX c;
    unsigned char hash[20];
    uuid_t net_nsid;
        
    /* put name space ID in network byte order so it hashes the same
       no matter what endian machine we're on */
    net_nsid = nsid;
    net_nsid.time_low = htonl(net_nsid.time_low);
    net_nsid.time_mid = htons(net_nsid.time_mid);
    net_nsid.time_hi_and_version = htons(net_nsid.time_hi_and_version);
        
    /* put name space ID in network byte order so it hashes the same
       no matter what endian machine we're on */
    net_nsid = nsid;
    net_nsid.time_low = htonl(net_nsid.time_low);
    net_nsid.time_mid = htons(net_nsid.time_mid);
    net_nsid.time_hi_and_version = htons(net_nsid.time_hi_and_version);
        
    SHA1_Init(&c);
    SHA1_Update(&c, &net_nsid, sizeof net_nsid);
    SHA1_Update(&c, name, namelen);
    SHA1_Final(hash, &c);
        
    SHA1_Init(&c);
    SHA1_Update(&c, &net_nsid, sizeof net_nsid);
    SHA1_Update(&c, name, namelen);
    SHA1_Final(hash, &c);
        
    /* the hash is in network byte order at this point */
    format_uuid_v3or5(uuid, hash, 5);
}
        
    /* the hash is in network byte order at this point */
    format_uuid_v3or5(uuid, hash, 5);
}
        
/* format_uuid_v3or5 -- make a UUID from a (pseudo)random 128-bit
   number */
void format_uuid_v3or5(uuid_t *uuid, unsigned char hash[16], int v)
{
    /* convert UUID to local byte order */
    memcpy(uuid, hash, sizeof *uuid);
    uuid->time_low = ntohl(uuid->time_low);
    uuid->time_mid = ntohs(uuid->time_mid);
    uuid->time_hi_and_version = ntohs(uuid->time_hi_and_version);
        
/* format_uuid_v3or5 -- make a UUID from a (pseudo)random 128-bit
   number */
void format_uuid_v3or5(uuid_t *uuid, unsigned char hash[16], int v)
{
    /* convert UUID to local byte order */
    memcpy(uuid, hash, sizeof *uuid);
    uuid->time_low = ntohl(uuid->time_low);
    uuid->time_mid = ntohs(uuid->time_mid);
    uuid->time_hi_and_version = ntohs(uuid->time_hi_and_version);
        
    /* put in the variant and version bits */
    uuid->time_hi_and_version &= 0x0FFF;
    uuid->time_hi_and_version |= (v << 12);
    uuid->clock_seq_hi_and_reserved &= 0x3F;
    uuid->clock_seq_hi_and_reserved |= 0x80;
}
        
    /* put in the variant and version bits */
    uuid->time_hi_and_version &= 0x0FFF;
    uuid->time_hi_and_version |= (v << 12);
    uuid->clock_seq_hi_and_reserved &= 0x3F;
    uuid->clock_seq_hi_and_reserved |= 0x80;
}
        
/* uuid_compare --  Compare two UUID's "lexically" and return */
#define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1;
int uuid_compare(uuid_t *u1, uuid_t *u2)
{
    int i;
        
/* uuid_compare --  Compare two UUID's "lexically" and return */
#define CHECK(f1, f2) if (f1 != f2) return f1 < f2 ? -1 : 1;
int uuid_compare(uuid_t *u1, uuid_t *u2)
{
    int i;
        
    CHECK(u1->time_low, u2->time_low);
    CHECK(u1->time_mid, u2->time_mid);
        
    CHECK(u1->time_low, u2->time_low);
    CHECK(u1->time_mid, u2->time_mid);
        
    CHECK(u1->time_hi_and_version, u2->time_hi_and_version);
    CHECK(u1->clock_seq_hi_and_reserved, u2->clock_seq_hi_and_reserved);
    CHECK(u1->clock_seq_low, u2->clock_seq_low)
    for (i = 0; i < 6; i++) {
        if (u1->node[i] < u2->node[i])
            return -1;
        if (u1->node[i] > u2->node[i])
            return 1;
    }
    return 0;
}
#undef CHECK
        
    CHECK(u1->time_hi_and_version, u2->time_hi_and_version);
    CHECK(u1->clock_seq_hi_and_reserved, u2->clock_seq_hi_and_reserved);
    CHECK(u1->clock_seq_low, u2->clock_seq_low)
    for (i = 0; i < 6; i++) {
        if (u1->node[i] < u2->node[i])
            return -1;
        if (u1->node[i] > u2->node[i])
            return 1;
    }
    return 0;
}
#undef CHECK
        

sysdep.h

系统Dep.h

#include "copyrt.h"
/* remove the following define if you aren't running WIN32 */
#define WININC 0
        
#include "copyrt.h"
/* remove the following define if you aren't running WIN32 */
#define WININC 0
        
#ifdef WININC
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/time.h>
#include <sys/sysinfo.h>
#endif
        
#ifdef WININC
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/time.h>
#include <sys/sysinfo.h>
#endif
        
#include "global.h"
/* change to point to where MD5 .h's live; RFC 1321 has sample
   implementation */
#include "md5.h"
        
#include "global.h"
/* change to point to where MD5 .h's live; RFC 1321 has sample
   implementation */
#include "md5.h"
        
/* set the following to the number of 100ns ticks of the actual
   resolution of your system's clock */
#define UUIDS_PER_TICK 1024
        
/* set the following to the number of 100ns ticks of the actual
   resolution of your system's clock */
#define UUIDS_PER_TICK 1024
        
/* Set the following to a calls to get and release a global lock */
#define LOCK
#define UNLOCK
        
/* Set the following to a calls to get and release a global lock */
#define LOCK
#define UNLOCK
        
typedef unsigned long   unsigned32;
typedef unsigned short  unsigned16;
typedef unsigned char   unsigned8;
typedef unsigned char   byte;
        
typedef unsigned long   unsigned32;
typedef unsigned short  unsigned16;
typedef unsigned char   unsigned8;
typedef unsigned char   byte;
        
/* Set this to what your compiler uses for 64-bit data type */
#ifdef WININC
        
/* Set this to what your compiler uses for 64-bit data type */
#ifdef WININC
        
#define unsigned64_t unsigned __int64
#define I64(C) C
#else
#define unsigned64_t unsigned long long
#define I64(C) C##LL
#endif
        
#define unsigned64_t unsigned __int64
#define I64(C) C
#else
#define unsigned64_t unsigned long long
#define I64(C) C##LL
#endif
        
typedef unsigned64_t uuid_time_t;
typedef struct {
    char nodeID[6];
} uuid_node_t;
        
typedef unsigned64_t uuid_time_t;
typedef struct {
    char nodeID[6];
} uuid_node_t;
        
void get_ieee_node_identifier(uuid_node_t *node);
void get_system_time(uuid_time_t *uuid_time);
void get_random_info(char seed[16]);
        
void get_ieee_node_identifier(uuid_node_t *node);
void get_system_time(uuid_time_t *uuid_time);
void get_random_info(char seed[16]);
        

sysdep.c

sysdep.c

#include "copyrt.h"
#include <stdio.h>
#include "sysdep.h"
        
#include "copyrt.h"
#include <stdio.h>
#include "sysdep.h"
        
/* system dependent call to get IEEE node ID.
   This sample implementation generates a random node ID. */
void get_ieee_node_identifier(uuid_node_t *node)
{
    static inited = 0;
    static uuid_node_t saved_node;
    char seed[16];
    FILE *fp;
        
/* system dependent call to get IEEE node ID.
   This sample implementation generates a random node ID. */
void get_ieee_node_identifier(uuid_node_t *node)
{
    static inited = 0;
    static uuid_node_t saved_node;
    char seed[16];
    FILE *fp;
        
    if (!inited) {
        fp = fopen("nodeid", "rb");
        if (fp) {
            fread(&saved_node, sizeof saved_node, 1, fp);
            fclose(fp);
        }
        else {
            get_random_info(seed);
            seed[0] |= 0x01;
            memcpy(&saved_node, seed, sizeof saved_node);
            fp = fopen("nodeid", "wb");
            if (fp) {
                fwrite(&saved_node, sizeof saved_node, 1, fp);
                fclose(fp);
            }
        }
        
    if (!inited) {
        fp = fopen("nodeid", "rb");
        if (fp) {
            fread(&saved_node, sizeof saved_node, 1, fp);
            fclose(fp);
        }
        else {
            get_random_info(seed);
            seed[0] |= 0x01;
            memcpy(&saved_node, seed, sizeof saved_node);
            fp = fopen("nodeid", "wb");
            if (fp) {
                fwrite(&saved_node, sizeof saved_node, 1, fp);
                fclose(fp);
            }
        }
        
        inited = 1;
    }
        
        inited = 1;
    }
        
    *node = saved_node;
}
        
    *node = saved_node;
}
        
/* system dependent call to get the current system time. Returned as
   100ns ticks since UUID epoch, but resolution may be less than
   100ns. */
#ifdef _WINDOWS_
        
/* system dependent call to get the current system time. Returned as
   100ns ticks since UUID epoch, but resolution may be less than
   100ns. */
#ifdef _WINDOWS_
        
void get_system_time(uuid_time_t *uuid_time)
{
    ULARGE_INTEGER time;
        
void get_system_time(uuid_time_t *uuid_time)
{
    ULARGE_INTEGER time;
        
    /* NT keeps time in FILETIME format which is 100ns ticks since
       Jan 1, 1601. UUIDs use time in 100ns ticks since Oct 15, 1582.
       The difference is 17 Days in Oct + 30 (Nov) + 31 (Dec)
       + 18 years and 5 leap days. */
    GetSystemTimeAsFileTime((FILETIME *)&time);
    time.QuadPart +=
        
    /* NT keeps time in FILETIME format which is 100ns ticks since
       Jan 1, 1601. UUIDs use time in 100ns ticks since Oct 15, 1582.
       The difference is 17 Days in Oct + 30 (Nov) + 31 (Dec)
       + 18 years and 5 leap days. */
    GetSystemTimeAsFileTime((FILETIME *)&time);
    time.QuadPart +=
        
          (unsigned __int64) (1000*1000*10)       // seconds
        * (unsigned __int64) (60 * 60 * 24)       // days
        * (unsigned __int64) (17+30+31+365*18+5); // # of days
    *uuid_time = time.QuadPart;
}
        
          (unsigned __int64) (1000*1000*10)       // seconds
        * (unsigned __int64) (60 * 60 * 24)       // days
        * (unsigned __int64) (17+30+31+365*18+5); // # of days
    *uuid_time = time.QuadPart;
}
        
/* Sample code, not for use in production; see RFC 1750 */
void get_random_info(char seed[16])
{
    MD5_CTX c;
    struct {
        MEMORYSTATUS m;
        SYSTEM_INFO s;
        FILETIME t;
        LARGE_INTEGER pc;
        DWORD tc;
        DWORD l;
        char hostname[MAX_COMPUTERNAME_LENGTH + 1];
    } r;
        
/* Sample code, not for use in production; see RFC 1750 */
void get_random_info(char seed[16])
{
    MD5_CTX c;
    struct {
        MEMORYSTATUS m;
        SYSTEM_INFO s;
        FILETIME t;
        LARGE_INTEGER pc;
        DWORD tc;
        DWORD l;
        char hostname[MAX_COMPUTERNAME_LENGTH + 1];
    } r;
        
    MD5Init(&c);
    GlobalMemoryStatus(&r.m);
    GetSystemInfo(&r.s);
    GetSystemTimeAsFileTime(&r.t);
    QueryPerformanceCounter(&r.pc);
    r.tc = GetTickCount();
        
    MD5Init(&c);
    GlobalMemoryStatus(&r.m);
    GetSystemInfo(&r.s);
    GetSystemTimeAsFileTime(&r.t);
    QueryPerformanceCounter(&r.pc);
    r.tc = GetTickCount();
        
    r.l = MAX_COMPUTERNAME_LENGTH + 1;
    GetComputerName(r.hostname, &r.l);
    MD5Update(&c, &r, sizeof r);
    MD5Final(seed, &c);
}
        
    r.l = MAX_COMPUTERNAME_LENGTH + 1;
    GetComputerName(r.hostname, &r.l);
    MD5Update(&c, &r, sizeof r);
    MD5Final(seed, &c);
}
        

#else

#否则

void get_system_time(uuid_time_t *uuid_time)
{
    struct timeval tp;
        
void get_system_time(uuid_time_t *uuid_time)
{
    struct timeval tp;
        
    gettimeofday(&tp, (struct timezone *)0);
        
    gettimeofday(&tp, (struct timezone *)0);
        
    /* Offset between UUID formatted times and Unix formatted times.
       UUID UTC base time is October 15, 1582.
       Unix base time is January 1, 1970.*/
    *uuid_time = ((unsigned64)tp.tv_sec * 10000000)
        + ((unsigned64)tp.tv_usec * 10)
        + I64(0x01B21DD213814000);
}
        
    /* Offset between UUID formatted times and Unix formatted times.
       UUID UTC base time is October 15, 1582.
       Unix base time is January 1, 1970.*/
    *uuid_time = ((unsigned64)tp.tv_sec * 10000000)
        + ((unsigned64)tp.tv_usec * 10)
        + I64(0x01B21DD213814000);
}
        
/* Sample code, not for use in production; see RFC 1750 */
void get_random_info(char seed[16])
{
    MD5_CTX c;
    struct {
        struct sysinfo s;
        struct timeval t;
        char hostname[257];
    } r;
        
/* Sample code, not for use in production; see RFC 1750 */
void get_random_info(char seed[16])
{
    MD5_CTX c;
    struct {
        struct sysinfo s;
        struct timeval t;
        char hostname[257];
    } r;
        
    MD5Init(&c);
    sysinfo(&r.s);
    gettimeofday(&r.t, (struct timezone *)0);
    gethostname(r.hostname, 256);
    MD5Update(&c, &r, sizeof r);
    MD5Final(seed, &c);
}
        
    MD5Init(&c);
    sysinfo(&r.s);
    gettimeofday(&r.t, (struct timezone *)0);
    gethostname(r.hostname, 256);
    MD5Update(&c, &r, sizeof r);
    MD5Final(seed, &c);
}
        

#endif

#恩迪夫

utest.c

utest.c

#include "copyrt.h"
#include "sysdep.h"
#include <stdio.h>
#include "uuid.h"
        
#include "copyrt.h"
#include "sysdep.h"
#include <stdio.h>
#include "uuid.h"
        
uuid_t NameSpace_DNS = { /* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 */
    0x6ba7b810,
    0x9dad,
    0x11d1,
    0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
};
        
uuid_t NameSpace_DNS = { /* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 */
    0x6ba7b810,
    0x9dad,
    0x11d1,
    0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
};
        
/* puid -- print a UUID */
void puid(uuid_t u)
{
    int i;
        
/* puid -- print a UUID */
void puid(uuid_t u)
{
    int i;
        
    printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid,
    u.time_hi_and_version, u.clock_seq_hi_and_reserved,
    u.clock_seq_low);
    for (i = 0; i < 6; i++)
        printf("%2.2x", u.node[i]);
    printf("\n");
}
        
    printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid,
    u.time_hi_and_version, u.clock_seq_hi_and_reserved,
    u.clock_seq_low);
    for (i = 0; i < 6; i++)
        printf("%2.2x", u.node[i]);
    printf("\n");
}
        
/* Simple driver for UUID generator */
void main(int argc, char **argv)
{
    uuid_t u;
    int f;
        
/* Simple driver for UUID generator */
void main(int argc, char **argv)
{
    uuid_t u;
    int f;
        
    uuid_create(&u);
    printf("uuid_create(): "); puid(u);
        
    uuid_create(&u);
    printf("uuid_create(): "); puid(u);
        
    f = uuid_compare(&u, &u);
    printf("uuid_compare(u,u): %d\n", f);     /* should be 0 */
    f = uuid_compare(&u, &NameSpace_DNS);
    printf("uuid_compare(u, NameSpace_DNS): %d\n", f); /* s.b. 1 */
    f = uuid_compare(&NameSpace_DNS, &u);
    printf("uuid_compare(NameSpace_DNS, u): %d\n", f); /* s.b. -1 */
    uuid_create_md5_from_name(&u, NameSpace_DNS, "www.widgets.com", 15);
    printf("uuid_create_md5_from_name(): "); puid(u);
}
        
    f = uuid_compare(&u, &u);
    printf("uuid_compare(u,u): %d\n", f);     /* should be 0 */
    f = uuid_compare(&u, &NameSpace_DNS);
    printf("uuid_compare(u, NameSpace_DNS): %d\n", f); /* s.b. 1 */
    f = uuid_compare(&NameSpace_DNS, &u);
    printf("uuid_compare(NameSpace_DNS, u): %d\n", f); /* s.b. -1 */
    uuid_create_md5_from_name(&u, NameSpace_DNS, "www.widgets.com", 15);
    printf("uuid_create_md5_from_name(): "); puid(u);
}
        
Appendix B. Appendix B - Sample Output of utest
附录B.附录B-utest的样本输出
     uuid_create(): 7d444840-9dc0-11d1-b245-5ffdce74fad2
     uuid_compare(u,u): 0
     uuid_compare(u, NameSpace_DNS): 1
     uuid_compare(NameSpace_DNS, u): -1
     uuid_create_md5_from_name(): e902893a-9d22-3c7e-a7b8-d6e313b71d9f
        
     uuid_create(): 7d444840-9dc0-11d1-b245-5ffdce74fad2
     uuid_compare(u,u): 0
     uuid_compare(u, NameSpace_DNS): 1
     uuid_compare(NameSpace_DNS, u): -1
     uuid_create_md5_from_name(): e902893a-9d22-3c7e-a7b8-d6e313b71d9f
        
Appendix C. Appendix C - Some Name Space IDs
附录C.附录C-一些名称空间ID

This appendix lists the name space IDs for some potentially interesting name spaces, as initialized C structures and in the string representation defined above.

本附录列出了一些可能感兴趣的名称空间的名称空间ID,如初始化的C结构和上面定义的字符串表示形式。

   /* Name string is a fully-qualified domain name */
   uuid_t NameSpace_DNS = { /* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 */
       0x6ba7b810,
       0x9dad,
       0x11d1,
       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
   };
        
   /* Name string is a fully-qualified domain name */
   uuid_t NameSpace_DNS = { /* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 */
       0x6ba7b810,
       0x9dad,
       0x11d1,
       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
   };
        
   /* Name string is a URL */
   uuid_t NameSpace_URL = { /* 6ba7b811-9dad-11d1-80b4-00c04fd430c8 */
       0x6ba7b811,
       0x9dad,
       0x11d1,
       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
   };
        
   /* Name string is a URL */
   uuid_t NameSpace_URL = { /* 6ba7b811-9dad-11d1-80b4-00c04fd430c8 */
       0x6ba7b811,
       0x9dad,
       0x11d1,
       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
   };
        
   /* Name string is an ISO OID */
   uuid_t NameSpace_OID = { /* 6ba7b812-9dad-11d1-80b4-00c04fd430c8 */
       0x6ba7b812,
       0x9dad,
       0x11d1,
       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
   };
        
   /* Name string is an ISO OID */
   uuid_t NameSpace_OID = { /* 6ba7b812-9dad-11d1-80b4-00c04fd430c8 */
       0x6ba7b812,
       0x9dad,
       0x11d1,
       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
   };
        
   /* Name string is an X.500 DN (in DER or a text output format) */
   uuid_t NameSpace_X500 = { /* 6ba7b814-9dad-11d1-80b4-00c04fd430c8 */
       0x6ba7b814,
       0x9dad,
       0x11d1,
       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
   };
        
   /* Name string is an X.500 DN (in DER or a text output format) */
   uuid_t NameSpace_X500 = { /* 6ba7b814-9dad-11d1-80b4-00c04fd430c8 */
       0x6ba7b814,
       0x9dad,
       0x11d1,
       0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8
   };
        

Authors' Addresses

作者地址

Paul J. Leach Microsoft 1 Microsoft Way Redmond, WA 98052 US

Paul J.Leach微软1微软路雷德蒙德,华盛顿州,美国98052

   Phone: +1 425-882-8080
   EMail: paulle@microsoft.com
        
   Phone: +1 425-882-8080
   EMail: paulle@microsoft.com
        

Michael Mealling Refactored Networks, LLC 1635 Old Hwy 41 Suite 112, Box 138 Kennesaw, GA 30152 US

迈克尔·米林重构网络有限责任公司美国佐治亚州肯尼索市41号老公路1635号112室138号信箱30152

   Phone: +1-678-581-9656
   EMail: michael@refactored-networks.com
   URI: http://www.refactored-networks.com
        
   Phone: +1-678-581-9656
   EMail: michael@refactored-networks.com
   URI: http://www.refactored-networks.com
        

Rich Salz DataPower Technology, Inc. 1 Alewife Center Cambridge, MA 02142 US

Rich Salz DataPower Technology,Inc.美国马萨诸塞州剑桥市阿莱夫中心1号,邮编:02142

   Phone: +1 617-864-0455
   EMail: rsalz@datapower.com
   URI:   http://www.datapower.com
        
   Phone: +1 617-864-0455
   EMail: rsalz@datapower.com
   URI:   http://www.datapower.com
        

Full Copyright Statement

完整版权声明

Copyright (C) The Internet Society (2005).

版权所有(C)互联网协会(2005年)。

This document is subject to the rights, licenses and restrictions contained in BCP 78, and except as set forth therein, the authors retain all their rights.

本文件受BCP 78中包含的权利、许可和限制的约束,除其中规定外,作者保留其所有权利。

This document and the information contained herein are provided on an "AS IS" basis and THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY AND THE INTERNET ENGINEERING TASK FORCE DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.

本文件及其包含的信息是按“原样”提供的,贡献者、他/她所代表或赞助的组织(如有)、互联网协会和互联网工程任务组不承担任何明示或暗示的担保,包括但不限于任何保证,即使用本文中的信息不会侵犯任何权利,或对适销性或特定用途适用性的任何默示保证。

Intellectual Property

知识产权

The IETF takes no position regarding the validity or scope of any Intellectual Property Rights or other rights that might be claimed to pertain to the implementation or use of the technology described in this document or the extent to which any license under such rights might or might not be available; nor does it represent that it has made any independent effort to identify any such rights. Information on the procedures with respect to rights in RFC documents can be found in BCP 78 and BCP 79.

IETF对可能声称与本文件所述技术的实施或使用有关的任何知识产权或其他权利的有效性或范围,或此类权利下的任何许可可能或可能不可用的程度,不采取任何立场;它也不表示它已作出任何独立努力来确定任何此类权利。有关RFC文件中权利的程序信息,请参见BCP 78和BCP 79。

Copies of IPR disclosures made to the IETF Secretariat and any assurances of licenses to be made available, or the result of an attempt made to obtain a general license or permission for the use of such proprietary rights by implementers or users of this specification can be obtained from the IETF on-line IPR repository at http://www.ietf.org/ipr.

向IETF秘书处披露的知识产权副本和任何许可证保证,或本规范实施者或用户试图获得使用此类专有权利的一般许可证或许可的结果,可从IETF在线知识产权存储库获取,网址为http://www.ietf.org/ipr.

The IETF invites any interested party to bring to its attention any copyrights, patents or patent applications, or other proprietary rights that may cover technology that may be required to implement this standard. Please address the information to the IETF at ietf-ipr@ietf.org.

IETF邀请任何相关方提请其注意任何版权、专利或专利申请,或其他可能涵盖实施本标准所需技术的专有权利。请将信息发送至IETF的IETF-ipr@ietf.org.

Acknowledgement

确认

Funding for the RFC Editor function is currently provided by the Internet Society.

RFC编辑功能的资金目前由互联网协会提供。