{"id":9200,"library":"py-multihash","title":"Multihash for Python","description":"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.","status":"active","version":"3.0.0","language":"en","source_language":"en","source_url":"https://github.com/multiformats/multihash","tags":["cryptography","hashing","multiformats","digest"],"install":[{"cmd":"pip install py-multihash","lang":"bash","label":"Install py-multihash"}],"dependencies":[{"reason":"Provides multicodec constants and decoding, essential for working with multihash algorithm codes.","package":"py-multicodec","optional":false},{"reason":"Required for multibase encoding/decoding of multihash digests.","package":"py-multibase","optional":false}],"imports":[{"symbol":"encode","correct":"from multihash import encode"},{"symbol":"decode","correct":"from multihash import decode"},{"symbol":"Digest","correct":"from multihash import Digest"},{"note":"The `Multihash` class was removed in v3.0.0. `encode` and `decode` are now top-level module functions, and `Digest` is directly importable.","wrong":"from multihash import Multihash","symbol":"Multihash","correct":"from multihash import encode, decode, Digest"}],"quickstart":{"code":"import multihash\nimport multicodec\n\n# Encode a byte string with a specific hash algorithm\ndata = b'Hello multihash world!'\nmh = multihash.encode(data, 'sha2-256')\nprint(f\"Encoded multihash: {mh.hex()}\")\n\n# Decode a multihash digest\ndecoded_digest = multihash.decode(mh)\nprint(f\"Decoded digest name: {decoded_digest.name}\")\nprint(f\"Decoded digest code: {hex(decoded_digest.code)}\")\nprint(f\"Decoded digest length: {decoded_digest.length}\")\nprint(f\"Original digest: {decoded_digest.digest.hex()}\")\n\n# Verify the algorithm code using multicodec constants\nassert decoded_digest.code == multicodec.SHA2_256\nprint(\"Verification successful!\")","lang":"python","description":"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."},"warnings":[{"fix":"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`.","message":"The `Multihash` class has been removed in version 3.0.0. `encode` and `decode` are now top-level functions in the `multihash` module, and `Digest` is a directly exposed class.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Instead of `from multihash.constants import SHA2_256`, use `import multicodec` and refer to constants as `multicodec.SHA2_256`.","message":"The `multihash.constants` module was removed in version 3.0.0. All algorithm constants (e.g., `SHA2_256`) are now provided by the `py-multicodec` library.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Upgrade your Python environment to 3.10 or higher. If unable, pin `py-multihash<3.0.0`.","message":"Python 3.10 or newer is now required for `py-multihash` version 3.0.0 and above.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"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.","message":"The `multihash.encode` function expects the hash algorithm to be specified by its string name (e.g., 'sha2-256') or its integer code, not a `py-multicodec` constant directly for the algorithm parameter.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"The `encode` and `decode` functions are now top-level. Use `multihash.encode(...)` and `multihash.decode(...)` directly.","cause":"Attempting to use the `Multihash` class which was removed in v3.0.0.","error":"AttributeError: module 'multihash' has no attribute 'Multihash'"},{"fix":"Algorithm constants are now part of the `py-multicodec` library. Use `import multicodec` and access constants like `multicodec.SHA2_256`.","cause":"Attempting to import the `constants` module which was removed in v3.0.0.","error":"ImportError: cannot import name 'constants' from 'multihash'"},{"fix":"These are now top-level functions. Call them as `multihash.encode(data, alg)` or `multihash.decode(mh)`.","cause":"Attempting to call `encode` or `decode` as a method of the removed `Multihash` class, or statically without providing `self`.","error":"TypeError: encode() missing 1 required positional argument: 'data' (if called as `Multihash.encode(alg)`) or AttributeError: type object 'Multihash' has no attribute 'encode'"}]}