Metadata-Version: 2.1
Name: ipfs-cid
Version: 0.2.0
Summary: A library for building IPFS CID v1 compatible content identifiers using fixed encoding parameters.
Home-page: https://github.com/thunderstore-io/ipfs-cid
License: MIT
Keywords: ipfs,multihash,multibase,cid,hash
Author: Mythic
Author-email: mythic@thunderstore.io
Requires-Python: >=3.8,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Project-URL: Repository, https://github.com/thunderstore-io/ipfs-cid
Description-Content-Type: text/markdown

# ipfs-cid

[![pypi](https://img.shields.io/pypi/v/ipfs-cid)](https://pypi.org/project/ipfs-cid/)
[![test](https://github.com/thunderstore-io/ipfs-cid/workflows/Test/badge.svg)](https://github.com/thunderstore-io/ipfs-cid/actions)
[![codecov](https://codecov.io/gh/thunderstore-io/ipfs-cid/branch/master/graph/badge.svg?token=6lS3pEHvIw)](https://codecov.io/gh/thunderstore-io/ipfs-cid)
[![python-versions](https://img.shields.io/pypi/pyversions/ipfs-cid.svg)](https://pypi.org/project/ipfs-cid/)

A library for building IPFS CID v1 compatible content identifiers using fixed
encoding parameters.

## Usage

### Get CID from bytes

All at once

```python
from ipfs_cid import cid_sha256_hash

data = b"Hello world"
result = cid_sha256_hash(data)
assert result == "bafkreide5semuafsnds3ugrvm6fbwuyw2ijpj43gwjdxemstjkfozi37hq"
```

In chunks with a generator

```python
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

```python
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"
```

## Encoding Format

[The CID spec](https://github.com/multiformats/cid) 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                   |

