Pattern: Missing use of HashData()
Issue: -
It's more efficient to use static HashData
methods than to create and manage a HashAlgorithm
instance to call ComputeHash
.
Static HashData
methods were introduced in .NET 5 on the following types:
- MD5
- SHA1
- SHA256
- SHA384
- SHA512
Example of incorrect code:
public bool CheckHash(byte[] buffer)
{
using (var sha256 = SHA256.Create())
{
byte[] digest = sha256.ComputeHash(buffer);
return DoesHashExist(digest);
}
}
Example of correct code:
public bool CheckHash(byte[] buffer)
{
byte[] digest = SHA256.HashData(buffer);
return DoesHashExist(digest);
}