From a90387d2f39f4d97976cdc05c6166d715e2963c9 Mon Sep 17 00:00:00 2001 From: Millicent Achieng Date: Wed, 29 Mar 2023 14:39:42 +0300 Subject: [PATCH] Make hasher static and readonly in the CryptographicExtensions --- .../Common/CryptographyExtensions.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Common/CryptographyExtensions.cs b/src/Microsoft.OpenApi.OData.Reader/Common/CryptographyExtensions.cs index 46e3dff1..de93a5cb 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Common/CryptographyExtensions.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Common/CryptographyExtensions.cs @@ -5,11 +5,14 @@ using System.Security.Cryptography; using System.Text; +using System.Threading; namespace Microsoft.OpenApi.OData.Common { internal static class CryptographyExtensions { + private static readonly ThreadLocal hasher = new (SHA256.Create); + /// /// Calculates the SHA256 hash for the given string. /// @@ -17,10 +20,9 @@ internal static class CryptographyExtensions public static string GetHashSHA256(this string input) { Utils.CheckArgumentNull(input, nameof(input)); - - var hasher = new SHA256CryptoServiceProvider(); + var inputBytes = Encoding.UTF8.GetBytes(input); - var hashBytes = hasher.ComputeHash(inputBytes); + var hashBytes = hasher.Value.ComputeHash(inputBytes); var hash = new StringBuilder(); foreach (var b in hashBytes) {