Skip to content

Commit 02fd751

Browse files
authored
Fix shimmed implementation of TryGetHashAndReset to handle HMAC.
The TryGetHashAndReset switches on the algorithm name of IncrementalHash. IncrementalHash prepends "HMAC" in front of the algorithm name, so the shim did not correctly handle the HMAC-prepended algorithm names. This gets rid of the switch, since it was only used as an optimization in a non-success path.
1 parent cdb88b2 commit 02fd751

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

src/libraries/Microsoft.Bcl.Cryptography/src/System/Security/Cryptography/NetStandardShims.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,14 @@ internal static bool TryGetHashAndReset(
100100
Span<byte> destination,
101101
out int bytesWritten)
102102
{
103-
int hashSize = hash.AlgorithmName.Name switch
104-
{
105-
nameof(HashAlgorithmName.MD5) => 128 >> 3,
106-
nameof(HashAlgorithmName.SHA1) => 160 >> 3,
107-
nameof(HashAlgorithmName.SHA256) => 256 >> 3,
108-
nameof(HashAlgorithmName.SHA384) => 384 >> 3,
109-
nameof(HashAlgorithmName.SHA512) => 512 >> 3,
110-
_ => throw new CryptographicException(),
111-
};
112-
113-
if (destination.Length < hashSize)
103+
byte[] actual = hash.GetHashAndReset();
104+
105+
if (destination.Length < actual.Length)
114106
{
115107
bytesWritten = 0;
116108
return false;
117109
}
118110

119-
byte[] actual = hash.GetHashAndReset();
120-
Debug.Assert(actual.Length == hashSize);
121-
122111
actual.AsSpan().CopyTo(destination);
123112
bytesWritten = actual.Length;
124113
return true;

0 commit comments

Comments
 (0)