From 29d1f86b140939880100506555f56dfc3e1a1b0b Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Wed, 8 Jan 2025 10:56:37 +0100 Subject: [PATCH 1/3] Close https://github.com/neo-project/neo/issues/1760 --- src/Neo/Cryptography/Helper.cs | 9 +++++++-- src/Neo/Cryptography/RIPEMD160Managed.cs | 1 - 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Neo/Cryptography/Helper.cs b/src/Neo/Cryptography/Helper.cs index c6a3a72ae7..503fde8ae7 100644 --- a/src/Neo/Cryptography/Helper.cs +++ b/src/Neo/Cryptography/Helper.cs @@ -51,8 +51,13 @@ public static byte[] RIPEMD160(this byte[] value) /// The computed hash code. public static byte[] RIPEMD160(this ReadOnlySpan value) { - byte[] source = value.ToArray(); - return source.RIPEMD160(); + using var ripemd160 = new RIPEMD160Managed(); + + var output = new Span(new byte[ripemd160.HashSize]); + if (!ripemd160.TryComputeHash(value, output, out _)) + throw new Exception(); + + return output.ToArray(); } /// diff --git a/src/Neo/Cryptography/RIPEMD160Managed.cs b/src/Neo/Cryptography/RIPEMD160Managed.cs index 19a406d20f..4175aefffc 100644 --- a/src/Neo/Cryptography/RIPEMD160Managed.cs +++ b/src/Neo/Cryptography/RIPEMD160Managed.cs @@ -12,7 +12,6 @@ using System; using System.Runtime.InteropServices; using System.Security; -using System.Security.Cryptography; namespace Neo.Cryptography { From 77eb24fcc2f0f316c470a851cf87fb1d12636824 Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 8 Jan 2025 14:42:52 +0100 Subject: [PATCH 2/3] Update src/Neo/Cryptography/Helper.cs Co-authored-by: nan01ab --- src/Neo/Cryptography/Helper.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Neo/Cryptography/Helper.cs b/src/Neo/Cryptography/Helper.cs index 503fde8ae7..36a0927884 100644 --- a/src/Neo/Cryptography/Helper.cs +++ b/src/Neo/Cryptography/Helper.cs @@ -53,11 +53,10 @@ public static byte[] RIPEMD160(this ReadOnlySpan value) { using var ripemd160 = new RIPEMD160Managed(); - var output = new Span(new byte[ripemd160.HashSize]); - if (!ripemd160.TryComputeHash(value, output, out _)) + var output = new byte[ripemd160.HashSize / 8]; + if (!ripemd160.TryComputeHash(value, output.AsSpan(), out _)) throw new Exception(); - - return output.ToArray(); + return output; } /// From 8ccdc6952fa1f5b9e858714dae6034cb39df87d6 Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 11 Jan 2025 22:21:13 +0100 Subject: [PATCH 3/3] Update src/Neo/Cryptography/Helper.cs Co-authored-by: Christopher Schuchardt --- src/Neo/Cryptography/Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Neo/Cryptography/Helper.cs b/src/Neo/Cryptography/Helper.cs index 36a0927884..08671380df 100644 --- a/src/Neo/Cryptography/Helper.cs +++ b/src/Neo/Cryptography/Helper.cs @@ -55,7 +55,7 @@ public static byte[] RIPEMD160(this ReadOnlySpan value) var output = new byte[ripemd160.HashSize / 8]; if (!ripemd160.TryComputeHash(value, output.AsSpan(), out _)) - throw new Exception(); + throw new CryptographicException(); return output; }