Skip to content

Commit 985cb7d

Browse files
authored
Merge pull request #17 from magic5644:feat-upgrade-performance
refactor: optimize SHA256 hash generation in HashUtils
2 parents 3608f1e + 42c4823 commit 985cb7d

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

CodeLineCounter/Utils/HashUtils.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ public static string ComputeHash(string? input)
1111
{
1212
return "";
1313
}
14+
1415
byte[] bytes = SHA256.HashData(Encoding.UTF8.GetBytes(input));
15-
StringBuilder builder = new();
16-
for (int i = 0; i < bytes.Length; i++)
16+
17+
return string.Create(bytes.Length * 2, bytes, static (span, byteArray) =>
1718
{
18-
builder.Append(bytes[i].ToString("x2"));
19-
}
20-
return builder.ToString();
19+
const string format = "x2";
20+
for (int i = 0; i < byteArray.Length; i++)
21+
{
22+
byteArray[i].TryFormat(span.Slice(i * 2, 2), out _, format);
23+
}
24+
});
2125
}
2226
}
2327
}

0 commit comments

Comments
 (0)