Skip to content
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
34 changes: 34 additions & 0 deletions src/Blake3.Tests/Blake3StreamTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Blake3.Tests
Expand Down Expand Up @@ -56,5 +58,37 @@ public void TestHashWrite()
blake3Stream.Write(HasherTests.BigData);
AssertTextAreEqual(HasherTests.BigExpected, blake3Stream.ComputeHash().ToString());
}

[Test]
public void TestHashReadSpan_PartialRead()
{
var data = HasherTests.SimpleData;
var expected = HasherTests.SimpleExpected;

var stream = new MemoryStream(data);
using var blake3Stream = new Blake3Stream(stream);

var oversizedBuffer = new byte[data.Length + 1024];
var bytesRead = blake3Stream.Read(oversizedBuffer.AsSpan());

Assert.AreEqual(data.Length, bytesRead);
AssertTextAreEqual(expected, blake3Stream.ComputeHash().ToString());
}

[Test]
public async Task TestHashReadAsyncMemory_PartialRead()
{
var data = HasherTests.SimpleData;
var expected = HasherTests.SimpleExpected;

var stream = new MemoryStream(data);
await using var blake3Stream = new Blake3Stream(stream);

var oversizedBuffer = new byte[data.Length + 1024];
var bytesRead = await blake3Stream.ReadAsync(oversizedBuffer.AsMemory());

Assert.AreEqual(data.Length, bytesRead);
AssertTextAreEqual(expected, blake3Stream.ComputeHash().ToString());
}
}
}
20 changes: 20 additions & 0 deletions src/Blake3.Tests/HasherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,25 @@ public void TestFinalizeWithOffset()

Assert.True(bigHash.SequenceEqual(reconstructedHash.ToArray()));
}

[Test]
public void TestUpdateWithJoinEmptySpan()
{
using var hasher = Hasher.New();
hasher.UpdateWithJoin(ReadOnlySpan<byte>.Empty);
var hash = hasher.Finalize();
using var hasher2 = Hasher.New();
var expected = hasher2.Finalize();
Assert.AreEqual(expected, hash);
}

[Test]
public void TestFinalizeWithNegativeOffset()
{
using var hasher = Hasher.New();
hasher.Update(SimpleData);
var output = new byte[32];
Assert.Throws<ArgumentOutOfRangeException>(() => hasher.Finalize(-1L, output));
}
}
}
4 changes: 2 additions & 2 deletions src/Blake3/Blake3Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
var length = await _stream.ReadAsync(buffer, cancellationToken);
if (length > 0)
{
_hasher.Update(buffer.Span);
_hasher.Update(buffer.Span.Slice(0, length));
}
return length;
}
Expand All @@ -111,7 +111,7 @@ public override int Read(Span<byte> buffer)
var length = _stream.Read(buffer);
if (length > 0)
{
_hasher.Update(buffer);
_hasher.Update(buffer.Slice(0, length));
}
return length;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Blake3/Hash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace Blake3;

Expand Down Expand Up @@ -85,7 +86,7 @@ public static Hash FromBytes(ReadOnlySpan<byte> data)

public bool Equals(Hash other)
{
return this.AsSpan().SequenceCompareTo(other.AsSpan()) == 0;
return CryptographicOperations.FixedTimeEquals(AsSpan(), other.AsSpan());
}

public override bool Equals(object obj)
Expand Down
2 changes: 1 addition & 1 deletion src/Blake3/Hasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ public void Update<T>(ReadOnlySpan<T> data) where T : unmanaged
/// </remarks>
public void UpdateWithJoin(ReadOnlySpan<byte> data)
{
if (data == null) ThrowArgumentNullException();
if (_hasher == null) ThrowNullReferenceException();
fixed (void* ptr = data)
{
Expand Down Expand Up @@ -283,6 +282,7 @@ public void Finalize(ulong offset, Span<byte> hash)
/// </remarks>
public void Finalize(long offset, Span<byte> hash)
{
ArgumentOutOfRangeException.ThrowIfLessThan(offset, 0);
Finalize((ulong)offset, hash);
}

Expand Down