Skip to main content

Python 3 implementation of Merkle Trees

Project description

Perkle: A simple Python 3 implementation of Merkle Trees

Perkle provides a simple, all python, implementation of Merkle Trees.

Installation

The package can be installed via pip:

pip install perkle

or by cloning this repository.

The package pycryptodome is in the requirements. Make sure that this will not conflict with an installation of the (deprecated) package pycrypto.

Example

Here is how to create a Merkle Tree of numbers from 0 to 9:

from Crypto.Hash import SHA256
from perkle import MerkleTree
from binascii import hexlify
data_list = [b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9']
sha256 = lambda x : SHA256.new(x).digest() 
mt = MerkleTree(data_list, sha256, random_padding=False, padding_byte=b'0')
print(hexlify(mt.root()))
#70cc27c03c0444d1dfc63f58e373a2882a7b9f4f7f6ed1a4dfc1a94a5ac5875c

By default, we add a random padding to the data to have a power of two leaves. You can make the padding non-random with the random_padding parameter to make the results consistent.

New data can be inserted in the Merkle Tree:

data = b'10'
mt.insert(data)
print(hexlify(mt.root()))
#3e32b51c76de4b124f1fedfbb4d5a30d117274bf04b538ca7f10434e2f8a35b9

And existing data can be updated:

new_data = b'zero'
mt.update(0,new_data)
print(hexlify(mt.root()))
#d485ca0c92d339d2e8495216cdb420f0e215003fffdb62d09da56d8739c94a11

Proofs can be easily generated:

index, proof_hashes = mt.proof(b'7')

and verified:

print(MerkleTree.verify(b'7',index,proof_hashes,mt.root(),sha256))
#True

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

perkle-0.0.5.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

perkle-0.0.5-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file perkle-0.0.5.tar.gz.

File metadata

  • Download URL: perkle-0.0.5.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for perkle-0.0.5.tar.gz
Algorithm Hash digest
SHA256 72ab02b13ec786e0a42ca257d773a403b7ce363c6374ebd14bb679997ad3dad9
MD5 2e4b4a83ed1900ef570b9c6ca047ed6c
BLAKE2b-256 3d9cbec9e9b4f04b4dffc04943abd653e30e191df2742775464c3887da7a2b4c

See more details on using hashes here.

File details

Details for the file perkle-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: perkle-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for perkle-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f971e7b51100acb60f34cafcf5f69ca0f68c601a1bbee92c31356d4035f8936c
MD5 48e9bb62dd244de092a3abff75ba9ea5
BLAKE2b-256 21bcda977aceb9fc5a7e4e8293b36cf74a5860882d6af9f72db4fc68ab96955f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page