Skip to content

Commit

Permalink
Create Uuid.cs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amsga committed Mar 26, 2022
1 parent aacfca3 commit 7ae8822
Show file tree
Hide file tree
Showing 17 changed files with 1,140 additions and 47 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
fail-fast: false
matrix:
language: [ 'csharp' ]
dotnet: [ '3.1.x' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# 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
Expand All @@ -53,8 +54,8 @@ jobs:

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

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -66,6 +67,14 @@ jobs:
#- run: |
# make bootstrap
# make release
- name: Setup .NET ${{ matrix.dotnet }}
uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ matrix.dotnet }}
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build ./UUIDUtil --no-restore

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
13 changes: 9 additions & 4 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ jobs:

runs-on: ubuntu-latest

strategy:
matrix:
dotnet: [ '3.1.x', '6.0.x' ]
name: .NET ${{ matrix.dotnet }}

steps:
- uses: actions/checkout@v2
- name: Setup .NET Core 3.1
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.x
dotnet-version: ${{ matrix.dotnet }}
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
run: dotnet build ./UUIDUtil --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
run: dotnet test ./UUIDUtil --no-build --verbosity normal
9 changes: 4 additions & 5 deletions .github/workflows/package-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
push:
tags:
- v[0-9]+.[0-9]+.[0-9]+
- v[0-9]+.[0-9]+.[0-9]+-alpha
- v[0-9]+.[0-9]+.[0-9]+-beta

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

- name: Setup .NET Core 3.1
- name: Setup .NET 6
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.x
dotnet-version: 6.0.x
source-url: https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
Expand All @@ -35,9 +37,6 @@ jobs:
- name: Build
run: dotnet build --no-restore

- name: Test
run: dotnet test --no-build --verbosity normal

- name: Build Release
run: dotnet build --configuration Release --no-restore

Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v1.0.0] - 2022-03-26
[v1.0.0](https://github.com/TensionDev/UUIDUtil/releases/tag/v1.0.0)

### Added
- Added TensionDev.UUID.Uuid as an implementation based on RFC 4122.
- Added UUID v3 generated based on MD5, System.Security.Cryptography.MD5.
- Added UUID v5 generated based on SHA-1, System.Security.Cryptography.SHA1.

### Removed
- Changed implementation from System.Guid to TensionDev.UUID.Uuid.


## [v0.2.0] - 2021-09-10
[v0.2.0](https://github.com/TensionDev/UUIDUtil/releases/tag/v0.2.0)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![.NET](https://github.com/TensionDev/UUIDUtil/actions/workflows/dotnet.yml/badge.svg)](https://github.com/TensionDev/UUIDUtil/actions/workflows/dotnet.yml)
[![Package Release](https://github.com/TensionDev/UUIDUtil/actions/workflows/package-release.yml/badge.svg)](https://github.com/TensionDev/UUIDUtil/actions/workflows/package-release.yml)
[![CodeQL](https://github.com/TensionDev/UUIDUtil/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/TensionDev/UUIDUtil/actions/workflows/codeql-analysis.yml)

A project to store UUID functions within a library for future use.
This project references the following documents for implementation.
Expand Down
25 changes: 25 additions & 0 deletions UUIDUtil/UUIDNamespace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace TensionDev.UUID
{
/// <summary>
/// Class Library to generate Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) based on Version 3 (MD5 namespace name-based).
/// </summary>
public class UUIDNamespace
{
/// <summary>
/// Namespace for Domain Name System
/// </summary>
public static Uuid DNS = new Uuid(0x6ba7b810, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
/// <summary>
/// Namespace for URLs
/// </summary>
public static Uuid URL = new Uuid(0x6ba7b811, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
/// <summary>
/// Namespace for ISO Object IDs (OIDs)
/// </summary>
public static Uuid OID = new Uuid(0x6ba7b812, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
/// <summary>
/// Namespace for X.500 Distinguished Names(DNs)
/// </summary>
public static Uuid X500 = new Uuid(0x6ba7b814, 0x9dad, 0x11d1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
}
}
10 changes: 5 additions & 5 deletions UUIDUtil/UUIDUtil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageId>TensionDev.UUID</PackageId>
<Version>0.2.0</Version>
<Version>1.0.0</Version>
<Authors>TensionDev amsga</Authors>
<Company>TensionDev</Company>
<Product>TensionDev.UUID</Product>
<Description>A project to store various UUID functions within a library for future use.</Description>
<Copyright>Copyright (c) TensionDev 2021</Copyright>
<Copyright>Copyright (c) TensionDev 2021 - 2022</Copyright>
<PackageLicenseExpression>GPL-3.0-or-later</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/TensionDev/UUIDUtil</PackageProjectUrl>
<RepositoryUrl>https://github.com/TensionDev/UUIDUtil</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>UUID GUID</PackageTags>
<PackageReleaseNotes>Release with UUID / GUID Version 1 and Version 4.</PackageReleaseNotes>
<PackageReleaseNotes>Release with UUID / GUID Version 1, Version 3, Version 4 and Version 5.</PackageReleaseNotes>
<NeutralLanguage>en-SG</NeutralLanguage>
<AssemblyVersion>0.2.0.0</AssemblyVersion>
<FileVersion>0.2.0.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
Expand Down
36 changes: 18 additions & 18 deletions UUIDUtil/UUIDv1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class UUIDv1
/// <summary>
/// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address)
/// </summary>
/// <returns>A new Guid object</returns>
public static Guid NewUUIDv1()
/// <returns>A new Uuid object</returns>
public static Uuid NewUUIDv1()
{
return NewUUIDv1(DateTime.UtcNow);
}
Expand Down Expand Up @@ -89,8 +89,8 @@ public static Byte[] GetClockSequence()
/// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address), based on the given date and time.
/// </summary>
/// <param name="dateTime">Given Date and Time</param>
/// <returns>A new Guid object</returns>
public static Guid NewUUIDv1(DateTime dateTime)
/// <returns>A new Uuid object</returns>
public static Uuid NewUUIDv1(DateTime dateTime)
{
return NewUUIDv1(dateTime, GetClockSequence(), GetNodeID());
}
Expand All @@ -99,10 +99,10 @@ public static Guid NewUUIDv1(DateTime dateTime)
/// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address), based on the given Node ID.
/// </summary>
/// <param name="nodeID">Given 48-bit Node ID</param>
/// <returns>A new Guid object</returns>
/// <returns>A new Uuid object</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static Guid NewUUIDv1(Byte[] nodeID)
public static Uuid NewUUIDv1(Byte[] nodeID)
{
return NewUUIDv1(DateTime.UtcNow, GetClockSequence(), nodeID);
}
Expand All @@ -113,10 +113,10 @@ public static Guid NewUUIDv1(Byte[] nodeID)
/// <param name="dateTime">Given Date and Time</param>
/// <param name="clockSequence">Given 16-bit Clock Sequence with Variant</param>
/// <param name="nodeID">Given 48-bit Node ID</param>
/// <returns>A new Guid object</returns>
/// <returns>A new Uuid object</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static Guid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nodeID)
public static Uuid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nodeID)
{
if (clockSequence == null)
throw new ArgumentNullException(nameof(clockSequence));
Expand All @@ -133,20 +133,20 @@ public static Guid NewUUIDv1(DateTime dateTime, Byte[] clockSequence, Byte[] nod
TimeSpan timesince = dateTime.ToUniversalTime() - s_epoch.ToUniversalTime();
Int64 timeinterval = timesince.Ticks;

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

Byte[] hex = new Byte[16];

hex[0] = time[0];
hex[1] = time[1];
hex[2] = time[2];
hex[3] = time[3];
hex[0] = time[4];
hex[1] = time[5];
hex[2] = time[6];
hex[3] = time[7];

hex[4] = time[4];
hex[5] = time[5];
hex[4] = time[2];
hex[5] = time[3];

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

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

Guid Id = new Guid(hex);
Uuid Id = new Uuid(hex);

return Id;
}
Expand Down
59 changes: 59 additions & 0 deletions UUIDUtil/UUIDv3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Security.Cryptography;
using System.Text;

namespace TensionDev.UUID
{
/// <summary>
/// Class Library to generate Universally Unique Identifier (UUID) / Globally Unique Identifier (GUID) based on Version 3 (MD5 namespace name-based).
/// </summary>
public class UUIDv3
{
/// <summary>
/// Initialises a new GUID/UUID based on Version 3 (MD5 namespace name-based)
/// </summary>
/// <returns>A new Uuid object</returns>
public static Uuid NewUUIDv3(Uuid nameSpace, String name)
{
Byte[] nsArray = nameSpace.ToByteArray();
Byte[] nArray = Encoding.UTF8.GetBytes(name);

Byte[] buffer = new Byte[nsArray.Length + nArray.Length];
Buffer.BlockCopy(nsArray, 0, buffer, 0, nsArray.Length);
Buffer.BlockCopy(nArray, 0, buffer, nsArray.Length, nArray.Length);

Byte[] hash;
using (MD5 md5 = MD5.Create())
{
hash = md5.ComputeHash(buffer);
}

Byte[] hex = new Byte[16];

hex[0] = hash[0];
hex[1] = hash[1];
hex[2] = hash[2];
hex[3] = hash[3];

hex[4] = hash[4];
hex[5] = hash[5];

hex[6] = (Byte)((hash[6] & 0x0F) + 0x30);
hex[7] = hash[7];

hex[8] = (Byte)((hash[8] & 0x3F) + 0x80);
hex[9] = hash[9];

hex[10] = hash[10];
hex[11] = hash[11];
hex[12] = hash[12];
hex[13] = hash[13];
hex[14] = hash[14];
hex[15] = hash[15];

Uuid Id = new Uuid(hex);

return Id;
}
}
}
22 changes: 11 additions & 11 deletions UUIDUtil/UUIDv4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class UUIDv4
/// <summary>
/// Initialises a new GUID/UUID based on Version 4 (random)
/// </summary>
/// <returns>A new Guid object</returns>
public static Guid NewUUIDv4()
/// <returns>A new Uuid object</returns>
public static Uuid NewUUIDv4()
{
Byte[] time = new Byte[8];
Byte[] clockSequence = new Byte[2];
Expand All @@ -26,16 +26,16 @@ public static Guid NewUUIDv4()

Byte[] hex = new Byte[16];

hex[0] = time[0];
hex[1] = time[1];
hex[2] = time[2];
hex[3] = time[3];
hex[0] = time[4];
hex[1] = time[5];
hex[2] = time[6];
hex[3] = time[7];

hex[4] = time[4];
hex[5] = time[5];
hex[4] = time[2];
hex[5] = time[3];

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

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

Guid Id = new Guid(hex);
Uuid Id = new Uuid(hex);

return Id;
}
Expand Down
Loading

0 comments on commit 7ae8822

Please sign in to comment.