Skip to content

Commit

Permalink
[release/9.0-staging] Fix shimmed implementation of TryGetHashAndRese…
Browse files Browse the repository at this point in the history
…t to handle HMAC.

The TryGetHashAndReset in 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.


---------

Co-authored-by: Kevin Jones <[email protected]>
Co-authored-by: Larry Ewing <[email protected]>
  • Loading branch information
3 people authored Feb 6, 2025
1 parent a7bfd90 commit e50cf90
Showing 1 changed file with 3 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,14 @@ internal static bool TryGetHashAndReset(
Span<byte> destination,
out int bytesWritten)
{
int hashSize = hash.AlgorithmName.Name switch
{
nameof(HashAlgorithmName.MD5) => 128 >> 3,
nameof(HashAlgorithmName.SHA1) => 160 >> 3,
nameof(HashAlgorithmName.SHA256) => 256 >> 3,
nameof(HashAlgorithmName.SHA384) => 384 >> 3,
nameof(HashAlgorithmName.SHA512) => 512 >> 3,
_ => throw new CryptographicException(),
};

if (destination.Length < hashSize)
byte[] actual = hash.GetHashAndReset();

if (destination.Length < actual.Length)
{
bytesWritten = 0;
return false;
}

byte[] actual = hash.GetHashAndReset();
Debug.Assert(actual.Length == hashSize);

actual.AsSpan().CopyTo(destination);
bytesWritten = actual.Length;
return true;
Expand Down

0 comments on commit e50cf90

Please sign in to comment.