# Manually calculate the IPFS CID v2

**URL:** <https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671>\
**Category:** Help\
**Created:** [July 2, 2021, 7:59am UTC](https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671 "2021-07-02T07:59:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ovanwijk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/ovanwijk/32/4431_2.png) [@ovanwijk](https://discuss.ipfs.tech/u/ovanwijk)\
**Post date:** [July 2, 2021, 7:59am UTC](https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671/1 "2021-07-02T07:59:45Z")

</div>

Hi!

I have been searching forever but somehow I am never really getting where I want to be.

I simply want to use the contents of a file + filename to calculate the CID but with plain code and not with any other library. None of the IPFS CLI commands seem to be able to give me sets of data that I can transform to bytes(either from hex or base58) that will match the CID(ofcourse 1220) if I sha256 it.

So I would really like some simple (javascript) example on how this can be done. Given UTF8:  
`999999999999999999999999999999999999999999999999999999999999999900000000000000000000000000000000` with filename `inputs.bytes`

What should I do eventually get to CID: `QmdyL59DkUaj6EMcDD3b4rMchGZFJnasT6C6N2RvpGLYXi`

The reason I want from content to CID is that I eventually want to be able to validate small files in solidity and perform file format checks. So I want to be able to calculate the CID FROM only it’s contents. Eventually I want to reconstruct a top level folder as well, but that is a problem I think I can solve if I can get to a CID from content only.

Really hope someone can help, because I am a bit lost here, the best I got so far is to manually construct some protobuff file and handle the varint etc (Note that I want to do this in solidity)

---

<div class="post-metadata">

**Author:** ![wclayf](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/wclayf/32/4936_2.png) [@wclayf](https://discuss.ipfs.tech/u/wclayf)\
**Post date:** [July 4, 2021, 3:04am UTC](https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671/2 "2021-07-04T03:04:53Z")

</div>

This is a common question that comes up a lot on this forum. Use the search to find the other discussions. I think someone did post a working solution perhaps, but it assumes a specific chunking algo, and other parameters. There’s no one-to-one mapping of data files to CIDs which has confused lots and lots of people. That is, unless you define ahead of time a bunch of parameters that go into the system, you can’t be sure what the output CID will be, so it’s not a simple algorithm like a SHA-256. Depending on which different parameters you use you can get DIFFERENT CIDs for the same file.

---

<div class="post-metadata">

**Author:** ![ovanwijk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/ovanwijk/32/4431_2.png) [@ovanwijk](https://discuss.ipfs.tech/u/ovanwijk)\
**Post date:** [July 5, 2021, 7:58pm UTC](https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671/3 "2021-07-05T19:58:30Z")

</div>

Yeah I figured it wasn’t that easy, but I am able to control the parameters of the files, so that is no problem really.

Also, I am happy to search but so far I came out empty-handed. Maybe just using the wrong keywords, most things related to multihash specifics don’t go further than the generic description and the more deepdive topics on chunks and trees all reference the API. I already got to the Protobuff2 encoding of the filesystem, so there is that.

Maybe there is some specific wording used to go to an even deeper worked out explanation? Seems it shouldn’t be too difficult to prop up an example if all params are known in advance.

---

<div class="post-metadata">

**Author:** ![wclayf](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/wclayf/32/4936_2.png) [@wclayf](https://discuss.ipfs.tech/u/wclayf)\
**Post date:** [July 5, 2021, 9:16pm UTC](https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671/4 "2021-07-05T21:16:13Z")

</div>

Here’s some prior discussions I found…

> [@How to calculate file/directory hash?](https://discuss.ipfs.tech/t/how-to-calculate-file-directory-hash/777):
>
> I’m interested in calculating IPFS hashes for various files and directories, somewhat similar to ipfs add -n. I’ve searched around, but wasn’t able to find any solid documentation on how this is calculated exactly. I’ve had a quick look through code and tried figuring it out from experimentation, and have some basic idea (files are broken into 256K chunks, wrapped in some protobuf format, hashed using SHA256 and there are index pieces which join together these small pieces, in a tree-like fashi…

> [@Generate File Hash Locally on Browser](https://discuss.ipfs.tech/t/generate-file-hash-locally-on-browser/10607):
>
> Hi everyone! I’m glad to be part of this community! Hopefully someone can help me with my following issue! What I’m trying to do is to generate the IPFS hash of a file while the user is using a PWA offline (just a web app). If I’m connected, I can just ipfs.add to add a file and get the file hash, no sweat. Now, if I’m offline I need to hash it locally and want the same output as if I added to an IPFS node. This is the code that I got so far: import { importer } from "ipfs-unixfs-importer"; …

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

---

<div class="post-metadata">

**Author:** ![ovanwijk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/ovanwijk/32/4431_2.png) [@ovanwijk](https://discuss.ipfs.tech/u/ovanwijk)\
**Post date:** [July 7, 2021, 6:41am UTC](https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671/5 "2021-07-07T06:41:54Z")

</div>

Thnx, these were indeed what I found, but they still use the default libs to actually preform the functions.

I finally got there though, for a single chunk for now, in Solidity.

Thnx!

---

<div class="post-metadata">

**Author:** ![anon47005736](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@anon47005736](https://discuss.ipfs.tech/u/anon47005736)\
**Post date:** [July 12, 2021, 1:43am UTC](https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671/6 "2021-07-12T01:43:55Z")

</div>

I think you meant CIDv0, not v2

---

<div class="post-metadata">

**Author:** ![kristian](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@kristian](https://discuss.ipfs.tech/u/kristian)\
**Post date:** [July 19, 2021, 3:55pm UTC](https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671/7 "2021-07-19T15:55:56Z")

</div>

Yeah i wanted to do this too. but ended up using the ipfs-http-client lib.

const { create, urlSource } = require(‘ipfs-http-client’)  
const file = await ipfs.add(urlSource(`http://blah/myfile.blah`))  
fileCID = file.cid  
console.log(fileCID)

---

<div class="post-metadata">

**Author:** ![ovanwijk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/ovanwijk/32/4431_2.png) [@ovanwijk](https://discuss.ipfs.tech/u/ovanwijk)\
**Post date:** [July 19, 2021, 7:19pm UTC](https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671/8 "2021-07-19T19:19:49Z")

</div>

> [@Sha256 content hashing in solidoty](https://discuss.ipfs.tech/t/sha256-content-hashing-in-solidoty/11793):
>
> Hi! For those who might find it interesting: I wrote a small smart contract that wraps any content from a single chunk into a ipfs-style hash. It also supports hashing folders with different files. I wrote this because I want to be able to validate the content of a small ipfs-file on chain. Given this file is in a larger folder structure. It is a very crude implementation and suits my needs for now but someone might find it useful! Prior discussion about this topic: [Manually calculate th…](https://discuss.ipfs.tech/t/manually-calculate-the-ipfs-cid-v2/11671)
