Skip to content

Commit e4163ea

Browse files
authored
Remove hash work in RsaPaddingProcessor.OpenProcessor
Remove hash work in RsaPaddingProcessor.OpenProcessor. This was hashing data to determine the length of the hash. But we know MD5 is always going to be 16, SHA1 is going to be 20, etc. so don't waste any time hashing anything.
1 parent 8dec3ce commit e4163ea

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

src/libraries/Common/src/System/Security/Cryptography/RsaPaddingProcessor.cs

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,46 +76,43 @@ internal static RsaPaddingProcessor OpenProcessor(HashAlgorithmName hashAlgorith
7676
hashAlgorithmName,
7777
static hashAlgorithmName =>
7878
{
79-
using (IncrementalHash hasher = IncrementalHash.CreateHash(hashAlgorithmName))
80-
{
81-
// SHA-2-512 is the biggest we expect
82-
Span<byte> stackDest = stackalloc byte[512 / 8];
83-
ReadOnlyMemory<byte> digestInfoPrefix;
84-
85-
if (hashAlgorithmName == HashAlgorithmName.MD5)
86-
{
87-
digestInfoPrefix = s_digestInfoMD5;
88-
}
89-
else if (hashAlgorithmName == HashAlgorithmName.SHA1)
90-
{
91-
digestInfoPrefix = s_digestInfoSha1;
92-
}
93-
else if (hashAlgorithmName == HashAlgorithmName.SHA256)
94-
{
95-
digestInfoPrefix = s_digestInfoSha256;
96-
}
97-
else if (hashAlgorithmName == HashAlgorithmName.SHA384)
98-
{
99-
digestInfoPrefix = s_digestInfoSha384;
100-
}
101-
else if (hashAlgorithmName == HashAlgorithmName.SHA512)
102-
{
103-
digestInfoPrefix = s_digestInfoSha512;
104-
}
105-
else
106-
{
107-
Debug.Fail("Unknown digest algorithm");
108-
throw new CryptographicException();
109-
}
79+
ReadOnlyMemory<byte> digestInfoPrefix;
80+
int digestLength;
11081

111-
if (hasher.TryGetHashAndReset(stackDest, out int bytesWritten))
112-
{
113-
return new RsaPaddingProcessor(hashAlgorithmName, bytesWritten, digestInfoPrefix);
114-
}
115-
116-
byte[] big = hasher.GetHashAndReset();
117-
return new RsaPaddingProcessor(hashAlgorithmName, big.Length, digestInfoPrefix);
82+
if (hashAlgorithmName == HashAlgorithmName.MD5)
83+
{
84+
digestInfoPrefix = s_digestInfoMD5;
85+
#pragma warning disable CA1416 // Unsupported on Browser. We just want the const here.
86+
digestLength = MD5.HashSizeInBytes;
87+
#pragma warning restore CA1416
88+
}
89+
else if (hashAlgorithmName == HashAlgorithmName.SHA1)
90+
{
91+
digestInfoPrefix = s_digestInfoSha1;
92+
digestLength = SHA1.HashSizeInBytes;
93+
}
94+
else if (hashAlgorithmName == HashAlgorithmName.SHA256)
95+
{
96+
digestInfoPrefix = s_digestInfoSha256;
97+
digestLength = SHA256.HashSizeInBytes;
98+
}
99+
else if (hashAlgorithmName == HashAlgorithmName.SHA384)
100+
{
101+
digestInfoPrefix = s_digestInfoSha384;
102+
digestLength = SHA384.HashSizeInBytes;
118103
}
104+
else if (hashAlgorithmName == HashAlgorithmName.SHA512)
105+
{
106+
digestInfoPrefix = s_digestInfoSha512;
107+
digestLength = SHA512.HashSizeInBytes;
108+
}
109+
else
110+
{
111+
Debug.Fail("Unknown digest algorithm");
112+
throw new CryptographicException();
113+
}
114+
115+
return new RsaPaddingProcessor(hashAlgorithmName, digestLength, digestInfoPrefix);
119116
});
120117
}
121118

0 commit comments

Comments
 (0)