Skip to content

Commit 7ae8822

Browse files
committed
Create Uuid.cs
Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) implementation based on RFC 4122 This will replaced all reference to System.Guid. Create UUIDv3.cs Class Library to generate Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) based on Version 3 (MD5 Namespace name-based). Relying on System.Security.Cryptography.MD5 for the hashing algorithm. Create UUIDv5.cs Class Library to generate Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) based on Version 5 (SHA-1 Namespace name-based). Relying on System.Security.Cryptography.SHA1 for the hashing algorithm.
1 parent aacfca3 commit 7ae8822

17 files changed

+1140
-47
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
fail-fast: false
3434
matrix:
3535
language: [ 'csharp' ]
36+
dotnet: [ '3.1.x' ]
3637
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
3738
# Learn more:
3839
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
@@ -53,8 +54,8 @@ jobs:
5354

5455
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
5556
# If this step fails, then you should remove it and run the build manually (see below)
56-
- name: Autobuild
57-
uses: github/codeql-action/autobuild@v1
57+
# - name: Autobuild
58+
# uses: github/codeql-action/autobuild@v1
5859

5960
# ℹ️ Command-line programs to run using the OS shell.
6061
# 📚 https://git.io/JvXDl
@@ -66,6 +67,14 @@ jobs:
6667
#- run: |
6768
# make bootstrap
6869
# make release
70+
- name: Setup .NET ${{ matrix.dotnet }}
71+
uses: actions/setup-dotnet@v1
72+
with:
73+
dotnet-version: ${{ matrix.dotnet }}
74+
- name: Restore dependencies
75+
run: dotnet restore
76+
- name: Build
77+
run: dotnet build ./UUIDUtil --no-restore
6978

7079
- name: Perform CodeQL Analysis
7180
uses: github/codeql-action/analyze@v1

.github/workflows/dotnet.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ jobs:
1111

1212
runs-on: ubuntu-latest
1313

14+
strategy:
15+
matrix:
16+
dotnet: [ '3.1.x', '6.0.x' ]
17+
name: .NET ${{ matrix.dotnet }}
18+
1419
steps:
1520
- uses: actions/checkout@v2
16-
- name: Setup .NET Core 3.1
21+
- name: Setup .NET
1722
uses: actions/setup-dotnet@v1
1823
with:
19-
dotnet-version: 3.1.x
24+
dotnet-version: ${{ matrix.dotnet }}
2025
- name: Restore dependencies
2126
run: dotnet restore
2227
- name: Build
23-
run: dotnet build --no-restore
28+
run: dotnet build ./UUIDUtil --no-restore
2429
- name: Test
25-
run: dotnet test --no-build --verbosity normal
30+
run: dotnet test ./UUIDUtil --no-build --verbosity normal

.github/workflows/package-release.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ on:
88
push:
99
tags:
1010
- v[0-9]+.[0-9]+.[0-9]+
11+
- v[0-9]+.[0-9]+.[0-9]+-alpha
12+
- v[0-9]+.[0-9]+.[0-9]+-beta
1113

1214
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
1315
jobs:
@@ -21,10 +23,10 @@ jobs:
2123
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
2224
- uses: actions/checkout@v2
2325

24-
- name: Setup .NET Core 3.1
26+
- name: Setup .NET 6
2527
uses: actions/setup-dotnet@v1
2628
with:
27-
dotnet-version: 3.1.x
29+
dotnet-version: 6.0.x
2830
source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
2931
env:
3032
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
@@ -35,9 +37,6 @@ jobs:
3537
- name: Build
3638
run: dotnet build --no-restore
3739

38-
- name: Test
39-
run: dotnet test --no-build --verbosity normal
40-
4140
- name: Build Release
4241
run: dotnet build --configuration Release --no-restore
4342

CHANGELOG.md

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

99
## [Unreleased]
1010

11+
## [v1.0.0] - 2022-03-26
12+
[v1.0.0](https://github.com/TensionDev/UUIDUtil/releases/tag/v1.0.0)
13+
14+
### Added
15+
- Added TensionDev.UUID.Uuid as an implementation based on RFC 4122.
16+
- Added UUID v3 generated based on MD5, System.Security.Cryptography.MD5.
17+
- Added UUID v5 generated based on SHA-1, System.Security.Cryptography.SHA1.
18+
19+
### Removed
20+
- Changed implementation from System.Guid to TensionDev.UUID.Uuid.
21+
22+
1123
## [v0.2.0] - 2021-09-10
1224
[v0.2.0](https://github.com/TensionDev/UUIDUtil/releases/tag/v0.2.0)
1325

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![.NET](https://github.com/TensionDev/UUIDUtil/actions/workflows/dotnet.yml/badge.svg)](https://github.com/TensionDev/UUIDUtil/actions/workflows/dotnet.yml)
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)
5+
[![CodeQL](https://github.com/TensionDev/UUIDUtil/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/TensionDev/UUIDUtil/actions/workflows/codeql-analysis.yml)
56

67
A project to store UUID functions within a library for future use.
78
This project references the following documents for implementation.

UUIDUtil/UUIDNamespace.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace TensionDev.UUID
2+
{
3+
/// <summary>
4+
/// Class Library to generate Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) based on Version 3 (MD5 namespace name-based).
5+
/// </summary>
6+
public class UUIDNamespace
7+
{
8+
/// <summary>
9+
/// Namespace for Domain Name System
10+
/// </summary>
11+
public static Uuid DNS = new Uuid(0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
12+
/// <summary>
13+
/// Namespace for URLs
14+
/// </summary>
15+
public static Uuid URL = new Uuid(0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
16+
/// <summary>
17+
/// Namespace for ISO Object IDs (OIDs)
18+
/// </summary>
19+
public static Uuid OID = new Uuid(0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
20+
/// <summary>
21+
/// Namespace for X.500 Distinguished Names(DNs)
22+
/// </summary>
23+
public static Uuid X500 = new Uuid(0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
24+
}
25+
}

UUIDUtil/UUIDUtil.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
99
<PackageId>TensionDev.UUID</PackageId>
10-
<Version>0.2.0</Version>
10+
<Version>1.0.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</Copyright>
15+
<Copyright>Copyright (c) TensionDev 2021 - 2022</Copyright>
1616
<PackageLicenseExpression>GPL-3.0-or-later</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>
21-
<PackageReleaseNotes>Release with UUID / GUID Version 1 and Version 4.</PackageReleaseNotes>
21+
<PackageReleaseNotes>Release with UUID / GUID Version 1, Version 3, Version 4 and Version 5.</PackageReleaseNotes>
2222
<NeutralLanguage>en-SG</NeutralLanguage>
23-
<AssemblyVersion>0.2.0.0</AssemblyVersion>
24-
<FileVersion>0.2.0.0</FileVersion>
23+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
24+
<FileVersion>1.0.0.0</FileVersion>
2525
<IncludeSymbols>true</IncludeSymbols>
2626
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2727
</PropertyGroup>

UUIDUtil/UUIDv1.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public class UUIDv1
1717
/// <summary>
1818
/// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address)
1919
/// </summary>
20-
/// <returns>A new Guid object</returns>
21-
public static Guid NewUUIDv1()
20+
/// <returns>A new Uuid object</returns>
21+
public static Uuid NewUUIDv1()
2222
{
2323
return NewUUIDv1(DateTime.UtcNow);
2424
}
@@ -89,8 +89,8 @@ public static Byte[] GetClockSequence()
8989
/// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address), based on the given date and time.
9090
/// </summary>
9191
/// <param name="dateTime">Given Date and Time</param>
92-
/// <returns>A new Guid object</returns>
93-
public static Guid NewUUIDv1(DateTime dateTime)
92+
/// <returns>A new Uuid object</returns>
93+
public static Uuid NewUUIDv1(DateTime dateTime)
9494
{
9595
return NewUUIDv1(dateTime, GetClockSequence(), GetNodeID());
9696
}
@@ -99,10 +99,10 @@ public static Guid NewUUIDv1(DateTime dateTime)
9999
/// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address), based on the given Node ID.
100100
/// </summary>
101101
/// <param name="nodeID">Given 48-bit Node ID</param>
102-
/// <returns>A new Guid object</returns>
102+
/// <returns>A new Uuid object</returns>
103103
/// <exception cref="ArgumentNullException"></exception>
104104
/// <exception cref="ArgumentException"></exception>
105-
public static Guid NewUUIDv1(Byte[] nodeID)
105+
public static Uuid NewUUIDv1(Byte[] nodeID)
106106
{
107107
return NewUUIDv1(DateTime.UtcNow, GetClockSequence(), nodeID);
108108
}
@@ -113,10 +113,10 @@ public static Guid NewUUIDv1(Byte[] nodeID)
113113
/// <param name="dateTime">Given Date and Time</param>
114114
/// <param name="clockSequence">Given 16-bit Clock Sequence with Variant</param>
115115
/// <param name="nodeID">Given 48-bit Node ID</param>
116-
/// <returns>A new Guid object</returns>
116+
/// <returns>A new Uuid object</returns>
117117
/// <exception cref="ArgumentNullException"></exception>
118118
/// <exception cref="ArgumentException"></exception>
119-
public static Guid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nodeID)
119+
public static Uuid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nodeID)
120120
{
121121
if (clockSequence == null)
122122
throw new ArgumentNullException(nameof(clockSequence));
@@ -133,20 +133,20 @@ public static Guid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nod
133133
TimeSpan timesince = dateTime.ToUniversalTime() - s_epoch.ToUniversalTime();
134134
Int64 timeinterval = timesince.Ticks;
135135

136-
Byte[] time = BitConverter.GetBytes(timeinterval);
136+
Byte[] time = BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(timeinterval));
137137

138138
Byte[] hex = new Byte[16];
139139

140-
hex[0] = time[0];
141-
hex[1] = time[1];
142-
hex[2] = time[2];
143-
hex[3] = time[3];
140+
hex[0] = time[4];
141+
hex[1] = time[5];
142+
hex[2] = time[6];
143+
hex[3] = time[7];
144144

145-
hex[4] = time[4];
146-
hex[5] = time[5];
145+
hex[4] = time[2];
146+
hex[5] = time[3];
147147

148-
hex[6] = time[6];
149-
hex[7] = (Byte)((time[7] & 0x0F) + 0x10);
148+
hex[6] = (Byte)((time[0] & 0x0F) + 0x10);
149+
hex[7] = time[1];
150150

151151
hex[8] = clockSequence[0];
152152
hex[9] = clockSequence[1];
@@ -158,7 +158,7 @@ public static Guid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nod
158158
hex[14] = nodeID[4];
159159
hex[15] = nodeID[5];
160160

161-
Guid Id = new Guid(hex);
161+
Uuid Id = new Uuid(hex);
162162

163163
return Id;
164164
}

UUIDUtil/UUIDv3.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Security.Cryptography;
3+
using System.Text;
4+
5+
namespace TensionDev.UUID
6+
{
7+
/// <summary>
8+
/// Class Library to generate Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) based on Version 3 (MD5 namespace name-based).
9+
/// </summary>
10+
public class UUIDv3
11+
{
12+
/// <summary>
13+
/// Initialises a new GUID/UUID based on Version 3 (MD5 namespace name-based)
14+
/// </summary>
15+
/// <returns>A new Uuid object</returns>
16+
public static Uuid NewUUIDv3(Uuid nameSpace, String name)
17+
{
18+
Byte[] nsArray = nameSpace.ToByteArray();
19+
Byte[] nArray = Encoding.UTF8.GetBytes(name);
20+
21+
Byte[] buffer = new Byte[nsArray.Length + nArray.Length];
22+
Buffer.BlockCopy(nsArray, 0, buffer, 0, nsArray.Length);
23+
Buffer.BlockCopy(nArray, 0, buffer, nsArray.Length, nArray.Length);
24+
25+
Byte[] hash;
26+
using (MD5 md5 = MD5.Create())
27+
{
28+
hash = md5.ComputeHash(buffer);
29+
}
30+
31+
Byte[] hex = new Byte[16];
32+
33+
hex[0] = hash[0];
34+
hex[1] = hash[1];
35+
hex[2] = hash[2];
36+
hex[3] = hash[3];
37+
38+
hex[4] = hash[4];
39+
hex[5] = hash[5];
40+
41+
hex[6] = (Byte)((hash[6] & 0x0F) + 0x30);
42+
hex[7] = hash[7];
43+
44+
hex[8] = (Byte)((hash[8] & 0x3F) + 0x80);
45+
hex[9] = hash[9];
46+
47+
hex[10] = hash[10];
48+
hex[11] = hash[11];
49+
hex[12] = hash[12];
50+
hex[13] = hash[13];
51+
hex[14] = hash[14];
52+
hex[15] = hash[15];
53+
54+
Uuid Id = new Uuid(hex);
55+
56+
return Id;
57+
}
58+
}
59+
}

UUIDUtil/UUIDv4.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public class UUIDv4
1010
/// <summary>
1111
/// Initialises a new GUID/UUID based on Version 4 (random)
1212
/// </summary>
13-
/// <returns>A new Guid object</returns>
14-
public static Guid NewUUIDv4()
13+
/// <returns>A new Uuid object</returns>
14+
public static Uuid NewUUIDv4()
1515
{
1616
Byte[] time = new Byte[8];
1717
Byte[] clockSequence = new Byte[2];
@@ -26,16 +26,16 @@ public static Guid NewUUIDv4()
2626

2727
Byte[] hex = new Byte[16];
2828

29-
hex[0] = time[0];
30-
hex[1] = time[1];
31-
hex[2] = time[2];
32-
hex[3] = time[3];
29+
hex[0] = time[4];
30+
hex[1] = time[5];
31+
hex[2] = time[6];
32+
hex[3] = time[7];
3333

34-
hex[4] = time[4];
35-
hex[5] = time[5];
34+
hex[4] = time[2];
35+
hex[5] = time[3];
3636

37-
hex[6] = time[6];
38-
hex[7] = (Byte)((time[7] & 0x0F) + 0x40);
37+
hex[6] = (Byte)((time[0] & 0x0F) + 0x40);
38+
hex[7] = time[1];
3939

4040
hex[8] = (Byte)((clockSequence[0] & 0x3F) + 0x80);
4141
hex[9] = clockSequence[1];
@@ -47,7 +47,7 @@ public static Guid NewUUIDv4()
4747
hex[14] = nodeID[4];
4848
hex[15] = nodeID[5];
4949

50-
Guid Id = new Guid(hex);
50+
Uuid Id = new Uuid(hex);
5151

5252
return Id;
5353
}

0 commit comments

Comments
 (0)