From 5e2467c69f3afd3c4663d892107267355d8fa257 Mon Sep 17 00:00:00 2001 From: Feitulla <113803928+OrionVerdikhanov@users.noreply.github.com> Date: Wed, 4 Jun 2025 23:38:48 +0500 Subject: [PATCH 1/2] feat(core): add BLAKE2b hashing --- CHANGELOG.md | 1 + core/README.md | 3 +- .../main/java/org/nem/core/crypto/Hashes.java | 17 ++++- .../java/org/nem/core/crypto/HashesTest.java | 63 +++++++++++++++---- 4 files changed, 69 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68ab6412b4..1d92ac6c38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. The changelog format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## next +- add BLAKE2b-256 hashing support ## [0.6.102] - 01-Apr-2025 diff --git a/core/README.md b/core/README.md index e0b47c7d85..2a9907a545 100644 --- a/core/README.md +++ b/core/README.md @@ -2,7 +2,8 @@ [![Build Status](https://travis-ci.org/NemProject/nem.core.svg?branch=dev)](https://travis-ci.org/NemProject/nem.core) -This Java package provides the cryptographic and serialization base methods used by [NEM](https://nemproject.github.io/nem-docs) nodes. To deploy a complete node please examine the [build script](../infra/docker). +This Java package provides the cryptographic and serialization base methods used by [NEM](https://nemproject.github.io/nem-docs) nodes. To deploy a complete node please examine the [build script](../infra/docker). +It now includes support for the modern BLAKE2b-256 hashing algorithm. ## Package Organization diff --git a/core/src/main/java/org/nem/core/crypto/Hashes.java b/core/src/main/java/org/nem/core/crypto/Hashes.java index c31bcc81ee..0879085526 100644 --- a/core/src/main/java/org/nem/core/crypto/Hashes.java +++ b/core/src/main/java/org/nem/core/crypto/Hashes.java @@ -42,9 +42,20 @@ public static byte[] sha3_512(final byte[]... inputs) { * @return The hash of the concatenated inputs. * @throws CryptoException if the hash operation failed. */ - public static byte[] ripemd160(final byte[]... inputs) { - return hash("RIPEMD160", inputs); - } + public static byte[] ripemd160(final byte[]... inputs) { + return hash("RIPEMD160", inputs); + } + + /** + * Performs a BLAKE2b-256 hash of the concatenated inputs. + * + * @param inputs The byte arrays to concatenate and hash. + * @return The hash of the concatenated inputs. + * @throws CryptoException if the hash operation failed. + */ + public static byte[] blake2b_256(final byte[]... inputs) { + return hash("BLAKE2B-256", inputs); + } private static byte[] hash(final String algorithm, final byte[]... inputs) { return ExceptionUtils.propagate(() -> { diff --git a/core/src/test/java/org/nem/core/crypto/HashesTest.java b/core/src/test/java/org/nem/core/crypto/HashesTest.java index a1ffbf4d4e..8fe42c36e2 100644 --- a/core/src/test/java/org/nem/core/crypto/HashesTest.java +++ b/core/src/test/java/org/nem/core/crypto/HashesTest.java @@ -10,7 +10,8 @@ public class HashesTest { private static final HashTester SHA3_256_TESTER = new HashTester(Hashes::sha3_256, 32); private static final HashTester SHA3_512_TESTER = new HashTester(Hashes::sha3_512, 64); - private static final HashTester RIPEMD160_TESTER = new HashTester(Hashes::ripemd160, 20); + private static final HashTester RIPEMD160_TESTER = new HashTester(Hashes::ripemd160, 20); + private static final HashTester BLAKE2B_256_TESTER = new HashTester(Hashes::blake2b_256, 32); // region sha3_256 @@ -89,12 +90,40 @@ public void ripemd160GeneratesSameHashForSameMergedInputs() { } @Test - public void ripemd160GeneratesDifferentHashForDifferentInputs() { - // Assert: - RIPEMD160_TESTER.assertHashIsDifferentForDifferentInputs(); - } + public void ripemd160GeneratesDifferentHashForDifferentInputs() { + // Assert: + RIPEMD160_TESTER.assertHashIsDifferentForDifferentInputs(); + } - // endregion + // endregion + + // region blake2b_256 + + @Test + public void blake2b_256HashHasExpectedByteLength() { + // Assert: + BLAKE2B_256_TESTER.assertHashHasExpectedLength(); + } + + @Test + public void blake2b_256GeneratesSameHashForSameInputs() { + // Assert: + BLAKE2B_256_TESTER.assertHashIsSameForSameInputs(); + } + + @Test + public void blake2b_256GeneratesSameHashForSameMergedInputs() { + // Assert: + BLAKE2B_256_TESTER.assertHashIsSameForSplitInputs(); + } + + @Test + public void blake2b_256GeneratesDifferentHashForDifferentInputs() { + // Assert: + BLAKE2B_256_TESTER.assertHashIsDifferentForDifferentInputs(); + } + + // endregion // region different hash algorithm @@ -110,11 +139,23 @@ public void sha3_256AndSha3_512GenerateDifferentHashForSameInputs() { assertHashesAreDifferent(Hashes::sha3_256, Hashes::sha3_512); } - @Test - public void sha3_512AndRipemd160GenerateDifferentHashForSameInputs() { - // Assert: - assertHashesAreDifferent(Hashes::sha3_512, Hashes::ripemd160); - } + @Test + public void sha3_512AndRipemd160GenerateDifferentHashForSameInputs() { + // Assert: + assertHashesAreDifferent(Hashes::sha3_512, Hashes::ripemd160); + } + + @Test + public void blake2b_256AndSha3_256GenerateDifferentHashForSameInputs() { + // Assert: + assertHashesAreDifferent(Hashes::blake2b_256, Hashes::sha3_256); + } + + @Test + public void blake2b_256AndRipemd160GenerateDifferentHashForSameInputs() { + // Assert: + assertHashesAreDifferent(Hashes::blake2b_256, Hashes::ripemd160); + } private static void assertHashesAreDifferent(final Function hashFunction1, final Function hashFunction2) { From faaa6e1d199ba3325c9a4d10ef961ec4f2e58479 Mon Sep 17 00:00:00 2001 From: Feitulla <113803928+OrionVerdikhanov@users.noreply.github.com> Date: Wed, 4 Jun 2025 23:50:19 +0500 Subject: [PATCH 2/2] Revert "Add BLAKE2b hashing support" --- CHANGELOG.md | 1 - core/README.md | 3 +- .../main/java/org/nem/core/crypto/Hashes.java | 17 +---- .../java/org/nem/core/crypto/HashesTest.java | 63 ++++--------------- 4 files changed, 15 insertions(+), 69 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d92ac6c38..68ab6412b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,6 @@ All notable changes to this project will be documented in this file. The changelog format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## next -- add BLAKE2b-256 hashing support ## [0.6.102] - 01-Apr-2025 diff --git a/core/README.md b/core/README.md index 2a9907a545..e0b47c7d85 100644 --- a/core/README.md +++ b/core/README.md @@ -2,8 +2,7 @@ [![Build Status](https://travis-ci.org/NemProject/nem.core.svg?branch=dev)](https://travis-ci.org/NemProject/nem.core) -This Java package provides the cryptographic and serialization base methods used by [NEM](https://nemproject.github.io/nem-docs) nodes. To deploy a complete node please examine the [build script](../infra/docker). -It now includes support for the modern BLAKE2b-256 hashing algorithm. +This Java package provides the cryptographic and serialization base methods used by [NEM](https://nemproject.github.io/nem-docs) nodes. To deploy a complete node please examine the [build script](../infra/docker). ## Package Organization diff --git a/core/src/main/java/org/nem/core/crypto/Hashes.java b/core/src/main/java/org/nem/core/crypto/Hashes.java index 0879085526..c31bcc81ee 100644 --- a/core/src/main/java/org/nem/core/crypto/Hashes.java +++ b/core/src/main/java/org/nem/core/crypto/Hashes.java @@ -42,20 +42,9 @@ public static byte[] sha3_512(final byte[]... inputs) { * @return The hash of the concatenated inputs. * @throws CryptoException if the hash operation failed. */ - public static byte[] ripemd160(final byte[]... inputs) { - return hash("RIPEMD160", inputs); - } - - /** - * Performs a BLAKE2b-256 hash of the concatenated inputs. - * - * @param inputs The byte arrays to concatenate and hash. - * @return The hash of the concatenated inputs. - * @throws CryptoException if the hash operation failed. - */ - public static byte[] blake2b_256(final byte[]... inputs) { - return hash("BLAKE2B-256", inputs); - } + public static byte[] ripemd160(final byte[]... inputs) { + return hash("RIPEMD160", inputs); + } private static byte[] hash(final String algorithm, final byte[]... inputs) { return ExceptionUtils.propagate(() -> { diff --git a/core/src/test/java/org/nem/core/crypto/HashesTest.java b/core/src/test/java/org/nem/core/crypto/HashesTest.java index 8fe42c36e2..a1ffbf4d4e 100644 --- a/core/src/test/java/org/nem/core/crypto/HashesTest.java +++ b/core/src/test/java/org/nem/core/crypto/HashesTest.java @@ -10,8 +10,7 @@ public class HashesTest { private static final HashTester SHA3_256_TESTER = new HashTester(Hashes::sha3_256, 32); private static final HashTester SHA3_512_TESTER = new HashTester(Hashes::sha3_512, 64); - private static final HashTester RIPEMD160_TESTER = new HashTester(Hashes::ripemd160, 20); - private static final HashTester BLAKE2B_256_TESTER = new HashTester(Hashes::blake2b_256, 32); + private static final HashTester RIPEMD160_TESTER = new HashTester(Hashes::ripemd160, 20); // region sha3_256 @@ -90,40 +89,12 @@ public void ripemd160GeneratesSameHashForSameMergedInputs() { } @Test - public void ripemd160GeneratesDifferentHashForDifferentInputs() { - // Assert: - RIPEMD160_TESTER.assertHashIsDifferentForDifferentInputs(); - } - - // endregion - - // region blake2b_256 - - @Test - public void blake2b_256HashHasExpectedByteLength() { - // Assert: - BLAKE2B_256_TESTER.assertHashHasExpectedLength(); - } - - @Test - public void blake2b_256GeneratesSameHashForSameInputs() { - // Assert: - BLAKE2B_256_TESTER.assertHashIsSameForSameInputs(); - } - - @Test - public void blake2b_256GeneratesSameHashForSameMergedInputs() { - // Assert: - BLAKE2B_256_TESTER.assertHashIsSameForSplitInputs(); - } - - @Test - public void blake2b_256GeneratesDifferentHashForDifferentInputs() { - // Assert: - BLAKE2B_256_TESTER.assertHashIsDifferentForDifferentInputs(); - } + public void ripemd160GeneratesDifferentHashForDifferentInputs() { + // Assert: + RIPEMD160_TESTER.assertHashIsDifferentForDifferentInputs(); + } - // endregion + // endregion // region different hash algorithm @@ -139,23 +110,11 @@ public void sha3_256AndSha3_512GenerateDifferentHashForSameInputs() { assertHashesAreDifferent(Hashes::sha3_256, Hashes::sha3_512); } - @Test - public void sha3_512AndRipemd160GenerateDifferentHashForSameInputs() { - // Assert: - assertHashesAreDifferent(Hashes::sha3_512, Hashes::ripemd160); - } - - @Test - public void blake2b_256AndSha3_256GenerateDifferentHashForSameInputs() { - // Assert: - assertHashesAreDifferent(Hashes::blake2b_256, Hashes::sha3_256); - } - - @Test - public void blake2b_256AndRipemd160GenerateDifferentHashForSameInputs() { - // Assert: - assertHashesAreDifferent(Hashes::blake2b_256, Hashes::ripemd160); - } + @Test + public void sha3_512AndRipemd160GenerateDifferentHashForSameInputs() { + // Assert: + assertHashesAreDifferent(Hashes::sha3_512, Hashes::ripemd160); + } private static void assertHashesAreDifferent(final Function hashFunction1, final Function hashFunction2) {