Multihash for Python

3.0.0 · active · verified Thu Apr 16

py-multihash is a Python implementation of the Multihash specification, providing functions to encode and decode self-describing cryptographic hashes. The current version is 3.0.0. Releases are infrequent, tied to updates in the Multiformats specification.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to encode a byte string into a multihash and then decode it back to retrieve its properties like the algorithm name, code, length, and the raw digest.

import multihash
import multicodec

# Encode a byte string with a specific hash algorithm
data = b'Hello multihash world!'
mh = multihash.encode(data, 'sha2-256')
print(f"Encoded multihash: {mh.hex()}")

# Decode a multihash digest
decoded_digest = multihash.decode(mh)
print(f"Decoded digest name: {decoded_digest.name}")
print(f"Decoded digest code: {hex(decoded_digest.code)}")
print(f"Decoded digest length: {decoded_digest.length}")
print(f"Original digest: {decoded_digest.digest.hex()}")

# Verify the algorithm code using multicodec constants
assert decoded_digest.code == multicodec.SHA2_256
print("Verification successful!")

view raw JSON →