Skip to content

Commit

Permalink
Implement ByteBuffer GetHashCode - v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminvanrenterghem committed Dec 8, 2024
1 parent 7304b94 commit 436ac08
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
26 changes: 26 additions & 0 deletions dotnet.Nyzo.CL.Tests/ByteBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ public void ComparingByteBufferWith_DifferentByteBufferInstance_ShouldYieldCorre
Assert.True(byteBufferDuo2.Equals(byteBufferDuo1));
}

[Fact]
public void GetHashCode_UsedAsSourceOfTruth_ShouldEnableComparison() {
// Large amount of bytes with a small difference
var array = new byte[int.MaxValue / 10];
var array1 = new byte[int.MaxValue / 10];

for(int i=0; i<array.Length; i++) {
var value = new Random().Next(0, 256);

array[i] = (byte)value;
array1[i] = (byte)value;

if(i == ((int.MaxValue / 10) / 2)) {
array1[i] = 1;
}
}

var buffer = new ByteBuffer(array);
var buffer1 = new ByteBuffer(array1);

var bufferHashCode = buffer.GetHashCode();
var buffer1HashCode = buffer1.GetHashCode();

Assert.NotEqual(bufferHashCode, buffer1HashCode);
}

[Fact]
public void PuttingByte_ShouldAllowForRetrieval() {
// Arrange
Expand Down
2 changes: 1 addition & 1 deletion dotnet.Nyzo.CL.Tests/Nyzo.CL.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageId>Nyzo.CL.Tests</PackageId>
<Version>1.1.0</Version>
<Version>1.3.0</Version>
<Authors>Benjamin Van Renterghem</Authors>
<Company>construct0</Company>
<PackageDescription>xUnit test project for the Nyzo class library (Nyzo.CL). The version indicates which Nyzo.CL version it is paired to.</PackageDescription>
Expand Down
4 changes: 3 additions & 1 deletion dotnet.Nyzo.CL/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public override bool Equals(object? obj) {
}

public override int GetHashCode() {
throw new NotSupportedException();
byte[] doubleSha256 = NyzoUtil.ByteArrayAsDoubleSha256ByteArray(this.Buffer);

return BitConverter.ToInt32(doubleSha256);
}

// Position
Expand Down
8 changes: 4 additions & 4 deletions dotnet.Nyzo.CL/Nyzo.CL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageId>Nyzo.CL</PackageId>
<Version>1.2.1</Version>
<Version>1.3.0</Version>
<Authors>Benjamin Van Renterghem</Authors>
<Company>construct0</Company>
<PackageDescription>Nyzo class library.</PackageDescription>
Expand All @@ -21,9 +21,9 @@
</ItemGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath=".\"/>
<None Include="LICENSE" Pack="true" PackagePath=".\"/>
<None Include="libsodium-core-master.zip.bak" Pack="true" PackagePath=".bak\"/>
<None Include="README.md" Pack="true" PackagePath=".\" />
<None Include="LICENSE" Pack="true" PackagePath=".\" />
<None Include="libsodium-core-master.zip.bak" Pack="true" PackagePath=".bak\" />
</ItemGroup>

</Project>

0 comments on commit 436ac08

Please sign in to comment.