merkle-tree-stream

crates.io version build status downloads docs.rs docs

A stream that generates a merkle tree based on the incoming data. Adapted from mafintosh/merkle-tree-stream.

Why?

Signatures & integrity checks are part of what makes Dat a great protocol. Each chunk that passes through the system is hashed and made part of a tree of hashes. We end up creating hashes of hashes thanks to flat-tree, which in the end allows us to validate our complete data set.

This module is only needed to create new Dat archives, but not to read them.

Usage

```rust extern crate merkletreestream; extern crate rust_sodium;

use merkletreestream::{HashMethods, MerkleTreeStream, Node, NodeVector, PartialNode}; use rust_sodium::crypto::hash::sha256; use std::rc::Rc;

struct S; impl HashMethods for S { fn leaf(&self, leaf: &PartialNode, roots: &[Rc]) -> Vec { let data = leaf.asref().unwrap(); sha256::hash(&data).0.to_vec() }

fn parent(&self, a: &Node, b: &Node) -> Vec { let mut buf: Vec = Vec::withcapacity(a.hash().len() + b.hash().len()); buf.extendfromslice(a.hash()); buf.extendfromslice(b.hash()); sha256::hash(&buf).0.tovec() } }

fn main() { let roots = Vec::new(); let mut mts = MerkleTreeStream::new(S, roots); let mut nodes: NodeVector = Vec::new(); mts.next(b"hello", &mut nodes); mts.next(b"hashed", &mut nodes); mts.next(b"world", &mut nodes); println!("nodes {:?}", nodes); } ```

Installation

sh $ cargo add merkle-tree-stream

License

MIT OR Apache-2.0