/
SHA256CryptoServiceProvider.cs
76 lines (64 loc) · 2.42 KB
/
SHA256CryptoServiceProvider.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Diagnostics;
namespace System.Security.Cryptography
{
//
// If you change this file, make the corresponding changes to all of the SHA*CryptoServiceProvider.cs files.
//
[Obsolete(Obsoletions.DerivedCryptographicTypesMessage, DiagnosticId = Obsoletions.DerivedCryptographicTypesDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class SHA256CryptoServiceProvider : SHA256
{
private readonly IncrementalHash _incrementalHash;
private bool _running;
public SHA256CryptoServiceProvider()
{
_incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
HashSizeValue = HashSizeInBits;
}
public override void Initialize()
{
if (_running)
{
Span<byte> destination = stackalloc byte[HashSizeInBytes];
if (!_incrementalHash.TryGetHashAndReset(destination, out _))
{
Debug.Fail("Reset expected a properly sized buffer.");
throw new CryptographicException();
}
_running = false;
}
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
_running = true;
_incrementalHash.AppendData(array, ibStart, cbSize);
}
protected override void HashCore(ReadOnlySpan<byte> source)
{
_running = true;
_incrementalHash.AppendData(source);
}
protected override byte[] HashFinal()
{
_running = false;
return _incrementalHash.GetHashAndReset();
}
protected override bool TryHashFinal(Span<byte> destination, out int bytesWritten)
{
_running = false;
return _incrementalHash.TryGetHashAndReset(destination, out bytesWritten);
}
// The Hash and HashSize properties are not overridden since the correct values are returned from base.
protected override void Dispose(bool disposing)
{
if (disposing)
{
_incrementalHash.Dispose();
}
base.Dispose(disposing);
}
}
}