Crate rs_merkletree

source ·
Expand description

§rs-merkletree

rs-merkletree is a Rust library to create Merkle trees.It allows creation of Merkle Trees using an Vec of data. It is possible to check whether a certain hash is included in the tree. Currently, supports merklization from a Vec of &str.

§About Merkle Trees

In cryptography and computer science, a hash tree or Merkle tree is a tree in which every “leaf” node is labelled with the cryptographic hash of a data block, and every node that is not a leaf (called a branch, inner node, or inode) is labelled with the cryptographic hash of the labels of its child nodes. A hash tree allows efficient and secure verification of the contents of a large data structure.

§Examples

Create a Merkle Tree and print the Root Hash

use rs_merkletree::MerkleTree;
let data: Vec<&str> = vec!["Hello", "World", "From", "Rust"];
let mut tree = MerkleTree::new(None);
let rootNode = tree.build_tree(data);
let root_hash = rootNode.root_node().unwrap().hash();
assert_eq!(
    String::from_utf8(root_hash),
    Ok(String::from(
        "725367a8cee028cf3360c19d20c175733191562b01e60d093e81d8570e865f81"
    ))
);

Check inclusion of a hash in a Merkle Tree

use rs_merkletree::MerkleTree;
let data: Vec<&str> = vec!["Hello", "World", "From", "Rust"];
let mut tree = MerkleTree::new(None);
let rootNode = tree.build_tree(data);
let root_hash = rootNode.root_node().unwrap().hash();
assert_eq!(
    String::from_utf8(root_hash),
    Ok(String::from(
        "725367a8cee028cf3360c19d20c175733191562b01e60d093e81d8570e865f81"
    ))
);
let path = tree.includes(
"d9aa89fdd15ad5c41d9c128feffe9e07dc828b83f85296f7f42bda506821300e".as_bytes(),
);
println!("{}",path);

Structs§