Skip to content

Commit d96a6a2

Browse files
committed
Merge branch 'new-uuid-format'
2 parents c2fe0c6 + cd60c5b commit d96a6a2

File tree

17 files changed

+859
-103
lines changed

17 files changed

+859
-103
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ name: "CodeQL"
1313

1414
on:
1515
push:
16-
branches: [ "master" ]
16+
branches: [ "**" ]
1717
pull_request:
1818
# The branches below must be a subset of the branches above
1919
branches: [ "master" ]

.github/workflows/dotnet.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: .NET
22

33
on:
44
push:
5-
branches: [ "master" ]
5+
branches: [ "**" ]
66
pull_request:
77
branches: [ "master" ]
88

@@ -13,7 +13,7 @@ jobs:
1313

1414
strategy:
1515
matrix:
16-
dotnet: [ '6.0.x' ]
16+
dotnet: [ '8.0.x' ]
1717
name: .NET ${{ matrix.dotnet }}
1818

1919
steps:

.github/workflows/package-release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ jobs:
2323
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
2424
- uses: actions/checkout@v2
2525

26-
- name: Setup .NET 6
26+
- name: Setup .NET 8
2727
uses: actions/setup-dotnet@v1
2828
with:
29-
dotnet-version: 6.0.x
29+
dotnet-version: 8.0.x
3030
source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
3131
env:
3232
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

CHANGELOG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,45 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## [Unreleased]
1010

11+
## [v2.1.0] - 2024-06-05
12+
[v2.1.0](https://github.com/TensionDev/UUIDUtil/releases/tag/v2.1.0)
13+
14+
15+
## [v2.1.0-beta] - 2024-06-05
16+
[v2.1.0-beta](https://github.com/TensionDev/UUIDUtil/releases/tag/v2.1.0-beta)
17+
18+
### Changed
19+
- Added UUID v7 generated based on current system date and time as well as Fixed Bit-Length Dedicated Counter (Method 1).
20+
- Added UUID v7 generated based on current system date and time as well as Replace Leftmost Random Bits with Increased Clock Precision (Method 3).
21+
22+
23+
## [v2.1.0-alpha] - 2023-06-15
24+
[v2.1.0-alpha](https://github.com/TensionDev/UUIDUtil/releases/tag/v2.1.0-alpha)
25+
26+
### Changed
27+
- Changed license from GNU General Public License Version 3 to Apache License Version 2.0.
28+
29+
1130
## [v2.0.0] - 2023-06-15
1231
[v2.0.0](https://github.com/TensionDev/UUIDUtil/releases/tag/v2.0.0)
1332

1433
### Changed
1534
- Changed license from GNU General Public License Version 3 to Apache License Version 2.0.
1635

1736

37+
## [v1.2.0-alpha] - 2022-12-15
38+
[v1.2.0-alpha](https://github.com/TensionDev/UUIDUtil/releases/tag/v1.2.0-alpha)
39+
40+
### Added
41+
- Added UUID v6 generated based on current system date and time as well as local Network MAC Address.
42+
- Added UUID v6 generated based on current system date and time as well as supplied Network MAC Address.
43+
- Added UUID v6 generated based on supplied system date and time as well as local Network MAC Address.
44+
- Added UUID v6 generated based on supplied system date and time as well as supplied Network MAC Address.
45+
- Added UUID v7 generated based on current system date and time as well as generated random fields.
46+
- Added UUID v7 generated based on supplied system date and time as well as generated random fields.
47+
- Added UUID v7 generated based on supplied system date and time as well as supplied random fields.
48+
49+
1850
## [v1.1.0] - 2022-05-31
1951
[v1.1.0](https://github.com/TensionDev/UUIDUtil/releases/tag/v1.1.0)
2052

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
[![Package Release](https://github.com/TensionDev/UUIDUtil/actions/workflows/package-release.yml/badge.svg)](https://github.com/TensionDev/UUIDUtil/actions/workflows/package-release.yml)
55
[![CodeQL](https://github.com/TensionDev/UUIDUtil/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/TensionDev/UUIDUtil/actions/workflows/codeql-analysis.yml)
66

7-
A project to store UUID functions within a library for future use.
8-
This project references the following documents for implementation.
7+
A project to store UUID functions within a library for future use.
8+
This project references the following documents for implementation.
99
- [Universally unique identifier - Wikipedia](https://en.wikipedia.org/wiki/Universally_unique_identifier)
1010
- [MySQL :: MySQL 8.0 Reference Manual :: 12.24 Miscellaneous Functions](https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_uuid)
1111
- [rfc4122](https://datatracker.ietf.org/doc/html/rfc4122)
12+
- [rfc9562](https://datatracker.ietf.org/doc/html/rfc9562)

UUIDUtil/UUIDNamespace.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
1-
namespace TensionDev.UUID
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// Copyright 2021 TensionDev <[email protected]>
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Text;
20+
21+
namespace TensionDev.UUID
222
{
323
/// <summary>
424
/// Class Library to generate Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) based on Version 3 (MD5 namespace name-based).
525
/// </summary>
6-
public class UUIDNamespace
26+
public static class UUIDNamespace
727
{
828
/// <summary>
929
/// Namespace for Domain Name System
1030
/// </summary>
11-
public static Uuid DNS = new Uuid(0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
31+
public static readonly Uuid DNS = new Uuid(0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
1232
/// <summary>
1333
/// Namespace for URLs
1434
/// </summary>
15-
public static Uuid URL = new Uuid(0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
35+
public static readonly Uuid URL = new Uuid(0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
1636
/// <summary>
1737
/// Namespace for ISO Object IDs (OIDs)
1838
/// </summary>
19-
public static Uuid OID = new Uuid(0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
39+
public static readonly Uuid OID = new Uuid(0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
2040
/// <summary>
2141
/// Namespace for X.500 Distinguished Names(DNs)
2242
/// </summary>
23-
public static Uuid X500 = new Uuid(0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
43+
public static readonly Uuid X500 = new Uuid(0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
2444
}
2545
}

UUIDUtil/UUIDUtil.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
99
<PackageId>TensionDev.UUID</PackageId>
10-
<Version>2.0.0</Version>
10+
<Version>2.1.0</Version>
1111
<Authors>TensionDev amsga</Authors>
1212
<Company>TensionDev</Company>
1313
<Product>TensionDev.UUID</Product>
1414
<Description>A project to store various UUID functions within a library for future use.</Description>
15-
<Copyright>Copyright (c) TensionDev 2021 - 2023</Copyright>
15+
<Copyright>Copyright (c) TensionDev 2021</Copyright>
1616
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
1717
<PackageProjectUrl>https://github.com/TensionDev/UUIDUtil</PackageProjectUrl>
1818
<RepositoryUrl>https://github.com/TensionDev/UUIDUtil</RepositoryUrl>
1919
<RepositoryType>git</RepositoryType>
2020
<PackageTags>UUID GUID</PackageTags>
2121
<PackageReleaseNotes>Change in license to Apache License 2.0.
22-
Release with UUID / GUID Version 1, Version 3, Version 4 and Version 5.</PackageReleaseNotes>
22+
Release with UUID / GUID Version 1, Version 3, Version 4 and Version 5, and Draft Versions 6 and 7.</PackageReleaseNotes>
2323
<NeutralLanguage>en-SG</NeutralLanguage>
24-
<AssemblyVersion>2.0.0.0</AssemblyVersion>
25-
<FileVersion>2.0.0.0</FileVersion>
24+
<AssemblyVersion>2.1.0.0</AssemblyVersion>
25+
<FileVersion>2.1.0.0</FileVersion>
2626
<IncludeSymbols>true</IncludeSymbols>
2727
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2828
<PackageReadmeFile>README.md</PackageReadmeFile>

UUIDUtil/UUIDv1.cs

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// Copyright 2021 TensionDev <[email protected]>
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// Copyright 2021 TensionDev <[email protected]>
24
//
35
// Licensed under the Apache License, Version 2.0 (the "License");
46
// you may not use this file except in compliance with the License.
@@ -23,10 +25,10 @@ public class UUIDv1
2325
{
2426
protected internal static System.Net.NetworkInformation.PhysicalAddress s_physicalAddress = System.Net.NetworkInformation.PhysicalAddress.None;
2527
protected internal static Int32 s_clock = Int32.MinValue;
26-
protected internal static DateTime s_epoch = new DateTime(1582, 10, 15, 0, 0, 0, DateTimeKind.Utc);
28+
protected internal static readonly DateTime s_epoch = new DateTime(1582, 10, 15, 0, 0, 0, DateTimeKind.Utc);
2729

28-
protected internal static Object s_initLock = new Object();
29-
protected internal static Object s_clockLock = new Object();
30+
protected internal static readonly Object s_initLock = new Object();
31+
protected internal static readonly Object s_clockLock = new Object();
3032

3133
/// <summary>
3234
/// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address)
@@ -37,68 +39,6 @@ public static Uuid NewUUIDv1()
3739
return NewUUIDv1(DateTime.UtcNow);
3840
}
3941

40-
/// <summary>
41-
/// Initialises the 48-bit Node ID and returns it.<br />
42-
/// Returns the MAC Address of a Network Interface Card, if available.
43-
/// Otherwise, returns a randomly genrated 48-bit Node ID.
44-
/// </summary>
45-
/// <returns>A byte-array representing the 48-bit Node ID</returns>
46-
public static Byte[] GetNodeID()
47-
{
48-
if (System.Net.NetworkInformation.PhysicalAddress.None.Equals(s_physicalAddress))
49-
{
50-
System.Net.NetworkInformation.NetworkInterface[] networkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
51-
if (networkInterfaces.Length > 0)
52-
{
53-
s_physicalAddress = networkInterfaces[0].GetPhysicalAddress();
54-
}
55-
else
56-
{
57-
using (System.Security.Cryptography.RNGCryptoServiceProvider cryptoServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider())
58-
{
59-
Byte[] fakeNode = new Byte[6];
60-
cryptoServiceProvider.GetBytes(fakeNode);
61-
fakeNode[0] = (Byte)(fakeNode[0] | 0x01);
62-
s_physicalAddress = new System.Net.NetworkInformation.PhysicalAddress(fakeNode);
63-
}
64-
}
65-
}
66-
67-
return s_physicalAddress.GetAddressBytes();
68-
}
69-
70-
/// <summary>
71-
/// Intialises the 14-bit Clock Sequence and returns the current value with the Variant.<br />
72-
/// Will return an incremented Clock Sequence on each call, modulo 14-bit.
73-
/// </summary>
74-
/// <returns>A byte-array representing the 14-bit Clock Sequence, together with the Variant</returns>
75-
public static Byte[] GetClockSequence()
76-
{
77-
lock (s_initLock)
78-
{
79-
if (s_clock < 0)
80-
{
81-
using (System.Security.Cryptography.RNGCryptoServiceProvider cryptoServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider())
82-
{
83-
Byte[] clockInit = new Byte[4];
84-
cryptoServiceProvider.GetBytes(clockInit);
85-
s_clock = BitConverter.ToInt32(clockInit, 0) & 0x3FFF;
86-
s_clock |= 0x8000;
87-
}
88-
}
89-
}
90-
91-
Int32 result;
92-
lock (s_clockLock)
93-
{
94-
result = s_clock++;
95-
if (s_clock >= 0xC000)
96-
s_clock = 0x8000;
97-
}
98-
99-
return BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((Int16)result));
100-
}
101-
10242
/// <summary>
10343
/// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address), based on the given date and time.
10444
/// </summary>
@@ -144,10 +84,10 @@ public static Uuid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nod
14484
if (nodeID.Length < 6)
14585
throw new ArgumentException(String.Format("Node ID contains less than 48-bit: {0} bytes", nodeID.Length), nameof(nodeID));
14686

147-
TimeSpan timesince = dateTime.ToUniversalTime() - s_epoch.ToUniversalTime();
148-
Int64 timeinterval = timesince.Ticks;
87+
TimeSpan timeSince = dateTime.ToUniversalTime() - s_epoch.ToUniversalTime();
88+
Int64 timeInterval = timeSince.Ticks;
14989

150-
Byte[] time = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(timeinterval));
90+
Byte[] time = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(timeInterval));
15191

15292
Byte[] hex = new Byte[16];
15393

@@ -176,5 +116,67 @@ public static Uuid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nod
176116

177117
return Id;
178118
}
119+
120+
/// <summary>
121+
/// Initialises the 48-bit Node ID and returns it.<br />
122+
/// Returns the MAC Address of a Network Interface Card, if available.
123+
/// Otherwise, returns a randomly genrated 48-bit Node ID.
124+
/// </summary>
125+
/// <returns>A byte-array representing the 48-bit Node ID</returns>
126+
public static Byte[] GetNodeID()
127+
{
128+
if (System.Net.NetworkInformation.PhysicalAddress.None.Equals(s_physicalAddress))
129+
{
130+
System.Net.NetworkInformation.NetworkInterface[] networkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
131+
if (networkInterfaces.Length > 0)
132+
{
133+
s_physicalAddress = networkInterfaces[0].GetPhysicalAddress();
134+
}
135+
else
136+
{
137+
using (System.Security.Cryptography.RNGCryptoServiceProvider cryptoServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider())
138+
{
139+
Byte[] fakeNode = new Byte[6];
140+
cryptoServiceProvider.GetBytes(fakeNode);
141+
fakeNode[0] = (Byte)(fakeNode[0] | 0x01);
142+
s_physicalAddress = new System.Net.NetworkInformation.PhysicalAddress(fakeNode);
143+
}
144+
}
145+
}
146+
147+
return s_physicalAddress.GetAddressBytes();
148+
}
149+
150+
/// <summary>
151+
/// Intialises the 14-bit Clock Sequence and returns the current value with the Variant.<br />
152+
/// Will return an incremented Clock Sequence on each call, modulo 14-bit.
153+
/// </summary>
154+
/// <returns>A byte-array representing the 14-bit Clock Sequence, together with the Variant</returns>
155+
public static Byte[] GetClockSequence()
156+
{
157+
lock (s_initLock)
158+
{
159+
if (s_clock < 0)
160+
{
161+
using (System.Security.Cryptography.RNGCryptoServiceProvider cryptoServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider())
162+
{
163+
Byte[] clockInit = new Byte[4];
164+
cryptoServiceProvider.GetBytes(clockInit);
165+
s_clock = BitConverter.ToInt32(clockInit, 0) & 0x3FFF;
166+
s_clock |= 0x8000;
167+
}
168+
}
169+
}
170+
171+
Int32 result;
172+
lock (s_clockLock)
173+
{
174+
result = s_clock++;
175+
if (s_clock >= 0xC000)
176+
s_clock = 0x8000;
177+
}
178+
179+
return BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder((Int16)result));
180+
}
179181
}
180182
}

UUIDUtil/UUIDv3.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// Copyright 2021 TensionDev <[email protected]>
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// Copyright 2021 TensionDev <[email protected]>
24
//
35
// Licensed under the Apache License, Version 2.0 (the "License");
46
// you may not use this file except in compliance with the License.

UUIDUtil/UUIDv4.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// Copyright 2021 TensionDev <[email protected]>
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// Copyright 2021 TensionDev <[email protected]>
24
//
35
// Licensed under the Apache License, Version 2.0 (the "License");
46
// you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)