Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the way bytes are handled for Guid #8

Merged
merged 1 commit into from
Nov 8, 2020
Merged
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<VlingoVersion>0.9.0</VlingoVersion>
<VlingoVersion>0.9.1</VlingoVersion>
</PropertyGroup>
</Project>
18 changes: 16 additions & 2 deletions src/Vlingo.UUID.Tests/NameBasedGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public void GeneratedUUID_ShouldHaveProperVersion(HashType hashType)
{
var guid = generator.GenerateGuid(nameSpace, name);

var array = guid.ToByteArray();
var array = guid.ToActuallyOrderedBytes();

Assert.Equal(expectedVersion, array[7] & 0xf0);
Assert.Equal(expectedVersion, array[6] & 0xf0);
}
}

Expand Down Expand Up @@ -120,5 +120,19 @@ public void UUIDGenerated_InWithSameName_InDifferentCustomNameSpace_ShouldBeDiff
Assert.NotEqual(first, second);
}
}

[Fact]
public void UUIDGenerated_ShouldGenerateProperNamebasedGuid_ForCustomNamespaceAndName()
{
// bugfix for issue: https://github.com/vlingo-net/vlingo-net-uuid/issues/7

var uuidNamespace = Guid.Parse("a4405a8d-8bb2-467a-bbc3-961ab93bb538");
var name = "9912310000";

var generator = new NameBasedGenerator(HashType.Sha1);
var uuidV5 = generator.GenerateGuid(uuidNamespace, name);

Assert.Equal("a045c4bc-d81c-5fc4-88bd-313db5b2d1fc", uuidV5.ToString());
}
}
}
4 changes: 2 additions & 2 deletions src/Vlingo.UUID.Tests/RandomBasedGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public void GeneratedUUID_ShouldHaveProperVersion()
var expectedVersion = 0x40;

var guid = generator.GenerateGuid();
var array = guid.ToByteArray();
var array = guid.ToActuallyOrderedBytes();

Assert.Equal(expectedVersion, array[7] & 0xf0);
Assert.Equal(expectedVersion, array[6] & 0xf0);
}
}
}
4 changes: 2 additions & 2 deletions src/Vlingo.UUID.Tests/TimeBasedGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public void GeneratedUUID_ShouldHaveProperVersion(GuidGenerationMode mode)
var expectedVersion = 0x10;

var guid = generator.GenerateGuid(mode);
var array = guid.ToByteArray();
var array = guid.ToActuallyOrderedBytes();

Assert.Equal(expectedVersion, array[7] & 0xf0);
Assert.Equal(expectedVersion, array[6] & 0xf0);
}
}
}
2 changes: 1 addition & 1 deletion src/Vlingo.UUID/ByteMarker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal static class ByteMarker
private const int VariantIndexPosition = 8;
private const int VariantMask = 0x3f;
private const int VariantBits = 0x80;
private const int VersionIndexPosition = 7;
private const int VersionIndexPosition = 6;
private const int VersionMask = 0x0f;

/// <summary>
Expand Down
61 changes: 48 additions & 13 deletions src/Vlingo.UUID/GuidExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,58 @@ namespace Vlingo.UUID
{
public static class GuidExtensions
{
public static long ToLeastSignificantBits(this Guid id)
/// <summary>
/// Converts the Guid into the byte order as they appear in Guid string
/// </summary>
/// <remarks>
/// Reference: https://docs.microsoft.com/en-us/dotnet/api/system.guid.tobytearray?view=netcore-3.1#remarks
/// </remarks>
/// <param name="guid"></param>
/// <returns>Array of bytes</returns>
public static byte[] ToActuallyOrderedBytes(this Guid guid)
{
var bytes = id.ToByteArray();
var boolArray = new bool[bytes.Length];
for(var i = 0; i < bytes.Length; i++)
{
boolArray[i] = GetBit(bytes[i]);
}
var array = guid.ToByteArray();
ChangeGuidByteOrders(array);
return array;
}

return BitConverter.ToInt64(bytes, 0);
/// <summary>
/// Converts the byte array so that it appears in the Guid string in the same order
/// Caution: mutates the actual array to save memory
/// </summary>
/// <remarks>
/// Reference: https://docs.microsoft.com/en-us/dotnet/api/system.guid.tobytearray?view=netcore-3.1#remarks
/// </remarks>
/// <param name="array"></param>
/// <returns>A Guid</returns>
public static Guid ToGuidFromActuallyOrderedBytes(this byte[] array)
{
ChangeGuidByteOrders(array);
return new Guid(array);
}

public static Guid ToGuid(this long id)

/// <summary>
/// Swaps bytes in positions as:
/// 0 <-> 3, 1 <-> 2, 4 <-> 5, 6 <-> 7
/// </summary>
/// <param name="array"></param>
private static void ChangeGuidByteOrders(byte[] array)
{
var bytes = new byte[16];
BitConverter.GetBytes(id).CopyTo(bytes, 0);
return new Guid(bytes);
var temp = array[0];
array[0] = array[3];
array[3] = temp;

temp = array[1];
array[1] = array[2];
array[2] = temp;

temp = array[4];
array[4] = array[5];
array[5] = temp;

temp = array[6];
array[6] = array[7];
array[7] = temp;
}

private static bool GetBit(byte b) => (b & 1) != 0;
Expand Down
4 changes: 2 additions & 2 deletions src/Vlingo.UUID/NameBasedGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public NameBasedGenerator()
/// <returns>RFC4122 UUID generated using the <paramref name="customNamespaceGuid"/> and the <paramref name="name"/></returns>
public Guid GenerateGuid(Guid customNamespaceGuid, string name)
{
var nsBytes = Guid.Empty == customNamespaceGuid ? new byte[0] : customNamespaceGuid.ToByteArray();
var nsBytes = Guid.Empty == customNamespaceGuid ? new byte[0] : customNamespaceGuid.ToActuallyOrderedBytes();
var nameBytes = Encoding.UTF8.GetBytes(name);
var data = new byte[nsBytes.Length + nameBytes.Length];
if(nsBytes.Length > 0)
Expand All @@ -86,7 +86,7 @@ public Guid GenerateGuid(Guid customNamespaceGuid, string name)
.AddVariantMarker()
.AddVersionMarker(_version);

return new Guid(result);
return result.ToGuidFromActuallyOrderedBytes();
}

public void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion src/Vlingo.UUID/RandomBasedGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Guid GenerateGuid()

data.AddVariantMarker().AddVersionMarker(UUIDVersion.Random);

return new Guid(data);
return data.ToGuidFromActuallyOrderedBytes();
}
}
}
2 changes: 1 addition & 1 deletion src/Vlingo.UUID/TimeBasedGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static Guid GenerateGuid(DateTimeOffset dateTime, byte[] clockSequenceDa

data.AddVariantMarker().AddVersionMarker(UUIDVersion.TimeBased);

return new Guid(data);
return data.ToGuidFromActuallyOrderedBytes();
}

private static byte[] GetClockSequenceData(long ticks)
Expand Down