Skip to content

Commit

Permalink
BLAKE2b.cs: Check whether the stream can be read.
Browse files Browse the repository at this point in the history
stream.Read() throws NotSupportedException, but this is more consistent with the rest of the library and prevents allocations.
  • Loading branch information
samuel-lucas6 committed Nov 14, 2024
1 parent 3e8a481 commit 0ab9993
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Geralt.Tests/BLAKE2bTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ public void ComputeHash_Stream_Invalid(int hashSize, int messageSize)
if (messageSize > 0) {
using var m = new MemoryStream(new byte[messageSize], writable: false);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => BLAKE2b.ComputeHash(h, m));

h = new byte[BLAKE2b.HashSize];
m.Close();
Assert.ThrowsException<InvalidOperationException>(() => BLAKE2b.ComputeHash(h, m));
}
else {
using MemoryStream? m = null;
Expand Down
1 change: 1 addition & 0 deletions src/Geralt/Crypto/BLAKE2b.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static void ComputeHash(Span<byte> hash, Stream message)
{
Validation.SizeBetween(nameof(hash), hash.Length, MinHashSize, MaxHashSize);
Validation.NotNull(nameof(message), message);
if (!message.CanRead) { throw new InvalidOperationException("The stream cannot be read."); }
int bytesRead;
Span<byte> buffer = new byte[StreamBufferSize];
using var blake2b = new IncrementalBLAKE2b(hash.Length);
Expand Down

0 comments on commit 0ab9993

Please sign in to comment.