Replace CngCommon hash with OneShotHashHelpers.
authorKevin Jones <kevin@vcsjones.com>
Sat, 5 Mar 2022 03:49:08 +0000 (22:49 -0500)
committerGitHub <noreply@github.com>
Sat, 5 Mar 2022 03:49:08 +0000 (22:49 -0500)
src/libraries/Common/src/System/Security/Cryptography/DSACng.cs
src/libraries/Common/src/System/Security/Cryptography/ECDsaCng.HashData.cs
src/libraries/Common/src/System/Security/Cryptography/RSACng.cs
src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CngCommon.Hash.cs [deleted file]

index d9f8ed5..3055b53 100644 (file)
@@ -47,13 +47,13 @@ namespace System.Security.Cryptography
 
         // Need to override since base methods throw a "override me" exception: makes SignData/VerifyData function.
         protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
-            CngCommon.HashData(data, offset, count, hashAlgorithm);
+            HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan<byte>(data, offset, count));
 
         protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
-            CngCommon.HashData(data, hashAlgorithm);
+            HashOneShotHelpers.HashData(hashAlgorithm, data);
 
         protected override bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
-            CngCommon.TryHashData(source, destination, hashAlgorithm, out bytesWritten);
+            HashOneShotHelpers.TryHashData(hashAlgorithm, source, destination, out bytesWritten);
 
         private void ForceSetKeySize(int newKeySize)
         {
index fcbc2db..21a13cf 100644 (file)
@@ -10,12 +10,12 @@ namespace System.Security.Cryptography
     public sealed partial class ECDsaCng : ECDsa
     {
         protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
-            CngCommon.HashData(data, offset, count, hashAlgorithm);
+            HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan<byte>(data, offset, count));
 
         protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
-            CngCommon.HashData(data, hashAlgorithm);
+            HashOneShotHelpers.HashData(hashAlgorithm, data);
 
         protected override bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
-            CngCommon.TryHashData(source, destination, hashAlgorithm, out bytesWritten);
+            HashOneShotHelpers.TryHashData(hashAlgorithm, source, destination, out bytesWritten);
     }
 }
index 9e9b1d5..29491d6 100644 (file)
@@ -46,13 +46,13 @@ namespace System.Security.Cryptography
         }
 
         protected override byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm) =>
-            CngCommon.HashData(data, offset, count, hashAlgorithm);
+            HashOneShotHelpers.HashData(hashAlgorithm, new ReadOnlySpan<byte>(data, offset, count));
 
         protected override bool TryHashData(ReadOnlySpan<byte> data, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten) =>
-            CngCommon.TryHashData(data, destination, hashAlgorithm, out bytesWritten);
+            HashOneShotHelpers.TryHashData(hashAlgorithm, data, destination, out bytesWritten);
 
         protected override byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm) =>
-            CngCommon.HashData(data, hashAlgorithm);
+            HashOneShotHelpers.HashData(hashAlgorithm, data);
 
         private void ForceSetKeySize(int newKeySize)
         {
index 2f68524..0cb9fd6 100644 (file)
     <Compile Include="System\Security\Cryptography\ChaCha20Poly1305.Windows.cs" />
     <Compile Include="System\Security\Cryptography\CngAlgorithmCore.cs" />
     <Compile Include="System\Security\Cryptography\CngAsnFormatter.cs" />
-    <Compile Include="System\Security\Cryptography\CngCommon.Hash.cs" />
     <Compile Include="System\Security\Cryptography\CngCommon.SignVerify.cs" />
     <Compile Include="System\Security\Cryptography\CngHelpers.cs" />
     <Compile Include="System\Security\Cryptography\CngKey.cs" />
diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CngCommon.Hash.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CngCommon.Hash.cs
deleted file mode 100644 (file)
index 2060412..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-using System;
-using System.Diagnostics;
-using System.IO;
-
-namespace System.Security.Cryptography
-{
-    internal static partial class CngCommon
-    {
-        public static byte[] HashData(byte[] data, int offset, int count, HashAlgorithmName hashAlgorithm)
-        {
-            // The classes that call us are sealed and their base class has checked this already.
-            Debug.Assert(data != null);
-            Debug.Assert(offset >= 0 && offset <= data.Length);
-            Debug.Assert(count >= 0 && count <= data.Length);
-            Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name));
-
-            using (var hashProvider = new HashProviderCng(hashAlgorithm.Name, null))
-            {
-                hashProvider.AppendHashData(data, offset, count);
-                return hashProvider.FinalizeHashAndReset();
-            }
-        }
-
-        public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination, HashAlgorithmName hashAlgorithm, out int bytesWritten)
-        {
-            Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name));
-
-            using (var hashProvider = new HashProviderCng(hashAlgorithm.Name, null))
-            {
-                if (destination.Length < hashProvider.HashSizeInBytes)
-                {
-                    bytesWritten = 0;
-                    return false;
-                }
-
-                hashProvider.AppendHashData(source);
-                return hashProvider.TryFinalizeHashAndReset(destination, out bytesWritten);
-            }
-        }
-
-        public static byte[] HashData(Stream data, HashAlgorithmName hashAlgorithm)
-        {
-            // The classes that call us are sealed and their base class has checked this already.
-            Debug.Assert(data != null);
-            Debug.Assert(!string.IsNullOrEmpty(hashAlgorithm.Name));
-
-            using (var hashProvider = new HashProviderCng(hashAlgorithm.Name, null))
-            {
-                // Default the buffer size to 4K.
-                byte[] buffer = new byte[4096];
-                int bytesRead;
-                while ((bytesRead = data.Read(buffer, 0, buffer.Length)) > 0)
-                {
-                    hashProvider.AppendHashData(buffer, 0, bytesRead);
-                }
-                byte[] hash = hashProvider.FinalizeHashAndReset();
-                return hash;
-            }
-        }
-    }
-}