Skip to content

Commit 654c926

Browse files
authored
Merge branch 'master' into mem-pool-improve
2 parents 3327801 + 7fac685 commit 654c926

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

src/Neo/Cryptography/Crypto.cs

+2
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ public static byte[] GetMessageHash(byte[] message, HashAlgorithm hashAlgorithm
233233
return hashAlgorithm switch
234234
{
235235
HashAlgorithm.SHA256 => message.Sha256(),
236+
HashAlgorithm.SHA512 => message.Sha512(),
236237
HashAlgorithm.Keccak256 => message.Keccak256(),
237238
_ => throw new NotSupportedException(nameof(hashAlgorithm))
238239
};
@@ -249,6 +250,7 @@ public static byte[] GetMessageHash(ReadOnlySpan<byte> message, HashAlgorithm ha
249250
return hashAlgorithm switch
250251
{
251252
HashAlgorithm.SHA256 => message.Sha256(),
253+
HashAlgorithm.SHA512 => message.Sha512(),
252254
HashAlgorithm.Keccak256 => message.Keccak256(),
253255
_ => throw new NotSupportedException(nameof(hashAlgorithm))
254256
};

src/Neo/Cryptography/HashAlgorithm.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public enum HashAlgorithm : byte
2121
/// <summary>
2222
/// The Keccak256 hash algorithm.
2323
/// </summary>
24-
Keccak256 = 0x01
24+
Keccak256 = 0x01,
25+
26+
/// <summary>
27+
/// The SHA512 hash algorithm.
28+
/// </summary>
29+
SHA512 = 0x02
2530
}
2631
}

src/Neo/Cryptography/Helper.cs

+63-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@ public static byte[] Sha256(this byte[] value)
122122
#endif
123123
}
124124

125+
/// <summary>
126+
/// Computes the hash value for the specified byte array using the sha512 algorithm.
127+
/// </summary>
128+
/// <param name="value">The input to compute the hash code for.</param>
129+
/// <returns>The computed hash code.</returns>
130+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
131+
public static byte[] Sha512(this byte[] value)
132+
{
133+
#if !NET5_0_OR_GREATER
134+
using var sha512 = SHA512.Create();
135+
return sha512.ComputeHash(value);
136+
#else
137+
return SHA512.HashData(value);
138+
#endif
139+
}
140+
125141
/// <summary>
126142
/// Computes the hash value for the specified region of the specified byte array using the sha256 algorithm.
127143
/// </summary>
@@ -140,6 +156,24 @@ public static byte[] Sha256(this byte[] value, int offset, int count)
140156
#endif
141157
}
142158

159+
/// <summary>
160+
/// Computes the hash value for the specified region of the specified byte array using the sha512 algorithm.
161+
/// </summary>
162+
/// <param name="value">The input to compute the hash code for.</param>
163+
/// <param name="offset">The offset into the byte array from which to begin using data.</param>
164+
/// <param name="count">The number of bytes in the array to use as data.</param>
165+
/// <returns>The computed hash code.</returns>
166+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
167+
public static byte[] Sha512(this byte[] value, int offset, int count)
168+
{
169+
#if !NET5_0_OR_GREATER
170+
using var sha512 = SHA512.Create();
171+
return sha512.ComputeHash(value, offset, count);
172+
#else
173+
return SHA512.HashData(value.AsSpan(offset, count));
174+
#endif
175+
}
176+
143177
/// <summary>
144178
/// Computes the hash value for the specified byte array using the sha256 algorithm.
145179
/// </summary>
@@ -148,7 +182,7 @@ public static byte[] Sha256(this byte[] value, int offset, int count)
148182
[MethodImpl(MethodImplOptions.AggressiveInlining)]
149183
public static byte[] Sha256(this ReadOnlySpan<byte> value)
150184
{
151-
byte[] buffer = new byte[32];
185+
var buffer = new byte[32];
152186
#if !NET5_0_OR_GREATER
153187
using var sha256 = SHA256.Create();
154188
sha256.TryComputeHash(value, buffer, out _);
@@ -158,6 +192,24 @@ public static byte[] Sha256(this ReadOnlySpan<byte> value)
158192
return buffer;
159193
}
160194

195+
/// <summary>
196+
/// Computes the hash value for the specified byte array using the sha512 algorithm.
197+
/// </summary>
198+
/// <param name="value">The input to compute the hash code for.</param>
199+
/// <returns>The computed hash code.</returns>
200+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
201+
public static byte[] Sha512(this ReadOnlySpan<byte> value)
202+
{
203+
var buffer = new byte[64];
204+
#if !NET5_0_OR_GREATER
205+
using var sha512 = SHA512.Create();
206+
sha512.TryComputeHash(value, buffer, out _);
207+
#else
208+
SHA512.HashData(value, buffer);
209+
#endif
210+
return buffer;
211+
}
212+
161213
/// <summary>
162214
/// Computes the hash value for the specified byte array using the sha256 algorithm.
163215
/// </summary>
@@ -168,6 +220,16 @@ public static byte[] Sha256(this Span<byte> value)
168220
return Sha256((ReadOnlySpan<byte>)value);
169221
}
170222

223+
/// <summary>
224+
/// Computes the hash value for the specified byte array using the sha512 algorithm.
225+
/// </summary>
226+
/// <param name="value">The input to compute the hash code for.</param>
227+
/// <returns>The computed hash code.</returns>
228+
public static byte[] Sha512(this Span<byte> value)
229+
{
230+
return Sha512((ReadOnlySpan<byte>)value);
231+
}
232+
171233
/// <summary>
172234
/// Computes the hash value for the specified byte array using the keccak256 algorithm.
173235
/// </summary>

tests/Neo.UnitTests/Cryptography/UT_Cryptography_Helper.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,25 @@ public void TestMurmurReadOnlySpan()
5757
[TestMethod]
5858
public void TestSha256()
5959
{
60-
byte[] value = Encoding.ASCII.GetBytes("hello world");
61-
byte[] result = value.Sha256(0, value.Length);
60+
var value = Encoding.ASCII.GetBytes("hello world");
61+
var result = value.Sha256(0, value.Length);
6262
Assert.AreEqual("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", result.ToHexString());
6363
CollectionAssert.AreEqual(result, value.Sha256());
6464
CollectionAssert.AreEqual(result, ((Span<byte>)value).Sha256());
6565
CollectionAssert.AreEqual(result, ((ReadOnlySpan<byte>)value).Sha256());
6666
}
6767

68+
[TestMethod]
69+
public void TestSha512()
70+
{
71+
var value = Encoding.ASCII.GetBytes("hello world");
72+
var result = value.Sha512(0, value.Length);
73+
Assert.AreEqual("309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f", result.ToHexString());
74+
CollectionAssert.AreEqual(result, value.Sha512());
75+
CollectionAssert.AreEqual(result, ((Span<byte>)value).Sha512());
76+
CollectionAssert.AreEqual(result, ((ReadOnlySpan<byte>)value).Sha512());
77+
}
78+
6879
[TestMethod]
6980
public void TestKeccak256()
7081
{

0 commit comments

Comments
 (0)