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.
pip install py-multihashVerified import paths — ran on the pinned version, not inferred.
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.
Replace `Multihash().encode(data, code)` with `multihash.encode(data, code)`. Replace `Multihash().decode(mh)` with `multihash.decode(mh)`. Import `Digest` directly: `from multihash import Digest`.
Instead of `from multihash.constants import SHA2_256`, use `import multicodec` and refer to constants as `multicodec.SHA2_256`.
Upgrade your Python environment to 3.10 or higher. If unable, pin `py-multihash<3.0.0`.
Use `multihash.encode(data, 'sha2-256')` or `multihash.encode(data, 0x12)` instead of `multihash.encode(data, multicodec.SHA2_256)` directly for the algorithm name/code parameter. The `multicodec` library is still essential for looking up constants and verifying codes.
The `encode` and `decode` functions are now top-level. Use `multihash.encode(...)` and `multihash.decode(...)` directly.
Algorithm constants are now part of the `py-multicodec` library. Use `import multicodec` and access constants like `multicodec.SHA2_256`.
These are now top-level functions. Call them as `multihash.encode(data, alg)` or `multihash.decode(mh)`.