# How to calculate Cid locally?

**URL:** <https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507>\
**Category:** Help\
**Created:** [March 16, 2021, 8:15am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507 "2021-03-16T08:15:58Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![serenity](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@serenity](https://discuss.ipfs.tech/u/serenity)\
**Post date:** [March 16, 2021, 8:15am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/1 "2021-03-16T08:15:58Z")

</div>

how can i calculate cid locally .  
like below

> **[GitHub - alanshaw/ipfs-only-hash: #️⃣ Just enough code to calculate the IPFS hash...](https://github.com/alanshaw/ipfs-only-hash)**
>
> \#️⃣ Just enough code to calculate the IPFS hash for some data

i only find js repo. is there any java implement?

---

<div class="post-metadata">

**Author:** ![serenity](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@serenity](https://discuss.ipfs.tech/u/serenity)\
**Post date:** [March 18, 2021, 6:04am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/2 "2021-03-18T06:04:04Z")

</div>

is there anyone help?

---

<div class="post-metadata">

**Author:** ![woss](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/woss/32/3588_2.png) [@woss](https://discuss.ipfs.tech/u/woss)\
**Post date:** [March 18, 2021, 7:51am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/3 "2021-03-18T07:51:34Z")

</div>

what about this [GitHub - ipfs-shipyard/java-ipfs-http-client: A Java implementation of the HTTP IPFS API](https://github.com/ipfs-shipyard/java-ipfs-http-client) ?

You get all ipfs commands over the http

---

<div class="post-metadata">

**Author:** ![serenity](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@serenity](https://discuss.ipfs.tech/u/serenity)\
**Post date:** [March 26, 2021, 7:24am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/4 "2021-03-26T07:24:42Z")

</div>

i need calculate my file CID hash locally. rather than through http api.  
so i needn’t upload to ipfs node to save internet traffic

---

<div class="post-metadata">

**Author:** ![woss](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/woss/32/3588_2.png) [@woss](https://discuss.ipfs.tech/u/woss)\
**Post date:** [March 26, 2021, 10:59am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/5 "2021-03-26T10:59:52Z")

</div>

oh in that case why don’t you see the implementations of the CID itself [GitHub - multiformats/cid: Self-describing content-addressed identifiers for distributed systems](https://github.com/multiformats/cid#implementations) you can use it without the ipfs.

---

<div class="post-metadata">

**Author:** ![Akita](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/akita/32/2650_2.png) [@Akita](https://discuss.ipfs.tech/u/Akita)\
**Post date:** [March 28, 2021, 5:26pm UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/6 "2021-03-28T17:26:50Z")

</div>

You need this:

> **[alanshaw/ipfs-only-hash](https://github.com/alanshaw/ipfs-only-hash)**
>
> \#️⃣ Just enough code to calculate the IPFS hash for some data

---

<div class="post-metadata">

**Author:** ![woss](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/woss/32/3588_2.png) [@woss](https://discuss.ipfs.tech/u/woss)\
**Post date:** [March 29, 2021, 11:14am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/7 "2021-03-29T11:14:25Z")

</div>

i believe he needs the Java version and he listed that repo already.

I’m failing to understand is this about calculating the CID or using the offline capability of the ipfs. if it’s just about the CID you can create it from any buffer.

---

<div class="post-metadata">

**Author:** ![serenity](https://avatars.discourse-cdn.com/v4/letter/s/3bc359/32.png) [@serenity](https://discuss.ipfs.tech/u/serenity)\
**Post date:** [March 30, 2021, 1:36am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/8 "2021-03-30T01:36:46Z")

</div>

any example code for calculate CID from buffer.  
i’m not familiar with java

---

<div class="post-metadata">

**Author:** ![woss](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/woss/32/3588_2.png) [@woss](https://discuss.ipfs.tech/u/woss)\
**Post date:** [May 24, 2021, 6:50pm UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/9 "2021-05-24T18:50:30Z")

</div>

you need to use multihash then pass that into the CID constructor. Check this \>\> [GitHub - multiformats/js-cid: CID implementation in JavaScript](https://github.com/multiformats/js-cid#usage)

---

<div class="post-metadata">

**Author:** ![twdragon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/twdragon/32/4073_2.png) [@twdragon](https://discuss.ipfs.tech/u/twdragon)\
**Post date:** [March 16, 2024, 8:50pm UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/10 "2024-03-16T20:50:20Z")

</div>

I worked a bit on [this StackOverflow answer](https://stackoverflow.com/questions/76799279/compute-cid-of-data-in-go), and created the minimal standalone implementation in Go that recreates the CID v1 for the specified file. I am not so familiar with Go, so it is just a minimal viable example.

> **[GitHub - twdragon/ipfs-cid-local: An example program in Go to calculate IPFS...](https://github.com/twdragon/ipfs-cid-local)**
>
> An example program in Go to calculate IPFS CIDs. Contribute to twdragon/ipfs-cid-local development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![hector](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/hector/32/178_2.png) [@hector](https://discuss.ipfs.tech/u/hector)\
**Post date:** [March 16, 2024, 10:07pm UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/11 "2024-03-16T22:07:28Z")

</div>

Hello, this is wrong. It works by chance on small files with raw-cid encoding but nothing else. iirc from my quick look, it chunks and then writes all the chunks to a buffer and hashes that, so chunking happens for nothing. Chunks are not used to build a dag or anything.

The reference implementation of adding is: [kubo/core/coreunix/add.go at master · ipfs/kubo · GitHub](https://github.com/ipfs/kubo/blob/master/core/coreunix/add.go#L48)

There are multiple gotchas around folders etc but for single files they would still need a DAG-builder, like done here: [kubo/core/coreunix/add.go at master · ipfs/kubo · GitHub](https://github.com/ipfs/kubo/blob/master/core/coreunix/add.go#L115-L131)

---

<div class="post-metadata">

**Author:** ![twdragon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/twdragon/32/4073_2.png) [@twdragon](https://discuss.ipfs.tech/u/twdragon)\
**Post date:** [March 17, 2024, 3:22pm UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/12 "2024-03-17T15:22:31Z")

</div>

Thanks @hector, really, it does not build DAG as it should be done. I made this example just to see that for smaller linear data areays it would work. Anyhow, it should be very good to have a tool to chunk the data and visualize CID, and possibly, next the resulting DAG. I guess, all the people who cares about persistence, would want this. As I said, I am not so familiar with Go, so for now I only could do a request through the issue on the repository.

---

<div class="post-metadata">

**Author:** ![Jorropo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/jorropo/32/3230_2.png) [@Jorropo](https://discuss.ipfs.tech/u/Jorropo)\
**Post date:** [March 17, 2024, 10:45pm UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/13 "2024-03-17T22:45:16Z")

</div>

> [@twdragon](#):
>
> Anyhow, it should be very good to have a tool to chunk the data and visualize CID

[https://dag.ipfs.tech/](https://dag.ipfs.tech/) ✨

---

<div class="post-metadata">

**Author:** ![twdragon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/twdragon/32/4073_2.png) [@twdragon](https://discuss.ipfs.tech/u/twdragon)\
**Post date:** [March 18, 2024, 6:18am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/14 "2024-03-18T06:18:43Z")

</div>

@Jorropo this is confusing not to have a link to Github for this tool on the page. Is there a repository?

I would still like to have it standalone and offline, so could you point me to any related code?

---

<div class="post-metadata">

**Author:** ![Jorropo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/jorropo/32/3230_2.png) [@Jorropo](https://discuss.ipfs.tech/u/Jorropo)\
**Post date:** [March 18, 2024, 7:13am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/15 "2024-03-18T07:13:32Z")

</div>

I don’t know where the source for dag.ipfs.tech lives.

[boxo](https://github.com/ipfs/boxo/tree/main/examples/unixfs-file-cid) and [helia](https://github.com/ipfs-examples/helia-examples/tree/main/examples/helia-create-car) have examples on how to add files.  
To get the CID you create the unixfs dag but write the blocks to `/dev/null` (\*in practice you don’t write them anywhere).  
Here is the implementation of that feature inside kubo `ipfs add --only-hash`:

> <https://github.com/ipfs/kubo/blob/5af3cc1c025f55aa7595563c3d1a8b5180b76189/core/coreapi/unixfs.go#L110-L118>

> <https://github.com/ipfs/kubo/blob/5af3cc1c025f55aa7595563c3d1a8b5180b76189/core/coreapi/unixfs.go#L127-L131>

> <https://github.com/ipfs/kubo/blob/5af3cc1c025f55aa7595563c3d1a8b5180b76189/core/coreapi/unixfs.go#L177-L191>

The code is exactly the same as `ipfs add` but it inject mock / null storage which don’t store anything anywhere.

Here is where the boxo example add the file:

> <https://github.com/ipfs/boxo/blob/2559ec2c7b93ac6905fe83d7a4b38b8e9a703b0a/examples/unixfs-file-cid/main.go#L151-L177>

`nd.Cid()` is the info you want, then you can use an implementation of the storage which doesn’t store anything.

---

<div class="post-metadata">

**Author:** ![twdragon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/twdragon/32/4073_2.png) [@twdragon](https://discuss.ipfs.tech/u/twdragon)\
**Post date:** [March 18, 2024, 11:00am UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/16 "2024-03-18T11:00:15Z")

</div>

> [@Jorropo](#):
>
> I don’t know where the source for dag.ipfs.tech lives.

@hector could you point us? Still cannot find it

---

<div class="post-metadata">

**Author:** ![twdragon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/twdragon/32/4073_2.png) [@twdragon](https://discuss.ipfs.tech/u/twdragon)\
**Post date:** [March 18, 2024, 4:25pm UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/17 "2024-03-18T16:25:18Z")

</div>

@hector @Jorropo I have updated my repository involving Boxo in a proper(?) way. I will be very appreciated if you have a quick look at it. The code is very simple, and it allows us to recreate CIDv1 with default DAG builder settings. Would be glad to hear about the underwater stones I did not see.

> **[GitHub - twdragon/ipfs-cid-local: An example program in Go to calculate IPFS...](https://github.com/twdragon/ipfs-cid-local)**
>
> An example program in Go to calculate IPFS CIDs. Contribute to twdragon/ipfs-cid-local development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![hector](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/hector/32/178_2.png) [@hector](https://discuss.ipfs.tech/u/hector)\
**Post date:** [March 19, 2024, 5:19pm UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/18 "2024-03-19T17:19:29Z")

</div>

> [@twdragon](#):
>
> > [@Jorropo](#):
> >
> > I don’t know where the source for dag.ipfs.tech lives.
> 
> @hector could you point us? Still cannot find it

Probably here [GitHub - ipfs-shipyard/ipfs-dag-builder-vis: See how the DAGs get built](https://github.com/ipfs-shipyard/ipfs-dag-builder-vis)

> [@twdragon](#):
>
> @hector @Jorropo I have updated my repository involving Boxo in a proper(?) way. I will be very appreciated if you have a quick look at it. The code is very simple, and it allows us to recreate CIDv1 with default DAG builder settings. Would be glad to hear about the underwater stones I did not see.
> 
> [GitHub - twdragon/ipfs-cid-local: An example program in Go to calculate IPFS CIDs](https://github.com/twdragon/ipfs-cid-local)

It looks OK to me, it should produce the same CIDs as Kubo, this is easily testable with a few big and small files.

---

<div class="post-metadata">

**Author:** ![twdragon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/twdragon/32/4073_2.png) [@twdragon](https://discuss.ipfs.tech/u/twdragon)\
**Post date:** [March 19, 2024, 5:21pm UTC](https://discuss.ipfs.tech/t/how-to-calculate-cid-locally/10507/19 "2024-03-19T17:21:58Z")

</div>

> [@hector](#):
>
> It looks OK to me, it should produce the same CIDs as Kubo, this is easily testable with a few big and small files.

It does, cause I tried to keep all the defaults. Thank you for having a look!
