Why is the 17th digit of version 4 GUIDs limited to only 4 possibilities?

巧了我就是萌 提交于 2019-12-30 18:56:07

问题


I understand that this doesn't take a significant chunk off of the entropy involved, and that even if a whole nother character of the GUID was reserved (for any purpose), we still would have more than enough for every insect to have one, so I'm not worried, just curious.

As this great answer shows, the Version 4 algorithm for generating GUIDs has the following format:

xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
  1. x is random
  2. 4 is constant, this represents the version number.
  3. y is one of: 8, 9, A, or B

The RFC spec for UUIDs says that these bits must be set this way, but I don't see any reason given.

Why is the third bullet (the 17th digit) limited to only those four digits?


回答1:


Quoting the estimable Mr. Lippert

First off, what bits are we talking about when we say “the bits”? We already know that in a “random” GUID the first hex digit of the third section is always 4....there is additional version information stored in the GUID in the bits in the fourth section as well; you’ll note that a GUID almost always has 8, 9, a or b as the first hex digit of the fourth section. So in total we have six bits reserved for version information, leaving 122 bits that can be chosen at random.

(from https://ericlippert.com/2012/05/07/guid-guide-part-three/)

tl;dr - it's more version information. To get more specific than that I suspect you're going to have to track down the author of the spec.




回答2:


Bits, not hex

Focusing on hexadecimal digits is confusing you.

A UUID is not made of hex. A UUID is made of 128 bits.

Humans would resent reading a series of 128 bits presented as a long string of 1 and 0 characters. So for the benefit of reading and writing by humans, we present the 128-bits in hex.

Always keep in mind that when you see the series of 36 hex characters with hyphens, you are not looking at a UUID. You are looking at some text generated to represent the 128-bits of that are actually in the UUID.

Version & Variant

The first special meaning you mention, the “version” of UUID, is recorded using 4 bits. See section 4.1.3 of your linked spec.

The second special meaning you indicate is the “variant”. This value takes 1-3 bits. This See section 4.1.1 of your linked spec.

A hex character represents 4 bits (half an octet).

  • The Version number, being 4 bits, takes an entire a single hex character to itself.
    • Version 4 specifically uses the bits 01 00 which in hex is 4 as it is too in decimal (base 10) numbers.
  • The Variant, being 1-3 bits, does not take an entire hex character.
    • Outside the Microsoft world of GUIDs, the rest of the industry nowadays uses two bits: 10, for a decimal value of 2, as the variant. This pair of bits lands in the most significant bits of octet # 8. That octet looks like this, where ‘n’ means 0 or 1: 10 nn nn nn. A pair of hex characters represent each half of that octet. So your 17th hex digit, the first half of that 8th octet, 10 nn, can only have four possible values:
      • 10 00 (hex 8)
      • 10 01 (hex 9)
      • 10 10 (hex A)
      • 10 11 (hex B)


来源:https://stackoverflow.com/questions/47230521/why-is-the-17th-digit-of-version-4-guids-limited-to-only-4-possibilities

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!