Skip to content

Commit

Permalink
[Core Crypto] Optimize hash (neo-project#3648)
Browse files Browse the repository at this point in the history
* optimize hash

* Update src/Neo/Cryptography/Helper.cs

Co-authored-by: nan01ab <[email protected]>

* Update src/Neo/Cryptography/Helper.cs

Co-authored-by: nan01ab <[email protected]>

* Update src/Neo/Cryptography/Helper.cs

Co-authored-by: nan01ab <[email protected]>

---------

Co-authored-by: Shargon <[email protected]>
Co-authored-by: nan01ab <[email protected]>
  • Loading branch information
3 people authored Dec 30, 2024
1 parent d55f0a1 commit 5ab7411
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Neo/Cryptography/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ public static byte[] Murmur128(this ReadOnlySpan<byte> value, uint seed)
/// </summary>
/// <param name="value">The input to compute the hash code for.</param>
/// <returns>The computed hash code.</returns>
[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
}

/// <summary>
Expand All @@ -125,22 +130,32 @@ public static byte[] Sha256(this byte[] value)
/// <param name="offset">The offset into the byte array from which to begin using data.</param>
/// <param name="count">The number of bytes in the array to use as data.</param>
/// <returns>The computed hash code.</returns>
[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
}

/// <summary>
/// Computes the hash value for the specified byte array using the sha256 algorithm.
/// </summary>
/// <param name="value">The input to compute the hash code for.</param>
/// <returns>The computed hash code.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] Sha256(this ReadOnlySpan<byte> 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;
}

Expand Down

0 comments on commit 5ab7411

Please sign in to comment.