Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 83 additions & 11 deletions src/DevToys.Tools/Helpers/UuidHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Cryptography;
using System.Text;
using DevToys.Tools.Models;

namespace DevToys.Tools.Helpers;
Expand All @@ -12,7 +13,8 @@ private enum GuidVersion
Random = 0x04,
NameBasedV5 = 0x05,
UuidV6 = 0x06,
UnixEpoch = 0x07
UnixEpoch = 0x07,
UuidObjectId = 0x08
}

private static readonly Random random = new();
Expand All @@ -30,6 +32,9 @@ private enum GuidVersion
private const byte TimestampByte = 0;
private const byte NodeByte = 10;
private const byte GuidClockSequenceByte = 8;
private static readonly object incrementLock = new();
private static int increment = new Random().Next();
private static readonly byte[] ProcessUnique = GenerateProcessUnique();

internal static string GenerateUuid(UuidVersion version, bool hyphens, bool uppercase)
{
Expand All @@ -44,17 +49,20 @@ internal static string GenerateUuid(UuidVersion version, bool hyphens, bool uppe
}

string? uuid;
if (version == UuidVersion.One)
switch (version)
{
uuid = GenerateTimeBasedGuid().ToString(guidStringFormat);
}
else if (version == UuidVersion.Seven)
{
uuid = GenerateUUIDv7().ToString(guidStringFormat);
}
else
{
uuid = Guid.NewGuid().ToString(guidStringFormat);
case UuidVersion.One:
uuid = GenerateTimeBasedGuid().ToString(guidStringFormat);
break;
case UuidVersion.Seven:
uuid = GenerateUUIDv7().ToString(guidStringFormat);
break;
case UuidVersion.ObjectId:
uuid = GenerateUuidObjectId();
break;
default:
uuid = Guid.NewGuid().ToString(guidStringFormat);
break;
}

if (uppercase)
Expand Down Expand Up @@ -158,4 +166,68 @@ public static byte[] GenerateClockSequenceBytes(DateTime dt)

return [bytes[0], bytes[1]];
}

/// <summary>
/// Generate a Mongodb ObjectID
/// </summary>
/// <returns></returns>
private static string GenerateUuidObjectId()
{
int time = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
int inc = GetIncrement();
byte[] buffer = new byte[12];

// 4-byte timestamp
buffer[0] = (byte)(time >> 24);
buffer[1] = (byte)(time >> 16);
buffer[2] = (byte)(time >> 8);
buffer[3] = (byte)time;

// 5-byte process unique
Buffer.BlockCopy(ProcessUnique, 0, buffer, 4, 5);

// 3-byte counter
buffer[9] = (byte)(inc >> 16);
buffer[10] = (byte)(inc >> 8);
buffer[11] = (byte)inc;
return ByteArrayToHexString(buffer);
}

/// <summary>
/// Retrieves and increments the internal counter in a thread-safe manner.
/// </summary>
/// <returns>Returns the current value of the counter</returns>
private static int GetIncrement()
{
lock (incrementLock)
{
return increment++;
}
}

/// <summary>
/// Generates a unique identifier specific to the current process.
/// </summary>
/// <returns>Returns a byte array containing the unique identifier for the process.</returns>
private static byte[] GenerateProcessUnique()
{
byte[] processUnique = new byte[5];
cRandom.GetBytes(processUnique);
return processUnique;
}

/// <summary>
/// Converts a byte array to its hexadecimal string representation.
/// </summary>
/// <param name="bytes">The byte array to be converted to a hexadecimal string.</param>
/// <returns>Returns a string containing the hexadecimal representation of the byte array.</returns>
private static string ByteArrayToHexString(byte[] bytes)
{
var sb = new StringBuilder(bytes.Length * 2);
foreach (byte b in bytes)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
}
3 changes: 2 additions & 1 deletion src/DevToys.Tools/Models/UuidVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ internal enum UuidVersion
{
One,
Four,
Seven
Seven,
ObjectId
}
11 changes: 11 additions & 0 deletions src/DevToys.Tools/Tools/Generators/UUID/UUIDGenerator.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Actualitza</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Obnovit</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Opdatér</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Aktualisieren</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Actualizar</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Rafraîchir</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Perbarui</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>再生成</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/DevToys.Tools/Tools/Generators/UUID/UUIDGenerator.nb.resx
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="Refresh" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="UuidObjectId" xml:space="preserve">
<value>ObjectId (MongoDB)</value>
</data>
</root>
Loading