From 5ab7411998f27857fcd05c20c450401db52daed3 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 30 Dec 2024 11:08:55 +0800 Subject: [PATCH] [Core Crypto] Optimize hash (#3648) * optimize hash * Update src/Neo/Cryptography/Helper.cs Co-authored-by: nan01ab * Update src/Neo/Cryptography/Helper.cs Co-authored-by: nan01ab * Update src/Neo/Cryptography/Helper.cs Co-authored-by: nan01ab --------- Co-authored-by: Shargon Co-authored-by: nan01ab --- src/Neo/Cryptography/Helper.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Neo/Cryptography/Helper.cs b/src/Neo/Cryptography/Helper.cs index 3a89ecba75..c6a3a72ae7 100644 --- a/src/Neo/Cryptography/Helper.cs +++ b/src/Neo/Cryptography/Helper.cs @@ -112,10 +112,15 @@ public static byte[] Murmur128(this ReadOnlySpan value, uint seed) /// /// The input to compute the hash code for. /// The computed hash code. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte[] Sha256(this byte[] value) { +#if !NET5_0_OR_GREATER using var sha256 = SHA256.Create(); return sha256.ComputeHash(value); +#else + return SHA256.HashData(value); +#endif } /// @@ -125,10 +130,15 @@ public static byte[] Sha256(this byte[] value) /// The offset into the byte array from which to begin using data. /// The number of bytes in the array to use as data. /// The computed hash code. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte[] Sha256(this byte[] value, int offset, int count) { +#if !NET5_0_OR_GREATER using var sha256 = SHA256.Create(); return sha256.ComputeHash(value, offset, count); +#else + return SHA256.HashData(value.AsSpan(offset, count)); +#endif } /// @@ -136,11 +146,16 @@ public static byte[] Sha256(this byte[] value, int offset, int count) /// /// The input to compute the hash code for. /// The computed hash code. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static byte[] Sha256(this ReadOnlySpan value) { byte[] buffer = new byte[32]; +#if !NET5_0_OR_GREATER using var sha256 = SHA256.Create(); sha256.TryComputeHash(value, buffer, out _); +#else + SHA256.HashData(value, buffer); +#endif return buffer; }