#merkle-tree #merkle #incoming #data #generate #node #hash

merkle-generator

generate a merkle tree based on incoming data

4 releases

Uses old Rust 2015

0.2.0 May 9, 2017
0.1.2 Jan 19, 2017
0.1.1 Jan 19, 2017
0.1.0 Jan 19, 2017

#9 in #incoming

MIT license

6KB
126 lines

merkle-generator

Rust version of mafintosh/merkle-tree-stream. Generate a merkle tree based on incoming data.

Cargo Version

merkle-generator = "0.1.2"

Usage

extern crate merkle_generator;

// define how to hash incoming data
fn parent(a: &Node, b: &Node) -> Vec<u8> {
    let ref mut hash = a.hash.clone();
    hash.extend(b.hash.iter().cloned());

    digest::digest(&digest::SHA256, data.as_slice())
        .as_ref()
        .to_vec()
}

// define how to hash two merkle tree node hashes into a new parent hash
fn leaf(leaf: &Node, roots: &Vec<Node>) -> Vec<u8> {
    let data = leaf.data.clone().unwrap();
    digest::digest(&digest::SHA256, data.as_slice()).as_ref().to_vec()
}

let mut gen = Generator::new(leaf, parent);

let nodes = gen.next(b"Hello World".to_vec());
println!("{:?}", nodes);

Tree Structure

See mafintosh/flat-tree-rs for more information about how node/parent indexes are calculated.

License

The MIT License

Dependencies

~5–13MB
~235K SLoC