From 6dc717f3de5e5e5d88377d971b48b49b7b70182d Mon Sep 17 00:00:00 2001 From: catbref Date: Tue, 14 Apr 2020 15:06:51 +0100 Subject: [PATCH] Store/use MSB of B for RMD160/HASH160 function codes. Previously used LSB of B but wasn't updated since switch to big-endian. --- .../src/main/java/org/ciyam/at/FunctionCode.java | 16 ++++++++-------- .../org/ciyam/at/HashingFunctionCodeTests.java | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Java/src/main/java/org/ciyam/at/FunctionCode.java b/Java/src/main/java/org/ciyam/at/FunctionCode.java index 73b9acc..56ce9af 100644 --- a/Java/src/main/java/org/ciyam/at/FunctionCode.java +++ b/Java/src/main/java/org/ciyam/at/FunctionCode.java @@ -593,9 +593,9 @@ public enum FunctionCode { ByteBuffer digestByteBuffer = ByteBuffer.wrap(digest); - state.b1 = (long) digestByteBuffer.getInt() & 0xffffffffL; + state.b1 = digestByteBuffer.getLong(); state.b2 = digestByteBuffer.getLong(); - state.b3 = digestByteBuffer.getLong(); + state.b3 = ((long) digestByteBuffer.getInt()) << 32; // MSB of B3 state.b4 = 0L; } catch (NoSuchAlgorithmException e) { throw new ExecutionException("No RIPEMD160 message digest service available", e); @@ -619,9 +619,9 @@ public enum FunctionCode { ByteBuffer digestByteBuffer = ByteBuffer.allocate(digester.getDigestLength()); - digestByteBuffer.putInt((int) (state.b1 & 0xffffffffL)); + digestByteBuffer.putLong(state.b1); digestByteBuffer.putLong(state.b2); - digestByteBuffer.putLong(state.b3); + digestByteBuffer.putInt((int) (state.b3 >>> 32)); // MSB of B3 // NOTE: b4 ignored byte[] expectedDigest = digestByteBuffer.array(); @@ -713,9 +713,9 @@ public enum FunctionCode { ByteBuffer digestByteBuffer = ByteBuffer.wrap(rmd160Digest); - state.b1 = (long) digestByteBuffer.getInt() & 0xffffffffL; + state.b1 = digestByteBuffer.getLong(); state.b2 = digestByteBuffer.getLong(); - state.b3 = digestByteBuffer.getLong(); + state.b3 = ((long) digestByteBuffer.getInt()) << 32; // MSB of B3 state.b4 = 0L; } catch (NoSuchAlgorithmException e) { throw new ExecutionException("No SHA-256 or RIPEMD160 message digest service available", e); @@ -742,9 +742,9 @@ public enum FunctionCode { ByteBuffer digestByteBuffer = ByteBuffer.allocate(rmd160Digester.getDigestLength()); - digestByteBuffer.putInt((int) (state.b1 & 0xffffffffL)); + digestByteBuffer.putLong(state.b1); digestByteBuffer.putLong(state.b2); - digestByteBuffer.putLong(state.b3); + digestByteBuffer.putInt((int) (state.b3 >>> 32)); // MSB of B3 // NOTE: b4 ignored byte[] expectedDigest = digestByteBuffer.array(); diff --git a/Java/src/test/java/org/ciyam/at/HashingFunctionCodeTests.java b/Java/src/test/java/org/ciyam/at/HashingFunctionCodeTests.java index be6e277..c6c9ba6 100644 --- a/Java/src/test/java/org/ciyam/at/HashingFunctionCodeTests.java +++ b/Java/src/test/java/org/ciyam/at/HashingFunctionCodeTests.java @@ -131,15 +131,15 @@ public class HashingFunctionCodeTests extends ExecutableTest { int numLongs = (expected.length() + 15) / 16; for (int longIndex = 0; longIndex < numLongs; ++longIndex) { - final int endIndex = expected.length() - (numLongs - longIndex - 1) * 16; - final int beginIndex = Math.max(0, endIndex - 16); + final int beginIndex = longIndex * 16; + final int endIndex = Math.min(expected.length(), beginIndex + 16); String hexChars = expected.substring(beginIndex, endIndex); codeByteBuffer.put(OpCode.SET_VAL.value); codeByteBuffer.putInt(0); // addr 0 - codeByteBuffer.put(new byte[8 - hexChars.length() / 2]); // pad LSB with zeros codeByteBuffer.put(hexToBytes(hexChars)); + codeByteBuffer.put(new byte[8 - hexChars.length() / 2]); // pad LSB with zeros final FunctionCode bSettingFunction = bSettingFunctions[longIndex]; codeByteBuffer.put(OpCode.EXT_FUN_DAT.value).putShort(bSettingFunction.value).putInt(0);