-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Blake3Stream.cs
181 lines (157 loc) · 4.83 KB
/
Blake3Stream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
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>
/// 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();
}
protected override void Dispose(bool disposing)
{
_hasher.Dispose();
if (_dispose)
{
_stream.Dispose();
}
}
public override async ValueTask DisposeAsync()
{
_hasher.Dispose();
if (_dispose)
{
await _stream.DisposeAsync();
}
}
public override void Flush()
{
_stream.Flush();
}
public override async Task FlushAsync(CancellationToken cancellationToken)
{
await _stream.FlushAsync(cancellationToken);
}
public void ResetHash()
{
_hasher.Reset();
}
public Hash ComputeHash()
{
return _hasher.Finalize();
}
public void ComputeHash(Span<byte> hash)
{
_hasher.Finalize(hash);
}
public override int Read(byte[] buffer, int offset, int count)
{
var length = _stream.Read(buffer, offset, count);
if (length > 0)
{
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)
{
var length = await _stream.ReadAsync(buffer, offset, count, cancellationToken);
if (length > 0)
{
_hasher.Update(new Span<byte>(buffer, offset, length));
}
return length;
}
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = new CancellationToken())
{
var length = await _stream.ReadAsync(buffer, cancellationToken);
if (length > 0)
{
_hasher.Update(buffer.Span);
}
return length;
}
public override int Read(Span<byte> buffer)
{
var length = _stream.Read(buffer);
if (length > 0)
{
_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 void Write(byte[] buffer, int offset, int count)
{
_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));
}
}
public override void Write(ReadOnlySpan<byte> buffer)
{
_stream.Write(buffer);
_hasher.Update(buffer);
}
public override unsafe void WriteByte(byte value)
{
_stream.WriteByte(value);
var span = new ReadOnlySpan<byte>(&value, 1);
_hasher.Update(span);
}
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 long Position
{
get => _stream.Position;
set => _stream.Position = value;
}
}