A stream that generates a merkle tree based on the incoming data. Adapted from mafintosh/merkle-tree-stream.
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.
```rust extern crate merkletreestream; extern crate rust_sodium;
use merkletreestream::{HashMethods, DefaultNode, MerkleTreeStream, Node, PartialNode}; use rust_sodium::crypto::hash::sha256; use std::rc::Rc;
struct H;
impl HashMethods for H {
type Node = DefaultNode;
type Hash = Vec
fn leaf(&self, leaf: &PartialNode, roots: &[Rc
fn parent(&self, a: &Self::Node, b: &Self::Node) -> Self::Hash { let mut buf = 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(H, roots); let mut nodes = vec![]; mts.next(b"hello", &mut nodes); mts.next(b"hashed", &mut nodes); mts.next(b"world", &mut nodes); println!("nodes {:?}", nodes); } ```
Node
or Hash
typesIf you have a specific need for a Node
type that is not covered by the
DefaultNode
type, you can define your own by implementing the Node
trait and
the appropriate From<NodeParts<Self::Hash>>
trait for your new type. You can
use the DefaultNode
implementation as a guide.
sh
$ cargo add merkle-tree-stream
MIT OR Apache-2.0