1 unstable release
0.1.0 | Aug 12, 2024 |
---|
#1978 in Cryptography
18KB
209 lines
rs-merkletree
A Rust Library to generate Merkle Trees.
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.
Hash trees can be used to verify any kind of data stored, handled and transferred in and between computers. They can help ensure that data blocks received from other peers in a peer-to-peer network are received undamaged and unaltered, and even to check that the other peers do not lie and send fake blocks.
Hash trees are used in:
- Hash-based Cryptography
- InterPlanetary File System (IPFS)
- BitTorrent
- Btrfs and ZFS file systems
- Dat protocol
- Apache Wave protocol
- Zeronet
- Bitcoin and Ethereum peer-to-peer networks
Usage
Add the following to your cargo.toml
to start using rs-merkletree
[dependencies]
rs-merkletree = "0.1.0"
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"
))
);
For more examples, check out the official docs or the tests
folder
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Dependencies
~4MB
~52K SLoC