Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HashAlgorithm.cs
Go to the documentation of this file.
3using System.IO;
6
8
10{
11 private bool _disposed;
12
13 protected int HashSizeValue;
14
15 protected internal byte[]? HashValue;
16
17 protected int State;
18
19 public virtual int HashSize => HashSizeValue;
20
21 public virtual byte[]? Hash
22 {
23 get
24 {
25 if (_disposed)
26 {
27 throw new ObjectDisposedException(null);
28 }
29 if (State != 0)
30 {
32 }
33 return (byte[])HashValue?.Clone();
34 }
35 }
36
37 public virtual int InputBlockSize => 1;
38
39 public virtual int OutputBlockSize => 1;
40
41 public virtual bool CanTransformMultipleBlocks => true;
42
43 public virtual bool CanReuseTransform => true;
44
45 [Obsolete("The default implementation of this cryptography algorithm is not supported.", DiagnosticId = "SYSLIB0007", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
50
51 [RequiresUnreferencedCode("The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead.")]
52 public static HashAlgorithm? Create(string hashName)
53 {
55 }
56
57 public byte[] ComputeHash(byte[] buffer)
58 {
59 if (_disposed)
60 {
61 throw new ObjectDisposedException(null);
62 }
63 if (buffer == null)
64 {
65 throw new ArgumentNullException("buffer");
66 }
67 HashCore(buffer, 0, buffer.Length);
69 }
70
72 {
73 if (_disposed)
74 {
75 throw new ObjectDisposedException(null);
76 }
77 if (destination.Length < HashSizeValue / 8)
78 {
79 bytesWritten = 0;
80 return false;
81 }
83 if (!TryHashFinal(destination, out bytesWritten))
84 {
86 }
87 HashValue = null;
88 Initialize();
89 return true;
90 }
91
92 public byte[] ComputeHash(byte[] buffer, int offset, int count)
93 {
94 if (buffer == null)
95 {
96 throw new ArgumentNullException("buffer");
97 }
98 if (offset < 0)
99 {
101 }
102 if (count < 0 || count > buffer.Length)
103 {
105 }
106 if (buffer.Length - count < offset)
107 {
109 }
110 if (_disposed)
111 {
112 throw new ObjectDisposedException(null);
113 }
116 }
117
118 public byte[] ComputeHash(Stream inputStream)
119 {
120 if (_disposed)
121 {
122 throw new ObjectDisposedException(null);
123 }
124 byte[] array = ArrayPool<byte>.Shared.Rent(4096);
125 int num = 0;
126 int num2;
127 while ((num2 = inputStream.Read(array, 0, array.Length)) > 0)
128 {
129 if (num2 > num)
130 {
131 num = num2;
132 }
133 HashCore(array, 0, num2);
134 }
138 }
139
141 {
142 if (inputStream == null)
143 {
144 throw new ArgumentNullException("inputStream");
145 }
146 if (_disposed)
147 {
148 throw new ObjectDisposedException(null);
149 }
150 return ComputeHashAsyncCore(inputStream, cancellationToken);
151 }
152
154 {
155 byte[] rented = ArrayPool<byte>.Shared.Rent(4096);
156 Memory<byte> buffer = rented;
157 int clearLimit = 0;
158 int num;
159 while ((num = await inputStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(continueOnCapturedContext: false)) > 0)
160 {
161 if (num > clearLimit)
162 {
163 clearLimit = num;
164 }
165 HashCore(rented, 0, num);
166 }
167 CryptographicOperations.ZeroMemory(rented.AsSpan(0, clearLimit));
168 ArrayPool<byte>.Shared.Return(rented);
170 }
171
173 {
175 byte[] result = (byte[])HashValue.Clone();
176 Initialize();
177 return result;
178 }
179
180 public void Dispose()
181 {
182 Dispose(disposing: true);
183 GC.SuppressFinalize(this);
184 }
185
186 public void Clear()
187 {
188 ((IDisposable)this).Dispose();
189 }
190
191 protected virtual void Dispose(bool disposing)
192 {
193 if (disposing)
194 {
195 _disposed = true;
196 }
197 }
198
199 public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[]? outputBuffer, int outputOffset)
200 {
201 ValidateTransformBlock(inputBuffer, inputOffset, inputCount);
202 State = 1;
203 HashCore(inputBuffer, inputOffset, inputCount);
204 if (outputBuffer != null && (inputBuffer != outputBuffer || inputOffset != outputOffset))
205 {
206 Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
207 }
208 return inputCount;
209 }
210
211 public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
212 {
213 ValidateTransformBlock(inputBuffer, inputOffset, inputCount);
214 HashCore(inputBuffer, inputOffset, inputCount);
216 byte[] array;
217 if (inputCount != 0)
218 {
219 array = new byte[inputCount];
220 Buffer.BlockCopy(inputBuffer, inputOffset, array, 0, inputCount);
221 }
222 else
223 {
224 array = Array.Empty<byte>();
225 }
226 State = 0;
227 return array;
228 }
229
230 private void ValidateTransformBlock(byte[] inputBuffer, int inputOffset, int inputCount)
231 {
232 if (inputBuffer == null)
233 {
234 throw new ArgumentNullException("inputBuffer");
235 }
236 if (inputOffset < 0)
237 {
239 }
240 if (inputCount < 0 || inputCount > inputBuffer.Length)
241 {
243 }
244 if (inputBuffer.Length - inputCount < inputOffset)
245 {
247 }
248 if (_disposed)
249 {
250 throw new ObjectDisposedException(null);
251 }
252 }
253
254 protected abstract void HashCore(byte[] array, int ibStart, int cbSize);
255
256 protected abstract byte[] HashFinal();
257
258 public abstract void Initialize();
259
260 protected virtual void HashCore(ReadOnlySpan<byte> source)
261 {
262 byte[] array = ArrayPool<byte>.Shared.Rent(source.Length);
263 source.CopyTo(array);
264 HashCore(array, 0, source.Length);
265 Array.Clear(array, 0, source.Length);
267 }
268
269 protected virtual bool TryHashFinal(Span<byte> destination, out int bytesWritten)
270 {
271 int num = HashSizeValue / 8;
272 if (destination.Length >= num)
273 {
274 byte[] array = HashFinal();
275 if (array.Length == num)
276 {
278 bytesWritten = array.Length;
279 return true;
280 }
282 }
283 bytesWritten = 0;
284 return false;
285 }
286}
static unsafe void Clear(Array array)
Definition Array.cs:755
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static ArrayPool< T > Shared
Definition ArrayPool.cs:7
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
int Read(byte[] buffer, int offset, int count)
Task< int > ReadAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:762
static string Argument_InvalidValue
Definition SR.cs:24
static string Cryptography_HashNotYetFinalized
Definition SR.cs:36
static string InvalidOperation_IncorrectImplementation
Definition SR.cs:72
static string Argument_InvalidOffLen
Definition SR.cs:22
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
void HashCore(byte[] array, int ibStart, int cbSize)
virtual void Dispose(bool disposing)
virtual bool TryHashFinal(Span< byte > destination, out int bytesWritten)
async Task< byte[]> ComputeHashAsyncCore(Stream inputStream, CancellationToken cancellationToken)
bool TryComputeHash(ReadOnlySpan< byte > source, Span< byte > destination, out int bytesWritten)
static ? HashAlgorithm Create(string hashName)
byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
byte[] ComputeHash(byte[] buffer, int offset, int count)
int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[]? outputBuffer, int outputOffset)
virtual void HashCore(ReadOnlySpan< byte > source)
void ValidateTransformBlock(byte[] inputBuffer, int inputOffset, int inputCount)
Task< byte[]> ComputeHashAsync(Stream inputStream, CancellationToken cancellationToken=default(CancellationToken))
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
void CopyTo(Span< T > destination)