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

Use Stream hash one-shot #64174

Merged
merged 1 commit into from
Jan 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}