3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-13 02:35:52 +00:00

Sha256Hash: Add twiceOf() and hashTwice() variants that simply take two byte arrays.

This commit is contained in:
Andreas Schildbach 2019-02-22 01:10:10 +01:00
parent 5749e3796d
commit 533487b97f
3 changed files with 24 additions and 4 deletions

View File

@ -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;

View File

@ -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)));
}
/**

View File

@ -100,6 +100,17 @@ public class Sha256Hash implements Serializable, Comparable<Sha256Hash> {
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<Sha256Hash> {
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.