Skip to content

Extend System.Guid with a new creation API for v7 #103658

@tannergooding

Description

@tannergooding

Rationale

The UUID specification (https://datatracker.ietf.org/doc/rfc9562) defines several different UUID versions which can be created and which allow developers to produce and consume UUIDs that have a particular structure.

As such, we should expose helpers to allow creating such UUIDs. For .NET 9, the set of potential versions is detailed below, but only UUIDv7 is proposed for the time being.

As per the UUID spec, GUID is a valid alternative name and so the name of the new APIs remains NewGuid* for consistency with our existing APIs:

This specification defines UUIDs (Universally Unique IDentifiers) -- also known as GUIDs (Globally Unique IDentifiers)

API Proposal

namespace System;

public partial struct Guid
{
    // Alternatively: MaxValue -or- AllBitsSet
    public static Guid Max { get; }

    public int Variant { get; }
    public int Version { get; }

    // v1
    //     60-bit timestamp 100ns ticks since 00:00:00.00, 15 October 1582 UTC
    //     14-bit clock sequence, intended to be random once in the lifetime of system
    //     48-bit node identifier, as in IEEE 802 Node IDs

    // v2
    //     DCE Security UUIDs

    // v3
    //     128-bit MD5 of namespaceId + name converted to canonical octet sequence
    //     6-bits then replaced with the version/variant fields

    // v4
    //     Already exposed as NewGuid()

    // v5
    //     192-bit SHA1 of namespaceId + name converted to canonical octet sequence, taking the most significant 128-bits
    //     6-bits then replaced with the version/variant fields

    // v6
    //     60-bit timestamp 100ns ticks since 00:00:00.00, 15 October 1582 UTC
    //     14-bit clock sequence, intended to be random for every new UUID, can be compatible with v1
    //     48-bit node identifier, intended to be random for every new UUID, can be compatible with v1

    // v7
    //     48-bit timestamp millisecond ticks since Unix Epoch
    //     12-bits of random data -or- submillisecond timestamp (M3)
    //         M3 - Scale remaining precision to fit into the 12-bits at even intervals
    //     62-bits of random data -or- carefully seeded counter (M1 or M2)
    //         M1 - Dedicated counter, simply increment per UUID created within a given tick stamp
    //         M2 - Monotonic random, seed random then increment by random amount within a given tick stamp
    public static Guid NewGuidv7(); // uses DateTime.UtcNow
    public static Guid NewGuidv7(DateTime timestamp);

    // v8
    //     48-bits of custom data
    //     12-bits of custom data
    //     62-bits of custom data
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions