# 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
**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.
```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"
```
### 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.
```python
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](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 |
Raw data
{
"_id": null,
"home_page": "https://github.com/thunderstore-io/ipfs-cid",
"name": "ipfs-cid",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "ipfs,multihash,multibase,cid,hash",
"author": "Mythic",
"author_email": "mythic@thunderstore.io",
"download_url": "https://files.pythonhosted.org/packages/a2/11/9a363e839d17cc9e98391c7862fc20985d012e3b7b3b50e9b9991562b155/ipfs_cid-1.0.0.tar.gz",
"platform": null,
"description": "# ipfs-cid\n\n[![pypi](https://img.shields.io/pypi/v/ipfs-cid)](https://pypi.org/project/ipfs-cid/)\n[![test](https://github.com/thunderstore-io/ipfs-cid/workflows/Test/badge.svg)](https://github.com/thunderstore-io/ipfs-cid/actions)\n[![codecov](https://codecov.io/gh/thunderstore-io/ipfs-cid/branch/master/graph/badge.svg?token=6lS3pEHvIw)](https://codecov.io/gh/thunderstore-io/ipfs-cid)\n[![python-versions](https://img.shields.io/pypi/pyversions/ipfs-cid.svg)](https://pypi.org/project/ipfs-cid/)\n\nA library for building IPFS CID v1 compatible content identifiers using fixed\nencoding parameters.\n\n## Usage\n\n### Get CID from bytes\n\nAll at once\n\n```python\nfrom ipfs_cid import cid_sha256_hash\n\ndata = b\"Hello world\"\nresult = cid_sha256_hash(data)\nassert result == \"bafkreide5semuafsnds3ugrvm6fbwuyw2ijpj43gwjdxemstjkfozi37hq\"\n```\n\nIn chunks with a generator\n\n```python\nfrom typing import Iterable\nfrom io import BytesIO\nfrom ipfs_cid import cid_sha256_hash_chunked\n\ndef as_chunks(stream: BytesIO, chunk_size: int) -> Iterable[bytes]:\n while len((chunk := stream.read(chunk_size))) > 0:\n yield chunk\n\nbuffer = BytesIO(b\"Hello world\")\nresult = cid_sha256_hash_chunked(as_chunks(buffer, 4))\nassert result == \"bafkreide5semuafsnds3ugrvm6fbwuyw2ijpj43gwjdxemstjkfozi37hq\"\n```\n\n### Wrap an existing SHA 256 checksum as a CID\n\n**WARNING:** This will lead to an invalid CID if an invalid digest is provided.\nThis is not possible to validate against without the original data.\n\n```python\nfrom hashlib import sha256\nfrom ipfs_cid import cid_sha256_wrap_digest\n\ndata = b\"Hello world\"\ndigest = sha256(data).digest()\nresult = cid_sha256_wrap_digest(digest)\nassert result == \"bafkreide5semuafsnds3ugrvm6fbwuyw2ijpj43gwjdxemstjkfozi37hq\"\n```\n\n### Unwrap a compatible CID to a sha256 digest\n\n**NOTE:** The `cid_sha256_unwrap_digest` function will throw an `AttributeError`\nif the input CID is not using the same encoding parameters.\n\n```python\nfrom hashlib import sha256\nfrom ipfs_cid import cid_sha256_unwrap_digest\n\ndata = b\"Hello world\"\ndigest = sha256(data).digest()\n\ncid = \"bafkreide5semuafsnds3ugrvm6fbwuyw2ijpj43gwjdxemstjkfozi37hq\"\nresult = cid_sha256_unwrap_digest(cid)\nassert result == digest\n```\n\n## Encoding Format\n\n[The CID spec](https://github.com/multiformats/cid) supports multiple different\nencodings and hashing algorithms.\n\nThe resulting CID string is composed of the following components:\n\n```\n{multibase prefix} + multibase_encoded({cid version} + {content type} + {multihash})\n```\n\nThis library always uses the following encoding parameters:\n\n| multibase | CID version | Content Type | Multihash |\n| --------- | ----------- | ------------ | --------- |\n| base32 | cidv1 | raw | sha2-256 |\n\nMore details about the formats below:\n\n### Multibase\n\n| encoding | code | description |\n| -------- | ---- | ------------------------------------- |\n| base32 | b | rfc4648 case-insensitive - no padding |\n\n### Multicodec\n\n| name | code | description |\n| -------- | ---- | ---------------------------- |\n| cidv1 | 0x01 | CID v1 |\n| sha2-256 | 0x12 | sha2-256 with 256 bit digest |\n| raw | 0x55 | raw binary |\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A library for building IPFS CID v1 compatible content identifiers using fixed encoding parameters.",
"version": "1.0.0",
"split_keywords": [
"ipfs",
"multihash",
"multibase",
"cid",
"hash"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d051e006097277ff32f552f47b568ff7af292873fa395bbf38d8ca12859b44fc",
"md5": "5a57b38796036d049b68fb30ccbdc622",
"sha256": "341985f50f893e0e49cf7da0fc0c4ba1afbe57fdea8a215724fd708de4ef96cb"
},
"downloads": -1,
"filename": "ipfs_cid-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5a57b38796036d049b68fb30ccbdc622",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 4574,
"upload_time": "2023-01-11T17:53:34",
"upload_time_iso_8601": "2023-01-11T17:53:34.889092Z",
"url": "https://files.pythonhosted.org/packages/d0/51/e006097277ff32f552f47b568ff7af292873fa395bbf38d8ca12859b44fc/ipfs_cid-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a2119a363e839d17cc9e98391c7862fc20985d012e3b7b3b50e9b9991562b155",
"md5": "a1397c8c915fb5e44e6cd73e57a82c31",
"sha256": "a752c87cde68840e27dcb02f4e7cf940297fae43872bb5e6ef3bf73ad31af73d"
},
"downloads": -1,
"filename": "ipfs_cid-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "a1397c8c915fb5e44e6cd73e57a82c31",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 4137,
"upload_time": "2023-01-11T17:53:36",
"upload_time_iso_8601": "2023-01-11T17:53:36.006269Z",
"url": "https://files.pythonhosted.org/packages/a2/11/9a363e839d17cc9e98391c7862fc20985d012e3b7b3b50e9b9991562b155/ipfs_cid-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-11 17:53:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "thunderstore-io",
"github_project": "ipfs-cid",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "ipfs-cid"
}