XstarS.GuidGenerators 2.0.0-preview-01

This is a prerelease version of XstarS.GuidGenerators.
There is a newer version of this package available.
See the version list below for details.
dotnet add package XstarS.GuidGenerators --version 2.0.0-preview-01
NuGet\Install-Package XstarS.GuidGenerators -Version 2.0.0-preview-01
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XstarS.GuidGenerators" Version="2.0.0-preview-01" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add XstarS.GuidGenerators --version 2.0.0-preview-01
#r "nuget: XstarS.GuidGenerators, 2.0.0-preview-01"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install XstarS.GuidGenerators as a Cake Addin
#addin nuget:?package=XstarS.GuidGenerators&version=2.0.0-preview-01&prerelease

// Install XstarS.GuidGenerators as a Cake Tool
#tool nuget:?package=XstarS.GuidGenerators&version=2.0.0-preview-01&prerelease

.NET GUID Generator

Provides RFC 4122 UUID and RFC4122bis UUIDREV (draft) compliant GUID generators for .NET platform.

RFC 4122 UUID Standard

RFC 4122 defines the following five versions of UUID:

  • Version 1: The time-based version, contains a 60-bit timestamp and a 12-bit MAC address
  • Version 2: DCE Security version, contains a 28-bit timestamp, a 12-bit MAC address and a 32-bit local ID
  • Version 3: The name-based version, using MD5 hashing to compute the hash of the namespace and name
  • Version 4: The randomly or pseudo-andomly generated version, equivalent to Guid.NewGuid() in .NET
  • Version 5: The name-based version, using SHA-1 hashing to compute the hash of the namespace and name

There is also a special Nil UUID whose bytes are all 0x00s, which is equivalent to Guid.Empty in .NET.

RFC4122bis UUIDREV Draft

RFC4122bis UUIDREV defines the following three versions of UUID:

  • Version 6: The reordered time-based version, field-compatible with Version 1 except that the timestamp is reordered to big-endian order
  • Version 7: The Unix Epoch time-based version, contains a 48-bit timestamp and a 74-bit random number, field-compatible with ULID
  • Version 8: Reserved for custom UUID formats, fields except the variant and version are user-defined

There is also a special Max UUID whose bytes are all 0xffs, which has no equivalent implementation in .NET (provided in this project).

GUID Generator Library Usage

Get Generator Instance by Static Properties

using XNetEx.Guids;
using XNetEx.Guids.Generators;

// time-based GUID generation.
var guidV1 = GuidGenerator.Version1.NewGuid();
// 3944a871-aa14-11ed-8791-a9a9a46de54f

// name-based GUID generation.
var guidV5 = GuidGenerator.Version5.NewGuid(GuidNamespaces.Dns, "github.com");
// 6fca3dd2-d61d-58de-9363-1574b382ea68s

// Unix time-based GUID generation.
var guidV7 = GuidGenerator.Version7.NewGuid();
// 018640c6-0dc9-7189-a644-31acdba4cabc

Get Generator Instance by the Factory Method

using XNetEx.Guids;
using XNetEx.Guids.Generators;

// generate time-based GUID generation.
var guidGenV1 = GuidGenerator.OfVersion(GuidVersion.Version1);
var guidV1 = guidGenV1.NewGuid();
// 3944a871-aa14-11ed-8791-a9a9a46de54f

// generate name-based GUID.
var guidGenV5 = GuidGenerator.OfVersion(GuidVersion.Version5);
var guidV5 = guidGenV5.NewGuid(GuidNamespaces.Dns, "github.com");
// 6fca3dd2-d61d-58de-9363-1574b382ea68s

// generate Unix time-based GUID.
var guidGenV7 = GuidGenerator.OfVersion(GuidVersion.Version7);
var guidV7 = guidGenV7.NewGuid();
// 018640c6-0dc9-7189-a644-31acdba4cabc

GUID Generator State Storage

RFC 4122 Section 4.2.1

Optional support and requires configuration to enable:

using System;
using System.IO;
using XNetEx.Guids.Generators;

// listen state storage exceptions.
GuidGenerator.StateStorageException += (sender, e) =>
{
    if ((e.OperationType != FileAccess.Read) ||
        (e.Exception is not FileNotFoundException))
    {
        Console.Error.WriteLine(e.Exception);
    }
};
// set storage file path and load state.
var loadResult = GuidGenerator.SetStateStorageFile("state.bin");

Component-Based GUID Building

using System;
using XNetEx.Guids;

// build time-based GUID.
var guidV6 = Guid.Empty
    .ReplaceVariant(GuidVariant.Rfc4122)
    .ReplaceVersion(GuidVersion.Version6)
    .ReplaceTimestamp(new DateTime(0x08BEFFD14FDBF810, DateTimeKind.Utc))
    .ReplaceClockSequence((short)0x00b4)
    .ReplaceNodeId(new byte[] { 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8 });
    // 1d19dad6-ba7b-6810-80b4-00c04fd430c8

// build Unix time-based GUID.
var guidV7 = Guid.NewGuid()
    .ReplaceVersion(GuidVersion.Version7)
    .ReplaceTimestamp(new DateTime(0x08D9F638A666EB00, DateTimeKind.Utc));
    // 017f22e2-79b0-774a-8e21-a60c1ca56e82

Performance Benchmark

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22621
AMD Ryzen 7 5800H with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK=7.0.102
  [Host]     : .NET 6.0.13 (6.0.1322.58009), X64 RyuJIT
  DefaultJob : .NET 6.0.13 (6.0.1322.58009), X64 RyuJIT
Method GuidCount Mean StdDev Ratio Allocated
GuidNewGuid 1 46.045 ns 1.9225 ns 1.00 -
EmptyGenerate 1 3.342 ns 0.0408 ns 0.07 -
GuidV1Generate 1 50.837 ns 0.7831 ns 1.11 -
GuidV2Generate 1 60.078 ns 0.7185 ns 1.32 -
GuidV3Generate 1 240.826 ns 2.6978 ns 5.28 -
GuidV4Generate 1 46.548 ns 0.5383 ns 1.02 -
GuidV5Generate 1 237.623 ns 1.3916 ns 5.21 -
GuidV6Generate 1 48.740 ns 0.2368 ns 1.08 -
GuidV7Generate 1 81.095 ns 0.5619 ns 1.78 -
GuidV8Generate 1 87.887 ns 0.7458 ns 1.93 -
MaxValueGenerate 1 3.311 ns 0.0328 ns 0.07 -
GuidNewGuid 1000 45,720.244 ns 317.3079 ns 1.00 -
EmptyGenerate 1000 1,172.361 ns 10.5002 ns 0.03 -
GuidV1Generate 1000 48,985.832 ns 448.0066 ns 1.07 3 B
GuidV2Generate 1000 57,924.801 ns 644.1914 ns 1.27 3 B
GuidV3Generate 1000 237,845.954 ns 1,407.9636 ns 5.20 -
GuidV4Generate 1000 45,261.486 ns 342.1985 ns 0.99 -
GuidV5Generate 1000 234,121.297 ns 1,697.6017 ns 5.12 -
GuidV6Generate 1000 48,052.799 ns 260.9060 ns 1.05 -
GuidV7Generate 1000 80,142.755 ns 645.8843 ns 1.75 -
GuidV8Generate 1000 87,819.589 ns 598.3742 ns 1.92 -
MaxValueGenerate 1000 1,171.756 ns 8.0670 ns 0.03 -
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on XstarS.GuidGenerators:

Package Downloads
XstarS.GuidModule

Provides RFC 4122/9562 compliant GUID operations for F#.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.4.0-preview 92 4/20/2024
2.3.1 119 3/24/2024
2.3.0 249 2/24/2024
2.2.1 244 2/8/2024
2.2.0 104 1/26/2024
2.1.2 108 1/22/2024
2.1.1 105 1/20/2024
2.1.0 118 1/12/2024
2.0.0 154 12/24/2023
2.0.0-rc-19 128 12/22/2023
2.0.0-rc-16 140 12/12/2023
2.0.0-rc-15 119 12/6/2023
2.0.0-rc-14 144 11/26/2023
2.0.0-rc-12.5 82 10/15/2023
2.0.0-rc-12 114 10/15/2023
2.0.0-rc-00 137 7/15/2023
2.0.0-preview-04.5 70 6/4/2023
2.0.0-preview-04 159 5/28/2023
2.0.0-preview-03.8 67 5/20/2023
2.0.0-preview-03.5 66 5/14/2023
2.0.0-preview-03 161 4/15/2023
2.0.0-preview-02.5 85 3/26/2023
2.0.0-preview-02 167 3/4/2023
2.0.0-preview-01 177 2/12/2023
1.14.0-preview 87 4/20/2024
1.13.0 113 2/24/2024
1.12.1 114 2/8/2024
1.12.0 88 1/26/2024
1.11.2 101 1/22/2024
1.11.1 93 1/20/2024
1.11.0 96 1/12/2024
1.10.0 133 12/24/2023
1.9.5 131 12/22/2023
1.9.1 175 12/12/2023
1.9.0 159 12/6/2023
1.8.0 155 11/26/2023
1.7.8 173 10/15/2023
1.7.5 203 6/4/2023
1.7.1 186 5/28/2023
1.7.0 177 5/20/2023
1.6.0 181 5/13/2023
1.5.0 404 2/5/2023
1.4.0 427 1/21/2023
1.3.1 439 1/15/2023
1.3.0 443 12/31/2022
1.2.0 650 9/30/2022
1.1.0 640 8/5/2022
1.0.1 906 7/5/2022
1.0.0 497 7/3/2022