Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hash work in RsaPaddingProcessor.OpenProcessor. #68553

Merged
merged 2 commits into from Apr 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -76,46 +76,43 @@ internal static RsaPaddingProcessor OpenProcessor(HashAlgorithmName hashAlgorith
hashAlgorithmName,
static hashAlgorithmName =>
{
using (IncrementalHash hasher = IncrementalHash.CreateHash(hashAlgorithmName))
{
// SHA-2-512 is the biggest we expect
Span<byte> stackDest = stackalloc byte[512 / 8];
ReadOnlyMemory<byte> digestInfoPrefix;

if (hashAlgorithmName == HashAlgorithmName.MD5)
{
digestInfoPrefix = s_digestInfoMD5;
}
else if (hashAlgorithmName == HashAlgorithmName.SHA1)
{
digestInfoPrefix = s_digestInfoSha1;
}
else if (hashAlgorithmName == HashAlgorithmName.SHA256)
{
digestInfoPrefix = s_digestInfoSha256;
}
else if (hashAlgorithmName == HashAlgorithmName.SHA384)
{
digestInfoPrefix = s_digestInfoSha384;
}
else if (hashAlgorithmName == HashAlgorithmName.SHA512)
{
digestInfoPrefix = s_digestInfoSha512;
}
else
{
Debug.Fail("Unknown digest algorithm");
throw new CryptographicException();
}
ReadOnlyMemory<byte> digestInfoPrefix;
int digestLength;

if (hasher.TryGetHashAndReset(stackDest, out int bytesWritten))
{
return new RsaPaddingProcessor(hashAlgorithmName, bytesWritten, digestInfoPrefix);
}

byte[] big = hasher.GetHashAndReset();
return new RsaPaddingProcessor(hashAlgorithmName, big.Length, digestInfoPrefix);
if (hashAlgorithmName == HashAlgorithmName.MD5)
{
digestInfoPrefix = s_digestInfoMD5;
#pragma warning disable CA1416 // Unsupported on Browser. We just want the const here.
digestLength = MD5.HashSizeInBytes;
#pragma warning restore CA1416
}
else if (hashAlgorithmName == HashAlgorithmName.SHA1)
{
digestInfoPrefix = s_digestInfoSha1;
digestLength = SHA1.HashSizeInBytes;
}
else if (hashAlgorithmName == HashAlgorithmName.SHA256)
{
digestInfoPrefix = s_digestInfoSha256;
digestLength = SHA256.HashSizeInBytes;
}
else if (hashAlgorithmName == HashAlgorithmName.SHA384)
{
digestInfoPrefix = s_digestInfoSha384;
digestLength = SHA384.HashSizeInBytes;
}
else if (hashAlgorithmName == HashAlgorithmName.SHA512)
{
digestInfoPrefix = s_digestInfoSha512;
digestLength = SHA512.HashSizeInBytes;
}
else
{
Debug.Fail("Unknown digest algorithm");
throw new CryptographicException();
}

return new RsaPaddingProcessor(hashAlgorithmName, digestLength, digestInfoPrefix);
});
}

Expand Down