Repository navigation
Home
Enbrea.GuidFactory is a .NET library for generating GUID/UUID values according to RFC 9562.
- Supports
.NET 10. - Supports name-based UUID version 3 using MD5
- Supports random UUID version 4
- Supports name-based UUID version 5 using SHA-1
- Supports time-ordered UUID version 7
- Supports string and byte-oriented input for name-based UUIDs
- Provides the standard DNS, URL, ISO OID, and X.500 namespaces
- Generates deterministic UUIDs for version 3 and version 5
- Zero external dependencies
The implementation of the name-based UUID generation is based in part on code from the blog article Generating a deterministic GUID.
Install the package from NuGet using the .NET CLI:
dotnet add package Enbrea.GuidFactoryOr using the Package Manager Console in Visual Studio:
Install-Package Enbrea.GuidFactoryOr add the package reference directly to your project file:
<ItemGroup>
<PackageReference Include="Enbrea.GuidFactory" Version="x.y.z" />
</ItemGroup>Replace x.y.z with the desired package version.
To create a deterministic GUID using UUID version 5, provide a namespace and a name:
using Enbrea.GuidFactory;
var guid = GuidGenerator.CreateVersion5(GuidNamespaces.Dns, "www.example.com");
Console.WriteLine(guid);For the DNS namespace and the name www.example.com, the result is:
2ed6657d-e927-568b-95e1-2665a8aea6a2
Calling the method again with the same namespace and name produces exactly the same GUID:
var first = GuidGenerator.CreateVersion5(GuidNamespaces.Dns, "www.example.com");
var second = GuidGenerator.CreateVersion5(GuidNamespaces.Dns, "www.example.com");
Console.WriteLine(first == second); // TrueString names are encoded as UTF-8 before generating the UUID.
Name-based UUIDs can also be created directly from a sequence of bytes:
var name = Encoding.UTF8.GetBytes("www.example.com");
var guid = GuidGenerator.CreateVersion5(GuidNamespaces.Dns, name);The ReadOnlySpan<byte> overload uses the supplied bytes directly. Therefore, UTF-8 encoded bytes produce the same UUID as the corresponding string:
var guidFromString = GuidGenerator.CreateVersion5(GuidNamespaces.Dns, "www.example.com");
var guidFromBytes = GuidGenerator.CreateVersion5(GuidNamespaces.Dns, Encoding.UTF8.GetBytes("www.example.com"));
Console.WriteLine(guidFromString == guidFromBytes); // TrueThis is the essential difference between deterministic UUID versions 3 and 5 and randomly generated UUID version 4 values: deterministic UUIDs are derived from input data rather than being randomly generated.
UUID version 7 values are time-ordered and combine a Unix timestamp with random data.
Enbrea.GuidFactory supports UUID versions 3, 4, 5, and 7:
| Version | Type | Usage |
|---|---|---|
| UUID version 3 | Name-based | Deterministic UUID generation using MD5 |
| UUID version 4 | Random | Random UUID generation |
| UUID version 5 | Name-based | Deterministic UUID generation using SHA-1 |
| UUID version 7 | Unix Epoch time-based | Time-ordered UUID generation |
To create a UUID version 3:
var guid = GuidGenerator.CreateVersion3(GuidNamespaces.Dns, "www.example.com");A UUID version 3 can also be created from bytes:
var guid = GuidGenerator.CreateVersion3(GuidNamespaces.Dns, Encoding.UTF8.GetBytes("www.example.com"));To create a UUID version 4:
var guid = GuidGenerator.CreateVersion4();To create a UUID version 5:
var guid = GuidGenerator.CreateVersion5(GuidNamespaces.Dns, "www.example.com");A UUID version 5 can also be created from bytes:
var guid = GuidGenerator.CreateVersion5(GuidNamespaces.Dns, Encoding.UTF8.GetBytes("www.example.com"));To create a UUID version 7:
var guid = GuidGenerator.CreateVersion7();A specific timestamp can also be supplied when creating a UUID version 7:
var guid = GuidGenerator.CreateVersion7(timestamp);Name-based UUIDs are generated within a namespace. The namespace ensures that identical names used for different purposes do not automatically produce identical UUIDs.
Enbrea.GuidFactory provides the standard namespaces defined for name-based UUID generation:
| Property | Namespace |
|---|---|
GuidNamespaces.Dns |
Fully-qualified domain names |
GuidNamespaces.Url |
URLs |
GuidNamespaces.IsoOid |
ISO object identifiers |
GuidNamespaces.X500 |
X.500 distinguished names |
For example, a domain name should normally use the DNS namespace:
var guid = GuidGenerator.CreateVersion5(GuidNamespaces.Dns, "www.example.com");A URL can use the URL namespace:
var guid = GuidGenerator.CreateVersion5(GuidNamespaces.Url, "https://www.beispiel.de/api");The namespace is part of the UUID input. Consequently, using the same name with different namespaces produces different UUIDs:
var dnsGuid = GuidGenerator.CreateVersion5(GuidNamespaces.Dns, "www.example.com");
var urlGuid = GuidGenerator.CreateVersion5(GuidNamespaces.Url, "www.example.com");
Console.WriteLine(dnsGuid == urlGuid); // FalseApplications are not limited to the predefined namespaces. Any GUID can serve as a namespace identifier.
For example:
// Create custom namespace
var productNamespace = Guid.Parse("926b6b65-23e2-4f82-b3c7-31e70db007cc");
// Use this custom namespace
var productId = GuidGenerator.CreateVersion5(productNamespace, "product-12345");A custom namespace should itself be stable. Once UUIDs derived from it are persisted or exchanged with other systems, changing the namespace changes every UUID generated from the same names.
Custom namespaces are useful when identifiers need to be scoped to an application, tenant, entity type, import source, or other domain-specific context.
- RFC 9562 > Universally Unique IDentifiers (UUIDs)
- IANA UUID Registry > UUID Subtypes and Namespace IDs