Coder Social home page Coder Social logo

py-multihash's Introduction

py-multihash

https://img.shields.io/travis/multiformats/py-multihash.svg?branch=master Documentation Status

Multihash implementation in Python

Example usage for end-to-end hashing:

import multihash

# 1. compute multihash digest as bytes
multihash_digest = multihash.digest(b'hello world', 'sha2-256')
print(multihash_digest.hex())
# 1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

# 2. encode multihash bytes to base58 string
print(multihash.to_b58_string(multihash_digest))
# QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4

# 1. compute multihash digest directly as base58 string
multihash_digest_str = multihash.b58digest(b'hello world', 'sha2-256')
print(multihash_digest_str)
# QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4

Example usage with pre-computed hash (e.g. using hashlib):

import hashlib
import multihash

# 1. hash your data
m = hashlib.sha256()
m.update(b'hello world')
raw_digest = m.digest()

# 2. add multihash header to raw digest bytes, see that your data follows the header:
multihash_digest = multihash.encode(raw_digest, 'sha2-256')
print('    '+raw_digest.hex())
print(multihash_digest.hex())
#     b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
# 1220b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
# ^^^^ header 0x1220: 0x12 is the 'sha2-256' code, 0x20 is the hash length

# 4. encode multihash bytes to base58 string
multihash_digest_str = multihash.to_b58_string(multihash_digest)
print(multihash_digest_str)
# QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4

py-multihash's People

Contributors

dhruvbaldawa avatar matrixmanatyrservice avatar mhchia avatar reardencode avatar sg495 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.