Skip to content

Commit

Permalink
Use the static HashData(Stream) method in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsjones committed Jan 24, 2022
1 parent d99fa67 commit 2713e11
Showing 1 changed file with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,21 @@ public static byte[] HashData(byte[] data, int offset, int count, HashAlgorithmN
throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithm.Name);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5350", Justification = "SHA1 is used when the user asks for it.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5351", Justification = "MD5 is used when the user asks for it.")]
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 (HashAlgorithm hasher = GetHashAlgorithm(hashAlgorithm))
{
return hasher.ComputeHash(data);
}
return
hashAlgorithm == HashAlgorithmName.SHA256 ? SHA256.HashData(data) :
hashAlgorithm == HashAlgorithmName.SHA1 ? SHA1.HashData(data) :
hashAlgorithm == HashAlgorithmName.SHA512 ? SHA512.HashData(data) :
hashAlgorithm == HashAlgorithmName.SHA384 ? SHA384.HashData(data) :
hashAlgorithm == HashAlgorithmName.MD5 ? MD5.HashData(data) :
throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithm.Name);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5350", Justification = "SHA1 is used when the user asks for it.")]
Expand All @@ -61,15 +66,5 @@ public static bool TryHashData(ReadOnlySpan<byte> source, Span<byte> destination
hashAlgorithm == HashAlgorithmName.MD5 ? MD5.TryHashData(source, destination, out bytesWritten) :
throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithm.Name);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5350", Justification = "SHA1 is used when the user asks for it.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5351", Justification = "MD5 is used when the user asks for it.")]
private static HashAlgorithm GetHashAlgorithm(HashAlgorithmName hashAlgorithmName) =>
hashAlgorithmName == HashAlgorithmName.SHA256 ? SHA256.Create() :
hashAlgorithmName == HashAlgorithmName.SHA1 ? SHA1.Create() :
hashAlgorithmName == HashAlgorithmName.SHA512 ? SHA512.Create() :
hashAlgorithmName == HashAlgorithmName.SHA384 ? SHA384.Create() :
hashAlgorithmName == HashAlgorithmName.MD5 ? MD5.Create() :
throw new CryptographicException(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmName.Name);
}
}

0 comments on commit 2713e11

Please sign in to comment.