From 42c48232905e786a711e33148cbbe485803d63fc Mon Sep 17 00:00:00 2001 From: Gildas Le Bournault Date: Fri, 13 Dec 2024 10:57:00 +0100 Subject: [PATCH] refactor: optimize SHA256 hash generation in HashUtils --- CodeLineCounter/Utils/HashUtils.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/CodeLineCounter/Utils/HashUtils.cs b/CodeLineCounter/Utils/HashUtils.cs index 14942b7..59349d7 100644 --- a/CodeLineCounter/Utils/HashUtils.cs +++ b/CodeLineCounter/Utils/HashUtils.cs @@ -11,13 +11,17 @@ public static string ComputeHash(string? input) { return ""; } + byte[] bytes = SHA256.HashData(Encoding.UTF8.GetBytes(input)); - StringBuilder builder = new(); - for (int i = 0; i < bytes.Length; i++) + + return string.Create(bytes.Length * 2, bytes, static (span, byteArray) => { - builder.Append(bytes[i].ToString("x2")); - } - return builder.ToString(); + const string format = "x2"; + for (int i = 0; i < byteArray.Length; i++) + { + byteArray[i].TryFormat(span.Slice(i * 2, 2), out _, format); + } + }); } } }