有没有办法查明 GUID 是基于名称的 UUID 还是基于非名称的 UUID?

Is there a way to find out if GUID is a name-based UUID or a non-named-based UUID?

我看到我可以创建基于名称的 UUID/确定性 GUID,请参阅 。

non-name-based UUID 的示例:

System.Guid id1 = System.Guid.NewGuid()
// id1 = {780dc51b-8eb3-4d66-b76d-8ab44e1311e6} for example

named-based UUID 的示例:

string filePath = "Test";
System.Guid id2 = GuidUtility.Create(GuidUtility.UrlNamespace, filePath);
// id2 = {64ad81d8-15e2-5110-9024-83c64dc485f9}

现在我有以下问题:例如,在 C# 中有没有办法查明 GUID 是 name-based UUID 还是 non-named-based UUID

一般来说,对于非基于命名的 guid,第 13 位数字(紧接第二个破折号之后)将为 4,对于基于命名的 guid 将为 3 或 5。

这并非普遍适用,但适用于您正在使用的代码。

"0b5415ec-657c-4a80-9199-f7993aff3908"

"275b74ef-e22a-59d6-8b2c-4face1410f59"

版本号描述在RFC 4122:

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

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). The following table lists the currently-defined versions for this UUID variant:

  1. The time-based version specified in this document.
  2. DCE Security version, with embedded POSIX UIDs.
  3. The name-based version specified in this document that uses MD5 hashing.
  4. The randomly or pseudo-randomly generated version specified in this document.
  5. The name-based version specified in this document that uses SHA-1 hashing.

使用以下代码,您可以检查 GUID 是否基于名称:

public static bool IsNameBased(Guid id)
{
    byte version = GetVersion(id);
    return version == 3 || version == 5;
}

public static byte GetVersion(Guid id)
{
    byte[] byte_array = id.ToByteArray();
    byte version = (byte)(byte_array[7] >> 4);
    return version;
}