public class HashTreeBuilder extends Object implements TreeBuilder<ImprintNode>
Hash tree is a tree in which every non-leaf node is labelled with the hash of the labels or values (in case of leaves) of its child nodes.
Note that HashTreeBuilder
works only with ImprintNode
objects.
Current implementation calculates the parent hash by connecting the child node
values and the parent node height before hashing.
Constructor and Description |
---|
HashTreeBuilder()
Creates a new hash tree builder with
Util.DEFAULT_AGGREGATION_ALGORITHM hash algorithm. |
HashTreeBuilder(HashAlgorithm algorithm)
Creates a new hash tree builder with given hash algorithm.
|
Modifier and Type | Method and Description |
---|---|
void |
add(ImprintNode... nodes)
Adds a new array of child nodes to the hash tree.
|
void |
add(ImprintNode node)
Adds a new single child node to the hash tree.
|
void |
add(ImprintNode node,
IdentityMetadata metadata)
Adds a new leaf with its metadata to the hash tree.
|
protected ImprintNode |
aggregate(ImprintNode left,
ImprintNode right) |
ImprintNode |
build()
Builds the hash tree and returns the root hash of the tree.
|
long |
calculateHeight(ImprintNode node)
Calculates the height of the hash tree in case a new node would be added.
|
long |
calculateHeight(ImprintNode node,
IdentityMetadata metadata)
Calculates the height of the hash tree in case a new node with metadata would be added.
|
protected ImprintNode |
getRootNode(LinkedList<ImprintNode> heads) |
public HashTreeBuilder(HashAlgorithm algorithm)
algorithm
- hash algorithm to be used to calculate tree node hashes.public HashTreeBuilder()
Util.DEFAULT_AGGREGATION_ALGORITHM
hash algorithm.public void add(ImprintNode node) throws HashException
add
in interface TreeBuilder<ImprintNode>
node
- child node to be added.HashException
public void add(ImprintNode node, IdentityMetadata metadata) throws HashException, KSIException
add
in interface TreeBuilder<ImprintNode>
node
- leaf node to be added, must not be null.metadata
- node's metadata, must not be nullHashException
KSIException
public long calculateHeight(ImprintNode node) throws HashException
calculateHeight
in interface TreeBuilder<ImprintNode>
node
- a leaf to be added to the tree, must not be null.HashException
public long calculateHeight(ImprintNode node, IdentityMetadata metadata) throws HashException, KSIException
calculateHeight
in interface TreeBuilder<ImprintNode>
node
- a leaf to be added to the tree, must not be null.metadata
- metadata associated with the node.HashException
KSIException
public void add(ImprintNode... nodes) throws HashException
add
in interface TreeBuilder<ImprintNode>
nodes
- array of nodes to be added.HashException
public ImprintNode build() throws HashException
build
in interface TreeBuilder<ImprintNode>
HashException
protected ImprintNode getRootNode(LinkedList<ImprintNode> heads)
protected ImprintNode aggregate(ImprintNode left, ImprintNode right) throws HashException
HashException
Copyright © 2024 Guardtime. All rights reserved.