-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
--- | ||
title: "CA1850: Prefer static `HashData` method over `ComputeHash`" | ||
description: "It is more efficient to use the static `HashData` method over creating and managing a HashAlgorithm instance to call `ComputeHash`" | ||
ms.date: 10/23/2021 | ||
ms.topic: reference | ||
f1_keywords: | ||
- "CA1850" | ||
helpviewer_keywords: | ||
- "CA1850" | ||
author: wzchua | ||
dev_langs: | ||
- CSharp | ||
- VB | ||
--- | ||
|
||
# CA1850: Prefer static `HashData` method over `ComputeHash` | ||
|
||
| | Value | | ||
|-|-| | ||
| **Rule ID** |CA1850| | ||
| **Category** |[Performance](performance-warnings.md)| | ||
| **Fix is breaking or non-breaking** |Non-breaking| | ||
|
||
## Cause | ||
|
||
An instance of a type that derives from <xref:System.Security.Cryptography.HashAlgorithm> is created to call its `ComputeHash` method, and that type has a static `HashData` method. | ||
|
||
## Rule description | ||
|
||
Static `HashData` methods were introduced in .NET 5 on the following types: | ||
|
||
- <xref:System.Security.Cryptography.MD5> | ||
- <xref:System.Security.Cryptography.SHA1> | ||
- <xref:System.Security.Cryptography.SHA256> | ||
- <xref:System.Security.Cryptography.SHA384> | ||
- <xref:System.Security.Cryptography.SHA512> | ||
|
||
These methods help simplify code in cases where you just want to hash some data. | ||
|
||
It's more efficient to use theses static `HashData` methods than to create and manage a `HashAlgorithm` instance to call `ComputeHash`. | ||
|
||
## How to fix violations | ||
|
||
In general, you can fix the rule by changing your code to call `HashData` and remove use of the `HashAlgorithm` instance. | ||
|
||
```csharp | ||
public bool CheckHash(byte[] buffer) | ||
{ | ||
using (var sha256 = SHA256.Create()) | ||
{ | ||
byte[] digest = sha256.ComputeHash(buffer); | ||
return DoesHashExist(digest); | ||
} | ||
} | ||
``` | ||
|
||
```vb | ||
Public Function CheckHash(buffer As Byte()) As Boolean | ||
Using sha256 As SHA256 = SHA256.Create() | ||
Dim digest As Byte() = sha256.ComputeHash(buffer) | ||
Return DoesHashExist(digest) | ||
End Using | ||
End Function | ||
``` | ||
|
||
The previous code can be changed to call the static <xref:System.Security.Cryptography.SHA256.HashData(System.Byte[])> method directly. | ||
|
||
```csharp | ||
public bool CheckHash(byte[] buffer) | ||
{ | ||
byte[] digest = SHA256.HashData(buffer); | ||
return DoesHashExist(digest); | ||
} | ||
``` | ||
|
||
```vb | ||
Public Function CheckHash(buffer As Byte()) As Boolean | ||
Dim digest As Byte() = SHA256.HashData(buffer) | ||
Return DoesHashExist(digest) | ||
End Function | ||
``` | ||
|
||
## When to suppress warnings | ||
|
||
It is safe to suppress a warning from this rule. | ||
|
||
## See also | ||
|
||
- [Performance rules](performance-warnings.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters