Created
November 24, 2020 08:56
-
-
Save angularsen/92a3ba9d9a94d250accd257f9f5a3d54 to your computer and use it in GitHub Desktop.
NamespaceGuid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original source: https://github.com/Faithlife/FaithlifeUtility/blob/master/src/Faithlife.Utility/GuidUtility.cs | |
using System; | |
using System.Linq; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace Faithlife.Utility | |
{ | |
/// <summary> | |
/// Helper to create deterministic, named <see cref="Guid"/>s within a namespace.<br/> | |
/// https://tools.ietf.org/html/rfc4122 | |
/// </summary> | |
public static class NamespaceGuid | |
{ | |
/// <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="name">The name (within that namespace).</param> | |
/// <returns>A UUID derived from the namespace and name.</returns> | |
public static Guid Create(Guid namespaceId, string name) => Create(namespaceId, name, 5); | |
/// <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="name">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, string name, int version) | |
{ | |
if (name is null) | |
throw new ArgumentNullException(nameof(name)); | |
// convert the name to a sequence of octets (as defined by the standard or conventions of its namespace) (step 3) | |
// ASSUME: UTF-8 encoding is always appropriate | |
return Create(namespaceId, Encoding.UTF8.GetBytes(name), version); | |
} | |
/// <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> | |
/// <returns>A UUID derived from the namespace and name.</returns> | |
public static Guid Create(Guid namespaceId, byte[] nameBytes) => Create(namespaceId, nameBytes, 5); | |
/// <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) | |
var namespaceBytes = namespaceId.ToByteArray(); | |
SwapByteOrder(namespaceBytes); | |
// compute the hash of the namespace ID concatenated with the name (step 4) | |
var 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) | |
var 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); | |
} | |
/// <summary> | |
/// The namespace for fully-qualified domain names (from RFC 4122, Appendix C). | |
/// </summary> | |
public static readonly Guid DnsNamespace = new Guid("6ba7b810-9dad-11d1-80b4-00c04fd430c8"); | |
/// <summary> | |
/// The namespace for URLs (from RFC 4122, Appendix C). | |
/// </summary> | |
public static readonly Guid UrlNamespace = new Guid("6ba7b811-9dad-11d1-80b4-00c04fd430c8"); | |
/// <summary> | |
/// The namespace for ISO OIDs (from RFC 4122, Appendix C). | |
/// </summary> | |
public static readonly Guid IsoOidNamespace = new Guid("6ba7b812-9dad-11d1-80b4-00c04fd430c8"); | |
// Converts a GUID (expressed as a byte array) to/from network order (MSB-first). | |
internal static void SwapByteOrder(byte[] guid) | |
{ | |
SwapBytes(guid, 0, 3); | |
SwapBytes(guid, 1, 2); | |
SwapBytes(guid, 4, 5); | |
SwapBytes(guid, 6, 7); | |
} | |
private static void SwapBytes(byte[] guid, int left, int right) | |
{ | |
var temp = guid[left]; | |
guid[left] = guid[right]; | |
guid[right] = temp; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original source: https://github.com/Faithlife/FaithlifeUtility/blob/master/tests/Faithlife.Utility.Tests/GuidUtilityTests.cs | |
using System; | |
using Faithlife.Utility; | |
using Xunit; | |
namespace Faithlife.Utility.Tests | |
{ | |
/// <summary> | |
/// Tests <see cref="NamespaceGuid"/>. | |
/// </summary> | |
public class NamespaceGuidTests | |
{ | |
[Fact] | |
public void ToNetworkOrder() | |
{ | |
var guid = new Guid(0x01020304, 0x0506, 0x0708, 9, 10, 11, 12, 13, 14, 15, 16); | |
var bytes = guid.ToByteArray(); | |
Assert.Equal(new byte[] {4, 3, 2, 1, 6, 5, 8, 7, 9, 10, 11, 12, 13, 14, 15, 16}, bytes); | |
NamespaceGuid.SwapByteOrder(bytes); | |
Assert.Equal(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, bytes); | |
} | |
[Fact] | |
public void CreateVersion3FromWidgetsCom() | |
{ | |
// run the test case from RFC 4122 Appendix B, as updated by http://www.rfc-editor.org/errata_search.php?rfc=4122 | |
var guid = NamespaceGuid.Create(NamespaceGuid.DnsNamespace, "www.widgets.com", 3); | |
Assert.Equal(new Guid("3d813cbb-47fb-32ba-91df-831e1593ac29"), guid); | |
} | |
[Fact] | |
public void CreateVersion3FromPythonOrg() | |
{ | |
// run the test case from the Python implementation (http://docs.python.org/library/uuid.html#uuid-example) | |
var guid = NamespaceGuid.Create(NamespaceGuid.DnsNamespace, "python.org", 3); | |
Assert.Equal(new Guid("6fa459ea-ee8a-3ca4-894e-db77e160355e"), guid); | |
} | |
[Fact] | |
public void CreateVersion5FromPythonOrg() | |
{ | |
// run the test case from the Python implementation (http://docs.python.org/library/uuid.html#uuid-example) | |
var guid = NamespaceGuid.Create(NamespaceGuid.DnsNamespace, "python.org", 5); | |
Assert.Equal(new Guid("886313e1-3b8a-5372-9b90-0c9aee199e5d"), guid); | |
} | |
[Fact] | |
public void CreateNullName() | |
{ | |
Assert.Throws<ArgumentNullException>(() => NamespaceGuid.Create(NamespaceGuid.DnsNamespace, default(string)!)); | |
} | |
[Fact] | |
public void CreateInvalidVersion() | |
{ | |
Assert.Throws<ArgumentOutOfRangeException>(() => NamespaceGuid.Create(NamespaceGuid.DnsNamespace, "www.example.com", 4)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A stripped down version based on https://github.com/Faithlife/FaithlifeUtility/blob/master/src/Faithlife.Utility/GuidUtility.cs.
You can more easily copy this code and tests into your own codebase.
Found in StackOverflow answer to "How to Create Deterministic Guids".