Skip to content

Commit

Permalink
Update documentation to reference RFC 9562.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Oct 5, 2024
1 parent 75d059d commit c96efa9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 34 deletions.
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

NGuid provides efficient creation of name-based and random GUIDs according to
[RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122)
and the current [revision working draft](https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis):
and [RFC 9562](https://datatracker.ietf.org/doc/html/rfc9562)::

* Version 3 - created from an MD5 hash of a name
* Version 5 - created from a SHA1 hash of a name
Expand All @@ -23,25 +23,18 @@ var guid = GuidHelpers.CreateFromName(GuidHelpers.DnsNamespace, "www.example.org

// can also create "Version 3": {0012416f-9eec-3ed4-a8b0-3bceecde1cd9}
var guidv3 = GuidHelpers.CreateFromName(GuidHelpers.DnsNamespace, "www.example.org"u8, version: 3);
```

### Experimental

The following APIs are based on the current [working draft](https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis)
and are subject to change:

```csharp
// converts {6ba7b810-9dad-11d1-80b4-00c04fd430c8} to {1d19dad6-ba7b-6810-80b4-00c04fd430c8}
var guidv6 = GuidHelpers.CreateVersion6FromVersion1(GuidHelpers.DnsNamespace);

// creates a v7 GUID using the current time and random data
// creates a v7 GUID using the current time and random data
var guidv7 = GuidHelpers.CreateVersion7();

// .NET 8 only: specify a TimeProvider to provide the timestamp
var guidv7WithTime = GuidHelpers.CreateVersion7(TimeProvider.System);

// creates a v8 GUID using the specified data
ReadOnlySpan<byte> bytes = GetBytes();
ReadOnlySpan<byte> bytes = GetBytesFromSomewhere();
var guidv8 = GuidHelpers.CreateVersion8(bytes);

// creates a name-based v8 GUID using the specified hash algorithm
Expand Down
19 changes: 5 additions & 14 deletions src/NGuid/GuidHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ public static Guid CreateFromName(Guid namespaceId, ReadOnlySpan<byte> name, int
/// Creates a new Version 6 UUID based on the current time and a random node ID.
/// </summary>
/// <returns>A new Version 6 UUID based on the current time and a random node ID.</returns>
/// <remarks>This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-uuid-version-6">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
public static Guid CreateVersion6() =>
CreateVersion6(DateTimeOffset.UtcNow);

Expand All @@ -151,7 +150,6 @@ public static Guid CreateVersion6() =>
/// </summary>
/// <param name="timeProvider">A <see cref="TimeProvider"/> that can provide the current UTC time.</param>
/// <returns>A new Version 6 UUID based on the specified time and a random node ID.</returns>
/// <remarks>This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-uuid-version-6">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
public static Guid CreateVersion6(TimeProvider timeProvider)
{
ArgumentNullException.ThrowIfNull(timeProvider);
Expand All @@ -164,7 +162,6 @@ public static Guid CreateVersion6(TimeProvider timeProvider)
/// </summary>
/// <param name="timestamp">The timestamp to be used to fill the <c>time_high</c>, <c>time_mid</c>, and <c>time_low</c> fields of the UUID.</param>
/// <returns>A new time-based Version 6 UUID.</returns>
/// <remarks>This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-uuid-version-6">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
#if NET6_0_OR_GREATER
[SkipLocalsInit]
#endif
Expand Down Expand Up @@ -199,7 +196,6 @@ private static Guid CreateVersion6(DateTimeOffset timestamp)
/// </summary>
/// <param name="guid">The Version 1 UUID to convert.</param>
/// <returns>A UUID in Version 6 format, with the timestamp in MSB order.</returns>
/// <remarks>This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-uuid-version-6">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
#if NET6_0_OR_GREATER
[SkipLocalsInit]
#endif
Expand Down Expand Up @@ -244,7 +240,6 @@ public static Guid CreateVersion6FromVersion1(Guid guid)
/// Creates a new Version 7 UUID based on the current time combined with random data.
/// </summary>
/// <returns>A new Version 7 UUID based on the current time and random data.</returns>
/// <remarks>This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-uuid-version-7">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
public static Guid CreateVersion7() =>
CreateVersion7(DateTimeOffset.UtcNow);

Expand All @@ -254,7 +249,6 @@ public static Guid CreateVersion7() =>
/// </summary>
/// <param name="timeProvider">A <see cref="TimeProvider"/> that can provide the current UTC time.</param>
/// <returns>A new Version 7 UUID based on the specified time and random data.</returns>
/// <remarks>This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-uuid-version-7">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
public static Guid CreateVersion7(TimeProvider timeProvider)
{
ArgumentNullException.ThrowIfNull(timeProvider);
Expand All @@ -267,7 +261,6 @@ public static Guid CreateVersion7(TimeProvider timeProvider)
/// </summary>
/// <param name="timestamp">The timestamp to be used to fill the <c>unix_ts_ms</c> field of the UUID.</param>
/// <returns>A new time-based Version 7 UUID.</returns>
/// <remarks>This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-uuid-version-7">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
#if NET6_0_OR_GREATER
[SkipLocalsInit]
#endif
Expand Down Expand Up @@ -310,7 +303,7 @@ private static Guid CreateVersion7(DateTimeOffset timestamp)
/// This is the opposite of how the <see cref="Guid(byte[])"/> constructor treats its argument, and
/// will cause <see cref="Guid.ToByteArray()"/> to return a byte array whose bytes values are
/// "reversed" compared to the input values in <paramref name="bytes"/>.
/// This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-uuid-version-8">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
/// </remarks>
public static Guid CreateVersion8(byte[] bytes)
{
#if NET6_0_OR_GREATER
Expand Down Expand Up @@ -345,12 +338,12 @@ public static Guid CreateVersion8(byte[] bytes)
/// </summary>
/// <param name="bytes">The bytes to use to initialize the UUID.</param>
/// <returns>A new Version 8 UUID.</returns>
/// <remarks>This method treats the in MSB order; the first byte in <paramref name="bytes"/>
/// <remarks>This method treats the bytes in MSB order; the first byte in <paramref name="bytes"/>
/// will be the first byte in the standard string representation of the returned <see cref="Guid"/>.
/// This is the opposite of how the <see cref="Guid(byte[])"/> constructor treats its argument, and
/// will cause <see cref="Guid.ToByteArray()"/> to return a byte array whose bytes values are
/// "reversed" compared to the input values in <paramref name="bytes"/>.
/// This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-uuid-version-8">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
/// </remarks>
[SkipLocalsInit]
public static Guid CreateVersion8(ReadOnlySpan<byte> bytes)
{
Expand All @@ -374,13 +367,12 @@ public static Guid CreateVersion8(ReadOnlySpan<byte> bytes)

/// <summary>
/// Creates a Version 8 UUID from a name in the specified namespace using the specified hash algorithm, according to the algorithm
/// in <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-name-based-uuid-generation">draft-ietf-uuidrev-rfc4122bis-14, section 6.5</a>.
/// in <a href="https://datatracker.ietf.org/doc/html/rfc9562#name-name-based-uuid-generation">RFC 9562, section 6.5</a>.
/// </summary>
/// <param name="hashAlgorithmName">The name of the hash algorithm to use. Supported values are <c>SHA256</c>, <c>SHA384</c>, and <c>SHA512</c>.</param>
/// <param name="namespaceId">The namespace ID.</param>
/// <param name="name">The name within that namespace ID.</param>
/// <returns>A version 8 UUID formed by hashing the hash space ID, namespace ID, and name.</returns>
/// <remarks>This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-name-based-uuid-generation">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
public static Guid CreateVersion8FromName(HashAlgorithmName hashAlgorithmName, Guid namespaceId, byte[] name)
{
#if NET6_0_OR_GREATER
Expand Down Expand Up @@ -413,13 +405,12 @@ public static Guid CreateVersion8FromName(HashAlgorithmName hashAlgorithmName, G
#if NET6_0_OR_GREATER
/// <summary>
/// Creates a Version 8 UUID from a name in the specified namespace using the specified hash algorithm, according to the algorithm
/// in <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-name-based-uuid-generation">draft-ietf-uuidrev-rfc4122bis-14, section 6.5</a>.
/// in <a href="https://datatracker.ietf.org/doc/html/rfc9562#name-name-based-uuid-generation">RFC 9562, section 6.5</a>.
/// </summary>
/// <param name="hashAlgorithmName">The name of the hash algorithm to use. Supported values are <c>SHA256</c>, <c>SHA384</c>, and <c>SHA512</c>.</param>
/// <param name="namespaceId">The namespace ID.</param>
/// <param name="name">The name within that namespace ID.</param>
/// <returns>A version 8 UUID formed by hashing the hash space ID, namespace ID, and name.</returns>
/// <remarks>This method is based on <a href="https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-14#name-name-based-uuid-generation">draft-ietf-uuidrev-rfc4122bis-14</a> and is subject to change.</remarks>
[SkipLocalsInit]
public static Guid CreateVersion8FromName(HashAlgorithmName hashAlgorithmName, Guid namespaceId, ReadOnlySpan<byte> name)
{
Expand Down
13 changes: 3 additions & 10 deletions src/NGuid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

NGuid provides efficient creation of name-based and random GUIDs according to
[RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122)
and the current [revision working draft](https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis):
and [RFC 9562](https://datatracker.ietf.org/doc/html/rfc9562):

* Version 3 - created from an MD5 hash of a name
* Version 5 - created from a SHA1 hash of a name
Expand All @@ -18,25 +18,18 @@ var guid = GuidHelpers.CreateFromName(GuidHelpers.DnsNamespace, "www.example.org

// can also create "Version 3": {0012416f-9eec-3ed4-a8b0-3bceecde1cd9}
var guidv3 = GuidHelpers.CreateFromName(GuidHelpers.DnsNamespace, "www.example.org"u8, version: 3);
```

### Experimental

The following APIs are based on the current [working draft](https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis)
and are subject to change:

```csharp
// converts {6ba7b810-9dad-11d1-80b4-00c04fd430c8} to {1d19dad6-ba7b-6810-80b4-00c04fd430c8}
var guidv6 = GuidHelpers.CreateVersion6FromVersion1(GuidHelpers.DnsNamespace);

// creates a v7 GUID using the current time and random data
// creates a v7 GUID using the current time and random data
var guidv7 = GuidHelpers.CreateVersion7();

// .NET 8 only: specify a TimeProvider to provide the timestamp
var guidv7WithTime = GuidHelpers.CreateVersion7(TimeProvider.System);

// creates a v8 GUID using the specified data
ReadOnlySpan<byte> bytes = GetBytes();
ReadOnlySpan<byte> bytes = GetBytesFromSomewhere();
var guidv8 = GuidHelpers.CreateVersion8(bytes);

// creates a name-based v8 GUID using the specified hash algorithm
Expand Down

0 comments on commit c96efa9

Please sign in to comment.