Skip to content

Files

Latest commit

 

History

History
43 lines (31 loc) · 808 Bytes

File metadata and controls

43 lines (31 loc) · 808 Bytes

Pattern: Missing use of HashData()

Issue: -

Description

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);
}

Further Reading