UUID格式:8-4-4-4-12-为什么?


84

为什么UUID以“ 8-4-4-4-12”(数字)格式显示?由于这个原因,我环顾四周,但找不到所需的决定。

格式化为十六进制字符串的UUID的示例:58D5E212-165B-4CA0-909B-C86B9CEE0111


11
实际上,该十六进制字符串示例是不正确的。该UUID规范要求,该代表UUID值十六进制字符串必须小写。规范还要求实现能够解析大写或大小写混合的字符串,但只能生成小写。不幸的是,包括Apple,Microsoft和其他公司在内的常见实现都违反了此规则。
罗勒·布尔克

1
有趣的罗勒,谢谢
Fidel

Answers:


64

time, version, clock_seq_hi, clock_seq_lo, node如下面的rfc所示,它由分隔。

IETF RFC4122:

4.1.2.  Layout and Byte Order

   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.

   Field                  Data Type     Octet  Note
                                        #

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

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

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

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

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

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

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

   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.

   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)                            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

11
为什么将时间戳分为三个部分?
user253751 2014年

4
字段的生成方式取决于UUID版本。首选方法不使用时间,因为它可以显示生成ID的时间(潜在的安全问题)。 en.wikipedia.org/wiki/...
pmont

1
@pmont“首选”?
罗勒·布尔克

2
@brocoli我必须不同意。V4依赖于加密强度高的随机数生成器,如V1 UUID所示,与简单地获取MAC地址,当前时刻和递增的任意数字相比,更好地构建它困难得多。而且,V1的实现通常是开源的,并且在很多年前就已构建,并且在整个行业中已广泛使用,而现在已经过时了。声称V1“容易出现部分故障”只是愚蠢的。V1 UUID是系统的最后一部分,您需要担心失败。
罗勒·布尔克

2
@BasilBourque随着容器数量的增加以及容器网络的出现,您现在可以看到的问题之一就是MAC地址冲突。通常,容器和VM从有限范围的可能的MAC地址中提取。默认情况下,IIRC Hyper-V仅从256个可能的MAC地址的池中提取。
内森·克莱顿

12

该格式在IETF RFC4122的第3节中定义。输出格式定义为“ UUID = ...”。

3.-命名空间注册模板

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

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

句法结构的声明:UUID是相对于所有UUID的空间在时间和空间上都是唯一的标识符。由于UUID是固定大小且包含时间字段,因此值可能会翻转(大约在AD 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.

  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.

  The formal definition of the UUID string representation is
  provided by the following 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"

4

128位

“ 8-4-4-4-12”格式仅供人类阅读。该UUID是一个真正的128位数字。

考虑到字符串格式在存储或存储在内存中时需要的字节数是128位数字的两倍。我建议在内部使用该数字,并且当需要在UI上显示它或将其导出到文件中时,请使用字符串格式。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.