pymultihash
raw JSON → 0.8.2 verified Fri May 01 auth: no python
Python implementation of the multihash specification (current version 0.8.2). Multihash is a self-describing hash format that includes a hash function identifier and digest length. This library provides encoding, decoding, and hashing utilities. Release cadence is low; last release was in 2020.
pip install pymultihash Common errors
error ValueError: Unknown multihash code: 0x12 ↓
cause Passing a raw digest (without the multihash code+length prefix) to `decode()`.
fix
Ensure the input to
decode() is a complete multihash-encoded byte string, not a raw hash. Use digest() to create a valid multihash. error NameError: name 'multihash' is not defined ↓
cause Using incorrect import path (e.g., `from pymultihash import multihash`).
fix
Use
import multihash (the package exposes a top-level module). error ValueError: Unsupported hash function: sha256 ↓
cause Using an incorrect hash function name (common mistake: using 'sha256' instead of 'sha2-256').
fix
Use the full multihash name: 'sha2-256', 'sha2-512', 'sha3-512', etc.
error TypeError: digest() takes 2 positional arguments but 3 were given ↓
cause The third argument `length` is optional but must be passed as a keyword argument if provided.
fix
Use
multihash.digest(data, 'sha2-256', length=16) instead of positional third argument. Warnings
gotcha The `digest` function returns a multihash-encoded byte string (with code and length prefix), not a raw hash digest. This differs from other multihash libraries. ↓
fix Use `multihash.decode()` to extract the raw digest, or use `hashlib` for raw hashing and then `multihash.encode()` to wrap it.
gotcha Hash function names are lowercase with hyphens (e.g., 'sha2-256', 'sha3-512'). Using incorrect casing (e.g., 'SHA256') raises a ValueError. ↓
fix Use the exact names as defined in the multihash table: 'sha1', 'sha2-256', 'sha2-512', 'sha3-512', 'blake2b-256', etc.
deprecated The `encode` and `decode` functions may be renamed or deprecated in future versions. Check the latest documentation. ↓
fix Monitor the repo for API changes; no breaking changes have occurred yet.
Imports
- multihash wrong
from pymultihash import multihashcorrectimport multihash
Quickstart
import multihash
# Hash data using SHA2-256 (code 0x12)
digest = multihash.digest(b'hello world', 'sha2-256')
print(digest.hex()) # multihash encoded
# Decode a multihash
decoded = multihash.decode(digest)
print(decoded.code) # 0x12
print(decoded.digest.hex()) # raw digest
# Hash with a specific length (truncated)
digest_trunc = multihash.digest(b'hello world', 'sha2-256', 8)
print(len(digest_trunc)) # 1 (varint code) + 1 (length) + 8 = 10 bytes