1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.ComponentModel;
6 
7 namespace System.Security.Cryptography
8 {
9     //
10     // If you change this file, make the corresponding changes to all of the SHA*CryptoServiceProvider.cs files.
11     //
12     [EditorBrowsable(EditorBrowsableState.Never)]
13     public sealed class SHA512CryptoServiceProvider : SHA512
14     {
15         private const int HashSizeBits = 512;
16         private readonly IncrementalHash _incrementalHash;
17 
SHA512CryptoServiceProvider()18         public SHA512CryptoServiceProvider()
19         {
20             _incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA512);
21             HashSizeValue = HashSizeBits;
22         }
23 
Initialize()24         public override void Initialize()
25         {
26             // Nothing to do here. We expect HashAlgorithm to invoke HashFinal() and Initialize() as a pair. This reflects the
27             // reality that our native crypto providers (e.g. CNG) expose hash finalization and object reinitialization as an atomic operation.
28         }
29 
HashCore(byte[] array, int ibStart, int cbSize)30         protected override void HashCore(byte[] array, int ibStart, int cbSize) =>
31             _incrementalHash.AppendData(array, ibStart, cbSize);
32 
33         protected override void HashCore(ReadOnlySpan<byte> source) =>
34             _incrementalHash.AppendData(source);
35 
HashFinal()36         protected override byte[] HashFinal() =>
37             _incrementalHash.GetHashAndReset();
38 
TryHashFinal(Span<byte> destination, out int bytesWritten)39         protected override bool TryHashFinal(Span<byte> destination, out int bytesWritten) =>
40             _incrementalHash.TryGetHashAndReset(destination, out bytesWritten);
41 
42         // The Hash and HashSize properties are not overridden since the correct values are returned from base.
43 
Dispose(bool disposing)44         protected override void Dispose(bool disposing)
45         {
46             if (disposing)
47             {
48                 _incrementalHash.Dispose();
49             }
50             base.Dispose(disposing);
51          }
52     }
53 }
54