diff --git a/core/src/main/java/org/bitcoinj/core/Block.java b/core/src/main/java/org/bitcoinj/core/Block.java index 7ae08083..f1662dab 100644 --- a/core/src/main/java/org/bitcoinj/core/Block.java +++ b/core/src/main/java/org/bitcoinj/core/Block.java @@ -632,7 +632,7 @@ public class Block extends Message { int right = Math.min(left + 1, levelSize - 1); byte[] leftBytes = Utils.reverseBytes(tree.get(levelOffset + left)); byte[] rightBytes = Utils.reverseBytes(tree.get(levelOffset + right)); - tree.add(Utils.reverseBytes(hashTwice(leftBytes, 0, 32, rightBytes, 0, 32))); + tree.add(Utils.reverseBytes(hashTwice(leftBytes, rightBytes))); } // Move to the next level. levelOffset += levelSize; diff --git a/core/src/main/java/org/bitcoinj/core/PartialMerkleTree.java b/core/src/main/java/org/bitcoinj/core/PartialMerkleTree.java index 5073dbce..d1590085 100644 --- a/core/src/main/java/org/bitcoinj/core/PartialMerkleTree.java +++ b/core/src/main/java/org/bitcoinj/core/PartialMerkleTree.java @@ -215,9 +215,7 @@ public class PartialMerkleTree extends Message { } private static Sha256Hash combineLeftRight(byte[] left, byte[] right) { - return Sha256Hash.wrapReversed(Sha256Hash.hashTwice( - reverseBytes(left), 0, 32, - reverseBytes(right), 0, 32)); + return Sha256Hash.wrapReversed(Sha256Hash.hashTwice(reverseBytes(left), reverseBytes(right))); } /** diff --git a/core/src/main/java/org/bitcoinj/core/Sha256Hash.java b/core/src/main/java/org/bitcoinj/core/Sha256Hash.java index a9d4e427..a21539ba 100644 --- a/core/src/main/java/org/bitcoinj/core/Sha256Hash.java +++ b/core/src/main/java/org/bitcoinj/core/Sha256Hash.java @@ -100,6 +100,17 @@ public class Sha256Hash implements Serializable, Comparable { return wrap(hashTwice(contents)); } + /** + * Creates a new instance containing the hash of the calculated hash of the given bytes. + * + * @param content1 first bytes on which the hash value is calculated + * @param content2 second bytes on which the hash value is calculated + * @return a new instance containing the calculated (two-time) hash + */ + public static Sha256Hash twiceOf(byte[] content1, byte[] content2) { + return wrap(hashTwice(content1, content2)); + } + /** * Creates a new instance containing the calculated (one-time) hash of the given file's contents. * @@ -169,6 +180,17 @@ public class Sha256Hash implements Serializable, Comparable { return hashTwice(input, 0, input.length); } + /** + * Calculates the hash of hash on the given chunks of bytes. This is equivalent to concatenating the two + * chunks and then passing the result to {@link #hashTwice(byte[])}. + */ + public static byte[] hashTwice(byte[] input1, byte[] input2) { + MessageDigest digest = newDigest(); + digest.update(input1); + digest.update(input2); + return digest.digest(digest.digest()); + } + /** * Calculates the SHA-256 hash of the given byte range, * and then hashes the resulting hash again.