Giter VIP home page Giter VIP logo

merkle-tree-stream's Introduction

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

extern crate merkle_tree_stream;
extern crate rust_sodium;

use merkle_tree_stream::{HashMethods, DefaultNode, MerkleTreeStream, Node,
                        PartialNode};
use rust_sodium::crypto::hash::sha256;
use std::sync::Arc;

struct H;
impl HashMethods for H {
  type Node = DefaultNode;
  type Hash = Vec<u8>;

  fn leaf(&self, leaf: &PartialNode, _roots: &[Arc<Self::Node>]) -> Self::Hash {
    let data = leaf.as_ref().unwrap();
    sha256::hash(&data).0.to_vec()
  }

  fn parent(&self, a: &Self::Node, b: &Self::Node) -> Self::Hash {
    let mut buf = Vec::with_capacity(a.hash().len() + b.hash().len());
    buf.extend_from_slice(a.hash());
    buf.extend_from_slice(b.hash());
    sha256::hash(&buf).0.to_vec()
  }
}

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);
}

Custom Node or Hash types

If 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.

Installation

$ cargo add merkle-tree-stream

License

MIT OR Apache-2.0

merkle-tree-stream's People

Contributors

yoshuawuyts avatar bltavares avatar ralphtheninja avatar scotttrinh avatar khernyo avatar dependabot-preview[bot] avatar dependabot[bot] avatar dependabot-support avatar pierd avatar srenatus avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.