c# - 根据字符串生成UUID

标签 c# uuid guid

如何在 C# 中生成具有命名空间和名称作为字符串的确定性 GUID/UUID v3/v5(根据 RFC4122,您需要提供命名空间作为 GUID 和名称作为字符串)提供给函数,所以我想提供两个字符串而不是 namespace 的 guid 和名称的字符串,并且 namespace 的字符串和名称的字符串始终具有相同的 GUID/UUID。 是否使用 MD5/SHA1 对 namespace 字符串进行哈希处理并通过 Guid(byte[]) 构造函数创建新的 Guid 是一种安全的方法来完成此操作,因此我可以进一步提供它来运行? 我不是在询问通过 Guid.TryParse() 将类似 guid 的字符串解析到命名空间,而是将任何字符串转换为 guid 命名空间以进一步为下面的函数提供它,但它也具有确定性。 根据https://github.com/Faithlife/FaithlifeUtility/blob/master/src/Faithlife.Utility/GuidUtility.cs 和 RFC 4122 这就是在给定 GUID 命名空间和字符串名称/任何字符串的情况下创建 GUID 的方法。

        /// <summary>
    /// Creates a name-based UUID using the algorithm from RFC 4122 §4.3.
    /// </summary>
    /// <param name="namespaceId">The ID of the namespace.</param>
    /// <param name="nameBytes">The name (within that namespace).</param>
    /// <param name="version">The version number of the UUID to create; this value must be either
    /// 3 (for MD5 hashing) or 5 (for SHA-1 hashing).</param>
    /// <returns>A UUID derived from the namespace and name.</returns>
    public static Guid Create(Guid namespaceId, byte[] nameBytes, int version)
    {
        if (version != 3 && version != 5)
            throw new ArgumentOutOfRangeException(nameof(version), "version must be either 3 or 5.");

        // convert the namespace UUID to network order (step 3)
        byte[] namespaceBytes = namespaceId.ToByteArray();
        SwapByteOrder(namespaceBytes);

        // compute the hash of the namespace ID concatenated with the name (step 4)
        byte[] data = namespaceBytes.Concat(nameBytes).ToArray();
        byte[] hash;
        using (var algorithm = version == 3 ? (HashAlgorithm) MD5.Create() : SHA1.Create())
            hash = algorithm.ComputeHash(data);

        // most bytes from the hash are copied straight to the bytes of the new GUID (steps 5-7, 9, 11-12)
        byte[] newGuid = new byte[16];
        Array.Copy(hash, 0, newGuid, 0, 16);

        // 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 (step 8)
        newGuid[6] = (byte) ((newGuid[6] & 0x0F) | (version << 4));

        // set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively (step 10)
        newGuid[8] = (byte) ((newGuid[8] & 0x3F) | 0x80);

        // convert the resulting UUID to local byte order (step 13)
        SwapByteOrder(newGuid);
        return new Guid(newGuid);
    }

最佳答案

不,您的建议无效,因为它从根本上破坏了 UUID 的工作方式。为您的命名空间使用真实的 UUID。

实现此目的的一种方便(且有效)的方法是分层命名空间。首先,使用标准 DNS 命名空间 UUID 加上您的域名来生成您的根命名空间:

Guid nsDNS = new Guid("6ba7b810-9dad-11d1-80b4-00c04fd430c8"); Guid nsRoot = Guid.Create(nsDNS, "myapp.example.com", 5);

然后为您的字符串创建命名空间 UUID:

Guid nsFoo = Guid.Create(nsRoot, "Foo", 5);

现在您可以使用带有个人名称的新 Foo 命名空间 UUID:

Guid bar = Guid.Create(nsFoo, "Bar", 5);

这样做的好处是,其他任何人都将获得与您完全不同的 UUID,即使他们的字符串(显然除了域)与您的相同,如果您的数据集被合并,则可以防止冲突,但它是完全确定的,逻辑和 self 记录。

(注意:我从未真正使用过 C#,所以如果我的语法有轻微错误,请随时进行编辑。无论如何,我认为模式很清楚。)

关于c# - 根据字符串生成UUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61267254/

相关文章:

c# - (关于最佳实践的问题)为什么默认有 "using System.Text"?

android - AltBeacon 的 Android Beacon 库获取 major、minor 和 UUID

c# - 跨应用程序相同 GUID 的影响

c# - 为什么这个 C# 正则表达式会使我的程序崩溃?

c# - 如何在 Silverlight 中拉伸(stretch)控件以填充 Canvas?

c# - 如何使用 iTextSharp 阅读 PDF 包

vb.net - 将 GUID 转换为数字等效项

MySql UUID重复BUG

java - 为大数据生成最佳 UUID

random - 亚马逊如何生成订单号?