Skip to content

Commit

Permalink
Add CA1850 documentation (#26634)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzchua authored Oct 29, 2021
1 parent af3a10e commit 348646a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
89 changes: 89 additions & 0 deletions docs/fundamentals/code-analysis/quality-rules/ca1850.md
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)
1 change: 1 addition & 0 deletions docs/fundamentals/code-analysis/quality-rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ The following table lists code quality analysis rules.
> | [CA1846: Prefer `AsSpan` over `Substring`](ca1846.md) | `AsSpan` is more efficient than `Substring`. `Substring` performs an O(n) string copy, while `AsSpan` does not and has a constant cost. `AsSpan` also does not perform any heap allocations. |
> | [CA1847: Use char literal for a single character lookup](ca1847.md) | Use `string.Contains(char)` instead of `string.Contains(string)` when searching for a single character. |
> | [CA1849: Call async methods when in an async method](ca1849.md) | In a method which is already asynchronous, calls to other methods should be to their async versions, where they exist. |
> | [CA1850: Prefer static `HashData` method over `ComputeHash`](ca1850.md) | It's more efficient to use the static `HashData` method over creating and managing a `HashAlgorithm` instance to call `ComputeHash`. |
> | [CA2000: Dispose objects before losing scope](ca2000.md) | Because an exceptional event might occur that will prevent the finalizer of an object from running, the object should be explicitly disposed before all references to it are out of scope. |
> |[CA2002: Do not lock on objects with weak identity](ca2002.md) |An object is said to have a weak identity when it can be directly accessed across application domain boundaries. A thread that tries to acquire a lock on an object that has a weak identity can be blocked by a second thread in a different application domain that has a lock on the same object. |
> | [CA2007: Do not directly await a Task](ca2007.md) | An asynchronous method [awaits](../../../csharp/language-reference/operators/await.md) a <xref:System.Threading.Tasks.Task> directly. When an asynchronous method awaits a <xref:System.Threading.Tasks.Task> directly, continuation occurs in the same thread that created the task. This behavior can be costly in terms of performance and can result in a deadlock on the UI thread. Consider calling <xref:System.Threading.Tasks.Task.ConfigureAwait(System.Boolean)?displayProperty=nameWithType> to signal your intention for continuation. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ Performance rules support high-performance libraries and applications.
| [CA1846: Prefer `AsSpan` over `Substring`](ca1846.md) | `AsSpan` is more efficient than `Substring`. `Substring` performs an O(n) string copy, while `AsSpan` does not and has a constant cost. `AsSpan` also does not perform any heap allocations. |
| [CA1847: Use char literal for a single character lookup](ca1847.md) | Use `string.Contains(char)` instead of `string.Contains(string)` when searching for a single character. |
| [CA1849: Call async methods when in an async method](ca1849.md) | Use `string.Contains(char)` instead of `string.Contains(string)` when searching for a single character. |
| [CA1850: Prefer static `HashData` method over `ComputeHash`](ca1850.md) | It's more efficient to use the static `HashData` method over creating and managing a `HashAlgorithm` instance to call `ComputeHash`. |
2 changes: 2 additions & 0 deletions docs/fundamentals/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,8 @@ items:
href: code-analysis/quality-rules/ca1847.md
- name: CA1849
href: code-analysis/quality-rules/ca1849.md
- name: CA1850
href: code-analysis/quality-rules/ca1850.md
- name: SingleFile rules
items:
- name: Overview
Expand Down

0 comments on commit 348646a

Please sign in to comment.