From a60fe48720b967a93c821e4f7865d731a2400937 Mon Sep 17 00:00:00 2001 From: jamesm Date: Tue, 29 Oct 2024 17:09:57 -0400 Subject: [PATCH] Sha256 can just be a helper method on SolanaProgramDerivedAddress - it doesn't need to be it's own class --- .../encoding/SolanaProgramDerivedAddress.java | 19 ++++++++- .../java/com/lmax/solana4j/util/Sha256.java | 40 ------------------- .../com/lmax/solana4j/util/Sha256Test.java | 25 ------------ 3 files changed, 17 insertions(+), 67 deletions(-) delete mode 100644 message-encoding/src/main/java/com/lmax/solana4j/util/Sha256.java delete mode 100644 message-encoding/src/test/java/com/lmax/solana4j/util/Sha256Test.java diff --git a/message-encoding/src/main/java/com/lmax/solana4j/encoding/SolanaProgramDerivedAddress.java b/message-encoding/src/main/java/com/lmax/solana4j/encoding/SolanaProgramDerivedAddress.java index 2cf1ec2c..0b427012 100644 --- a/message-encoding/src/main/java/com/lmax/solana4j/encoding/SolanaProgramDerivedAddress.java +++ b/message-encoding/src/main/java/com/lmax/solana4j/encoding/SolanaProgramDerivedAddress.java @@ -3,10 +3,11 @@ import com.lmax.solana4j.api.ProgramDerivedAddress; import com.lmax.solana4j.api.PublicKey; import com.lmax.solana4j.util.Ed25519; -import com.lmax.solana4j.util.Sha256; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.List; import java.util.Objects; @@ -37,7 +38,7 @@ static ProgramDerivedAddress deriveProgramAddress(final List seeds, fina programId.write(seedsBuffer); seedsBuffer.put(PROGRAM_DERIVED_ADDRESS_BYTES); - final byte[] programAddress = Sha256.hash(seedsBuffer.array()); + final byte[] programAddress = hash(seedsBuffer.array()); if (isOffCurve(programAddress)) { @@ -48,6 +49,20 @@ static ProgramDerivedAddress deriveProgramAddress(final List seeds, fina throw new RuntimeException("Could not find a program address off the curve."); } + private static byte[] hash(final byte[] input) + { + final MessageDigest digest; + try + { + digest = MessageDigest.getInstance("SHA-256"); + } + catch (final NoSuchAlgorithmException e) + { + throw new RuntimeException(e); + } + return digest.digest(input); + } + private static boolean isOffCurve(final byte[] programAddress) { try diff --git a/message-encoding/src/main/java/com/lmax/solana4j/util/Sha256.java b/message-encoding/src/main/java/com/lmax/solana4j/util/Sha256.java deleted file mode 100644 index 3642ecb4..00000000 --- a/message-encoding/src/main/java/com/lmax/solana4j/util/Sha256.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.lmax.solana4j.util; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * Utility class for generating SHA-256 hashes. - *

- * This class provides a method to generate a SHA-256 hash from a given byte array. - * SHA-256 is a cryptographic hash function that produces a 256-bit (32-byte) hash value. - * It's commonly used for data integrity and security purposes. - *

- */ -public final class Sha256 -{ - private Sha256() - { - } - - /** - * Computes the SHA-256 hash of the input byte array. - * - * @param input the byte array to be hashed - * @return a byte array containing the SHA-256 hash of the input - * @throws RuntimeException if the SHA-256 algorithm is not available - */ - public static byte[] hash(final byte[] input) - { - final MessageDigest digest; - try - { - digest = MessageDigest.getInstance("SHA-256"); - } - catch (final NoSuchAlgorithmException e) - { - throw new RuntimeException(e); - } - return digest.digest(input); - } -} diff --git a/message-encoding/src/test/java/com/lmax/solana4j/util/Sha256Test.java b/message-encoding/src/test/java/com/lmax/solana4j/util/Sha256Test.java deleted file mode 100644 index 4150b319..00000000 --- a/message-encoding/src/test/java/com/lmax/solana4j/util/Sha256Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.lmax.solana4j.util; - -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -class Sha256Test -{ - @Test - void shouldHashToExpectedValue() - { - final var sample = new byte[]{10, 20, 30, 40, 50, 60, 70, 80, 90}; - final var hashSample = new byte[] - { - -91, 31, -102, 11, 110, -93, 82, 58, -49, 70, - -105, -84, 116, -4, -114, 54, 83, -82, -116, -66, - 26, -65, 88, -83, -1, -80, 105, -31, -11, -24, - -62, -27 - }; - - final byte[] hash = Sha256.hash(sample); - - assertThat(hash).containsExactly(hashSample); - } -} \ No newline at end of file