ipfs-cid
A library for building IPFS CID v1 compatible content identifiers using fixed encoding parameters.
Usage
Get CID from bytes
All at once
from ipfs_cid import cid_sha256_hash
data = b"Hello world"
result = cid_sha256_hash(data)
assert result == "bafkreide5semuafsnds3ugrvm6fbwuyw2ijpj43gwjdxemstjkfozi37hq"
In chunks with a generator
from typing import Iterable
from io import BytesIO
from ipfs_cid import cid_sha256_hash_chunked
def as_chunks(stream: BytesIO, chunk_size: int) -> Iterable[bytes]:
while len((chunk := stream.read(chunk_size))) > 0:
yield chunk
buffer = BytesIO(b"Hello world")
result = cid_sha256_hash_chunked(as_chunks(buffer, 4))
assert result == "bafkreide5semuafsnds3ugrvm6fbwuyw2ijpj43gwjdxemstjkfozi37hq"
Wrap an existing SHA 256 checksum as a CID
WARNING: This will lead to an invalid CID if an invalid digest is provided. This is not possible to validate against without the original data.
from hashlib import sha256
from ipfs_cid import cid_sha256_wrap_digest
data = b"Hello world"
digest = sha256(data).digest()
result = cid_sha256_wrap_digest(digest)
assert result == "bafkreide5semuafsnds3ugrvm6fbwuyw2ijpj43gwjdxemstjkfozi37hq"
Unwrap a compatible CID to a sha256 digest
NOTE: The cid_sha256_unwrap_digest function will throw an AttributeError
if the input CID is not using the same encoding parameters.
from hashlib import sha256
from ipfs_cid import cid_sha256_unwrap_digest
data = b"Hello world"
digest = sha256(data).digest()
cid = "bafkreide5semuafsnds3ugrvm6fbwuyw2ijpj43gwjdxemstjkfozi37hq"
result = cid_sha256_unwrap_digest(cid)
assert result == digest
Encoding Format
The CID spec supports multiple different encodings and hashing algorithms.
The resulting CID string is composed of the following components:
{multibase prefix} + multibase_encoded({cid version} + {content type} + {multihash})
This library always uses the following encoding parameters:
| multibase | CID version | Content Type | Multihash |
|---|---|---|---|
| base32 | cidv1 | raw | sha2-256 |
More details about the formats below:
Multibase
| encoding | code | description |
|---|---|---|
| base32 | b | rfc4648 case-insensitive - no padding |
Multicodec
| name | code | description |
|---|---|---|
| cidv1 | 0x01 | CID v1 |
| sha2-256 | 0x12 | sha2-256 with 256 bit digest |
| raw | 0x55 | raw binary |
Metadata
Release files for ipfs-cid 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ipfs_cid-1.0.0.tar.gz | 4.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ipfs_cid-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.7 kB
Release files / ipfs_cid-1.0.0.tar.gz
| Download URL | ipfs_cid-1.0.0.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a752c87cde68840e27dcb02f4e7cf940297fae43872bb5e6ef3bf73ad31af73d
|
|
BLAKE2b-256 checksum How to use checksums |
a2119a363e839d17cc9e98391c7862fc20985d012e3b7b3b50e9b9991562b155
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.10.9 Linux/5.15.0-1030-azure
|
Release files / ipfs_cid-1.0.0-py3-none-any.whl
| Download URL | ipfs_cid-1.0.0-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
341985f50f893e0e49cf7da0fc0c4ba1afbe57fdea8a215724fd708de4ef96cb
|
|
BLAKE2b-256 checksum How to use checksums |
d051e006097277ff32f552f47b568ff7af292873fa395bbf38d8ca12859b44fc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.10.9 Linux/5.15.0-1030-azure
|