-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathHashAlgorithm.cs
More file actions
284 lines (230 loc) · 10 KB
/
HashAlgorithm.cs
File metadata and controls
284 lines (230 loc) · 10 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace System.Security.Cryptography
{
public abstract class HashAlgorithm : IDisposable, ICryptoTransform
{
private bool _disposed;
protected int HashSizeValue;
protected internal byte[]? HashValue;
protected int State;
protected HashAlgorithm() { }
[Obsolete(Obsoletions.DefaultCryptoAlgorithmsMessage, DiagnosticId = Obsoletions.DefaultCryptoAlgorithmsDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public static HashAlgorithm Create() =>
CryptoConfigForwarder.CreateDefaultHashAlgorithm();
[Obsolete(Obsoletions.CryptoStringFactoryMessage, DiagnosticId = Obsoletions.CryptoStringFactoryDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[RequiresUnreferencedCode(CryptoConfigForwarder.CreateFromNameUnreferencedCodeMessage)]
public static HashAlgorithm? Create(string hashName) =>
CryptoConfigForwarder.CreateFromName<HashAlgorithm>(hashName);
public virtual int HashSize => HashSizeValue;
public virtual byte[]? Hash
{
get
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (State != 0)
throw new CryptographicUnexpectedOperationException(SR.Cryptography_HashNotYetFinalized);
return (byte[]?)HashValue?.Clone();
}
}
public byte[] ComputeHash(byte[] buffer)
{
ObjectDisposedException.ThrowIf(_disposed, this);
ArgumentNullException.ThrowIfNull(buffer);
HashCore(buffer, 0, buffer.Length);
return CaptureHashCodeAndReinitialize();
}
public bool TryComputeHash(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (destination.Length < HashSizeValue / 8)
{
bytesWritten = 0;
return false;
}
HashCore(source);
if (!TryHashFinal(destination, out bytesWritten))
{
// The only reason for failure should be that the destination isn't long enough,
// but we checked the size earlier.
throw new InvalidOperationException(SR.InvalidOperation_IncorrectImplementation);
}
HashValue = null;
Initialize();
return true;
}
public byte[] ComputeHash(byte[] buffer, int offset, int count)
{
ArgumentNullException.ThrowIfNull(buffer);
if (offset < 0)
throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
if (count < 0 || (count > buffer.Length))
throw new ArgumentException(SR.Argument_InvalidValue);
if ((buffer.Length - count) < offset)
throw new ArgumentException(SR.Argument_InvalidOffLen);
ObjectDisposedException.ThrowIf(_disposed, this);
HashCore(buffer, offset, count);
return CaptureHashCodeAndReinitialize();
}
public byte[] ComputeHash(Stream inputStream)
{
ObjectDisposedException.ThrowIf(_disposed, this);
// Use ArrayPool.Shared instead of CryptoPool because the array is passed out.
byte[] buffer = ArrayPool<byte>.Shared.Rent(4096);
int bytesRead;
int clearLimit = 0;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
if (bytesRead > clearLimit)
{
clearLimit = bytesRead;
}
HashCore(buffer, 0, bytesRead);
}
CryptographicOperations.ZeroMemory(buffer.AsSpan(0, clearLimit));
ArrayPool<byte>.Shared.Return(buffer, clearArray: false);
return CaptureHashCodeAndReinitialize();
}
public Task<byte[]> ComputeHashAsync(
Stream inputStream,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(inputStream);
ObjectDisposedException.ThrowIf(_disposed, this);
return ComputeHashAsyncCore(inputStream, cancellationToken);
}
private async Task<byte[]> ComputeHashAsyncCore(
Stream inputStream,
CancellationToken cancellationToken)
{
// Use ArrayPool.Shared instead of CryptoPool because the array is passed out.
byte[] rented = ArrayPool<byte>.Shared.Rent(4096);
Memory<byte> buffer = rented;
int clearLimit = 0;
int bytesRead;
while ((bytesRead = await inputStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0)
{
if (bytesRead > clearLimit)
{
clearLimit = bytesRead;
}
HashCore(rented, 0, bytesRead);
}
CryptographicOperations.ZeroMemory(rented.AsSpan(0, clearLimit));
ArrayPool<byte>.Shared.Return(rented, clearArray: false);
return CaptureHashCodeAndReinitialize();
}
private byte[] CaptureHashCodeAndReinitialize()
{
HashValue = HashFinal();
// Clone the hash value prior to invoking Initialize in case the user-defined Initialize
// manipulates the array.
byte[] tmp = (byte[])HashValue.Clone();
Initialize();
return tmp;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Clear()
{
(this as IDisposable).Dispose();
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Although we don't have any resources to dispose at this level,
// we need to continue to throw ObjectDisposedExceptions from CalculateHash
// for compatibility with the .NET Framework.
_disposed = true;
}
return;
}
// ICryptoTransform methods
// We assume any HashAlgorithm can take input a byte at a time
public virtual int InputBlockSize => 1;
public virtual int OutputBlockSize => 1;
public virtual bool CanTransformMultipleBlocks => true;
public virtual bool CanReuseTransform => true;
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[]? outputBuffer, int outputOffset)
{
ValidateTransformBlock(inputBuffer, inputOffset, inputCount);
// Change the State value
State = 1;
HashCore(inputBuffer, inputOffset, inputCount);
if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
{
// We let BlockCopy do the destination array validation
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
}
return inputCount;
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
ValidateTransformBlock(inputBuffer, inputOffset, inputCount);
HashCore(inputBuffer, inputOffset, inputCount);
HashValue = CaptureHashCodeAndReinitialize();
byte[] outputBytes;
if (inputCount != 0)
{
outputBytes = new byte[inputCount];
Buffer.BlockCopy(inputBuffer, inputOffset, outputBytes, 0, inputCount);
}
else
{
outputBytes = Array.Empty<byte>();
}
// Reset the State value
State = 0;
return outputBytes;
}
private void ValidateTransformBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
ArgumentNullException.ThrowIfNull(inputBuffer);
if (inputOffset < 0)
throw new ArgumentOutOfRangeException(nameof(inputOffset), SR.ArgumentOutOfRange_NeedNonNegNum);
if (inputCount < 0 || inputCount > inputBuffer.Length)
throw new ArgumentException(SR.Argument_InvalidValue);
if ((inputBuffer.Length - inputCount) < inputOffset)
throw new ArgumentException(SR.Argument_InvalidOffLen);
ObjectDisposedException.ThrowIf(_disposed, this);
}
protected abstract void HashCore(byte[] array, int ibStart, int cbSize);
protected abstract byte[] HashFinal();
public abstract void Initialize();
protected virtual void HashCore(ReadOnlySpan<byte> source)
{
// Use ArrayPool.Shared instead of CryptoPool because the array is passed out.
byte[] array = ArrayPool<byte>.Shared.Rent(source.Length);
source.CopyTo(array);
HashCore(array, 0, source.Length);
Array.Clear(array, 0, source.Length);
ArrayPool<byte>.Shared.Return(array);
}
protected virtual bool TryHashFinal(Span<byte> destination, out int bytesWritten)
{
int hashSizeInBytes = HashSizeValue / 8;
if (destination.Length >= hashSizeInBytes)
{
byte[] final = HashFinal();
if (final.Length == hashSizeInBytes)
{
new ReadOnlySpan<byte>(final).CopyTo(destination);
bytesWritten = final.Length;
return true;
}
throw new InvalidOperationException(SR.InvalidOperation_IncorrectImplementation);
}
bytesWritten = 0;
return false;
}
}
}