Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Mar 2, 2024
1 parent 7fb5079 commit b955382
Show file tree
Hide file tree
Showing 4 changed files with 632 additions and 641 deletions.
61 changes: 30 additions & 31 deletions src/Blake3/Blake3HashAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,40 @@
using System;
using System.Security.Cryptography;

namespace Blake3
namespace Blake3;

/// <summary>
/// Implementation of <see cref="HashAlgorithm"/> for BLAKE3.
/// </summary>
public class Blake3HashAlgorithm : HashAlgorithm
{
/// <summary>
/// Implementation of <see cref="HashAlgorithm"/> for BLAKE3.
/// </summary>
public class Blake3HashAlgorithm : HashAlgorithm
{
private Hasher _hasher;
private Hasher _hasher;

public Blake3HashAlgorithm()
{
_hasher = Hasher.New();
}
public Blake3HashAlgorithm()
{
_hasher = Hasher.New();
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_hasher.Dispose();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_hasher.Dispose();
}

protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
_hasher.Update(new ReadOnlySpan<byte>(array, ibStart, cbSize));
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
_hasher.Update(new ReadOnlySpan<byte>(array, ibStart, cbSize));
}

protected override byte[] HashFinal()
{
var hash = new byte[Blake3.Hash.Size];
_hasher.Finalize(hash);
return hash;
}
protected override byte[] HashFinal()
{
var hash = new byte[Blake3.Hash.Size];
_hasher.Finalize(hash);
return hash;
}

public override void Initialize()
{
_hasher.Reset();
}
public override void Initialize()
{
_hasher.Reset();
}
}
}
268 changes: 133 additions & 135 deletions src/Blake3/Blake3Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,180 +4,178 @@

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

namespace Blake3
namespace Blake3;

/// <summary>
/// A stream that allows to calculate a hash while reading/writing from a backend stream.
///
/// Use the <see cref="ComputeHash()"/> or <see cref="ComputeHash(System.Span{byte})"/> methods to calculate the hash before disposing the stream
/// </summary>
public class Blake3Stream : Stream
{
private readonly Stream _stream;
private readonly bool _dispose;
private Hasher _hasher;

/// <summary>
/// A stream that allows to calculate a hash while reading/writing from a backend stream.
///
/// Use the <see cref="ComputeHash()"/> or <see cref="ComputeHash(System.Span{byte})"/> methods to calculate the hash before disposing the stream
/// Creates an instance of <see cref="Blake3Stream"/> using the specified backend stream.
/// </summary>
public class Blake3Stream : Stream
{
private readonly Stream _stream;
private readonly bool _dispose;
private Hasher _hasher;

/// <summary>
/// Creates an instance of <see cref="Blake3Stream"/> using the specified backend stream.
/// </summary>
/// <param name="backendStream"></param>
/// <param name="dispose">A boolean that indicates if this stream will dispose the backend stream. Default is true.</param>
public Blake3Stream(Stream backendStream, bool dispose = true)
{
_stream = backendStream ?? throw new ArgumentNullException(nameof(backendStream));
_dispose = dispose;
_hasher = Hasher.New();
}
/// <param name="backendStream"></param>
/// <param name="dispose">A boolean that indicates if this stream will dispose the backend stream. Default is true.</param>
public Blake3Stream(Stream backendStream, bool dispose = true)
{
_stream = backendStream ?? throw new ArgumentNullException(nameof(backendStream));
_dispose = dispose;
_hasher = Hasher.New();
}

protected override void Dispose(bool disposing)
protected override void Dispose(bool disposing)
{
_hasher.Dispose();
if (_dispose)
{
_hasher.Dispose();
if (_dispose)
{
_stream.Dispose();
}
_stream.Dispose();
}
}

public override async ValueTask DisposeAsync()
public override async ValueTask DisposeAsync()
{
_hasher.Dispose();
if (_dispose)
{
_hasher.Dispose();
if (_dispose)
{
await _stream.DisposeAsync();
}
await _stream.DisposeAsync();
}
}

public override void Flush()
{
_stream.Flush();
}
public override void Flush()
{
_stream.Flush();
}

public override async Task FlushAsync(CancellationToken cancellationToken)
{
await _stream.FlushAsync(cancellationToken);
}
public override async Task FlushAsync(CancellationToken cancellationToken)
{
await _stream.FlushAsync(cancellationToken);
}

public void ResetHash()
{
_hasher.Reset();
}
public void ResetHash()
{
_hasher.Reset();
}

public Hash ComputeHash()
{
return _hasher.Finalize();
}
public Hash ComputeHash()
{
return _hasher.Finalize();
}

public void ComputeHash(Span<byte> hash)
{
_hasher.Finalize(hash);
}
public void ComputeHash(Span<byte> hash)
{
_hasher.Finalize(hash);
}

public override int Read(byte[] buffer, int offset, int count)
public override int Read(byte[] buffer, int offset, int count)
{
var length = _stream.Read(buffer, offset, count);
if (length > 0)
{
var length = _stream.Read(buffer, offset, count);
if (length > 0)
{
var span = new Span<byte>(buffer, offset, length);
_hasher.Update(span);
}
return length;
var span = new Span<byte>(buffer, offset, length);
_hasher.Update(span);
}
return length;
}

public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
var length = await _stream.ReadAsync(buffer, offset, count, cancellationToken);
if (length > 0)
{
var length = await _stream.ReadAsync(buffer, offset, count, cancellationToken);
if (length > 0)
{
_hasher.Update(new Span<byte>(buffer, offset, length));
}
return length;
_hasher.Update(new Span<byte>(buffer, offset, length));
}
return length;
}

public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = new CancellationToken())
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = new CancellationToken())
{
var length = await _stream.ReadAsync(buffer, cancellationToken);
if (length > 0)
{
var length = await _stream.ReadAsync(buffer, cancellationToken);
if (length > 0)
{
_hasher.Update(buffer.Span);
}
return length;
_hasher.Update(buffer.Span);
}
return length;
}

public override int Read(Span<byte> buffer)
public override int Read(Span<byte> buffer)
{
var length = _stream.Read(buffer);
if (length > 0)
{
var length = _stream.Read(buffer);
if (length > 0)
{
_hasher.Update(buffer);
}
return length;
_hasher.Update(buffer);
}
return length;
}

public override unsafe int ReadByte()
{
var value = _stream.ReadByte();
if (value < 0) return value;
var bValue = (byte) value;
var span = new ReadOnlySpan<byte>(&bValue, 1);
_hasher.Update(span);
return value;
}
public override unsafe int ReadByte()
{
var value = _stream.ReadByte();
if (value < 0) return value;
var bValue = (byte) value;
var span = new ReadOnlySpan<byte>(&bValue, 1);
_hasher.Update(span);
return value;
}

public override void Write(byte[] buffer, int offset, int count)
public override void Write(byte[] buffer, int offset, int count)
{
_stream.Write(buffer, offset, count);
if (count > 0)
{
_stream.Write(buffer, offset, count);
if (count > 0)
{
var span = new Span<byte>(buffer, offset, count);
_hasher.Update(span);
}
}

public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
await _stream.WriteAsync(buffer, offset, count, cancellationToken);
if (count > 0)
{
_hasher.Update(new Span<byte>(buffer, offset, count));
}
var span = new Span<byte>(buffer, offset, count);
_hasher.Update(span);
}
}

public override void Write(ReadOnlySpan<byte> buffer)
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
await _stream.WriteAsync(buffer, offset, count, cancellationToken);
if (count > 0)
{
_stream.Write(buffer);
_hasher.Update(buffer);
_hasher.Update(new Span<byte>(buffer, offset, count));
}
}

public override unsafe void WriteByte(byte value)
{
_stream.WriteByte(value);
var span = new ReadOnlySpan<byte>(&value, 1);
_hasher.Update(span);
}
public override void Write(ReadOnlySpan<byte> buffer)
{
_stream.Write(buffer);
_hasher.Update(buffer);
}

public override long Seek(long offset, SeekOrigin origin)
{
return _stream.Seek(offset, origin);
}
public override unsafe void WriteByte(byte value)
{
_stream.WriteByte(value);
var span = new ReadOnlySpan<byte>(&value, 1);
_hasher.Update(span);
}

public override void SetLength(long value)
{
_stream.SetLength(value);
}
public override long Seek(long offset, SeekOrigin origin)
{
return _stream.Seek(offset, origin);
}

public override void SetLength(long value)
{
_stream.SetLength(value);
}

public override bool CanRead => _stream.CanRead;
public override bool CanSeek => _stream.CanSeek;
public override bool CanWrite => _stream.CanWrite;
public override long Length => _stream.Length;
public override bool CanRead => _stream.CanRead;
public override bool CanSeek => _stream.CanSeek;
public override bool CanWrite => _stream.CanWrite;
public override long Length => _stream.Length;

public override long Position
{
get => _stream.Position;
set => _stream.Position = value;
}
public override long Position
{
get => _stream.Position;
set => _stream.Position = value;
}
}
}
Loading

0 comments on commit b955382

Please sign in to comment.