org.bouncycastle.tsp.ers.BinaryTreeRootCalculator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcpkix-jdk15on Show documentation
Show all versions of bcpkix-jdk15on Show documentation
The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. This jar contains APIs for JDK 1.5 to JDK 1.8. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.
The newest version!
package org.bouncycastle.tsp.ers;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.asn1.tsp.PartialHashtree;
import org.bouncycastle.operator.DigestCalculator;
/**
* Calculator based on the use of a left weighted binary Merkle tree created
* on top of the partial hash tree objects provided.
*/
public class BinaryTreeRootCalculator
implements ERSRootNodeCalculator
{
public byte[] computeRootHash(DigestCalculator digCalc, PartialHashtree[] nodes)
{
List hashes = new ArrayList();
for (int i = 0; i <= nodes.length - 2; i += 2)
{
byte[] left = ERSUtil.computeNodeHash(digCalc, nodes[i]);
byte[] right = ERSUtil.computeNodeHash(digCalc, nodes[i + 1]);
hashes.add(ERSUtil.calculateBranchHash(digCalc, left, right));
}
if (nodes.length % 2 == 1)
{
hashes.add(ERSUtil.computeNodeHash(digCalc, nodes[nodes.length - 1]));
}
do
{
List newHashes = new ArrayList((hashes.size() + 1) / 2);
for (int i = 0; i <= hashes.size() - 2; i += 2)
{
newHashes.add(ERSUtil.calculateBranchHash(digCalc, (byte[])hashes.get(i), (byte[])hashes.get(i + 1)));
}
if (hashes.size() % 2 == 1)
{
newHashes.add(hashes.get(hashes.size() - 1));
}
hashes = newHashes;
}
while (hashes.size() > 1);
return (byte[])hashes.get(0);
}
}