py-cid is a Python library that provides an implementation of the Content Identifier (CID) specification. CIDs are self-describing content-addressed identifiers used in distributed systems like IPFS and IPLD, leveraging cryptographic hashing and multiformats for flexible self-description. The library is actively maintained, with version 0.5.0 being the latest release, supporting Python 3.10 and newer.
pip install py-cidVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create CIDv0 and CIDv1 objects using `make_cid` from both base58 and multibase encoded strings. It also shows how to access their version, codec, multihash, and how to encode CIDv1 into different multibase formats.
Ensure you are installing `py-cid` from the `ipld/py-cid` project and using `pip install py-cid` for the current API. Avoid `py-multiformats-cid` if you intend to use the latest `py-cid` features.
Be explicit about the CID version you expect. Use `make_cid` which handles both, or `CIDv0` and `CIDv1` constructors directly for specific versions. Remember `cid.encode()` on `CIDv0` will only produce Base58, while `CIDv1.encode(base)` allows various multibase encodings.
Install the library using `pip install py-cid`.
`make_cid` generally infers the CID version and codec from the provided string. For `CIDv1` with a raw multihash, use `CIDv1('codec-name', multihash_bytes)` directly. For example: `CIDv1('raw', b'\x12\x20' + b'hash_bytes')`.Ensure you are working with a `CIDv1` object if you need flexible multibase encoding. For `CIDv0`, use `str(cid_obj)` or `cid_obj.encode()` to get its Base58 string representation.