A stream that generates a merkle tree based on the incoming data
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Go to file
Luke Murphy 292ef5464d
continuous-integration/drone/push Build is passing Details
Fix up README example
3 years ago
merkle_tree_stream Migrate to pyproject config 3 years ago
test Migrate to pyproject config 3 years ago
.drone.yml Follow the rename and reduce boilerplate 3 years ago
.gitignore Initialise the new module boilerplate 4 years ago
CHANGELOG.md Migrate to pyproject config 3 years ago
LICENSE Follow the rename and reduce boilerplate 3 years ago
README.md Fix up README example 3 years ago
poetry.lock Migrate to pyproject config 3 years ago
pyproject.toml Remove reference to setup file 3 years ago

README.md

merkle-tree-stream

Build Status

A stream that generates a merkle tree based on the incoming data

A hash tree or merkle tree is a tree in which every leaf node is labelled with the hash of a data block and every non-leaf node is labelled with the cryptographic hash of the labels of its child nodes. Merkle trees in Dat are specialized flat trees that contain the content of the archives.

Install

$ pip install merkle-tree-stream

Example

import hashlib

from merkle_tree_stream import MerkleTreeGenerator


def _leaf(node, roots=None):
    return hashlib.sha256(node.data).digest()


def _parent(first, second):
    sha256 = hashlib.sha256()
    sha256.update(first.data)
    sha256.update(second.data)
    return sha256.digest()


merkle = MerkleTreeGenerator(leaf=_leaf, parent=_parent)

merkle.write(b"a")
merkle.write(b"b")

print(merkle._nodes)

Output:

[
 MerkleTreeNode(index=0, parent=1, size=1, data=b'a', hash=b'...'),
 MerkleTreeNode(index=2, parent=1, size=1, data=b'b', hash=b'...'),
 MerkleTreeNode(index=1, parent=3, size=2, data=b'', hash=b'...')
]