# File hash is different from original

**URL:** <https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440>\
**Category:** Help\
**Created:** [March 9, 2020, 11:38am UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440 "2020-03-09T11:38:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 9, 2020, 11:38am UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/1 "2020-03-09T11:38:24Z")

</div>

Hello everyone,

im new to IPFS and testing it to potentially implement it into a blockchain dapp, for now there is something that is quite unclear to me:

when i add a file then retrieve it later through [https://ipfs.infura.io/ipfs/QmdZTvj9](https://ipfs.infura.io/ipfs/QmdZTvj9)…, if i download the file and hash it in a sha256 tool, the sha256 hash of the downloaded file is different than the one of the initial file, so the file is modified when uploaded? is there a way to retrieve the exact initial file with same hash?

Thank you in advance.

---

<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 9, 2020, 11:55am UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/2 "2020-03-09T11:55:52Z")

</div>

The “Hash” returned by IPFS is not a hash, but a CID.

You can see the original sha256 hash using this tool [https://cid.ipfs.io/](https://cid.ipfs.io/)

BUT, the files are chunked (by default in 256k blocks) and also, by default, wrapped in a wrapper thing called a UnixFS node. You can see how your file was chunked-wrapped at [https://explore.ipld.io/](https://explore.ipld.io/)

For things \<1MB you can increase the chunking size when you add (so that you get a single chunk), and you can enable `--raw-leaves`, so that the resulting single chunk is not wrapped. In that case, you will get a CID which embeds a sha256 hash, which corresponds to that returned by the sha256 tool.

1MB is a network contraint. Peers refuse to transfer things bigger than that (it may be 2MB, I can’t remember), so you can use bigger chunks but peers will not be able to download them.

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 9, 2020, 12:12pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/3 "2020-03-09T12:12:40Z")

</div>

Thanks a lot for your detailled answer, its interesting but actually my concern is not the hash that IPFS return but when i download the file itself, how come the sha256 hash of the downloaded file is different than original? it mean the file was modified in the process?

---

<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 9, 2020, 12:39pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/4 "2020-03-09T12:39:58Z")

</div>

ah, ok, I understand now. Well, I don’t know why it’s different. It should not be.

Can you check the differences or diff between the two somehow?

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 9, 2020, 1:13pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/5 "2020-03-09T13:13:31Z")

</div>

Thanks a lot for your help, here my test:

```
import IPFS from "ipfs-mini"; 
import { sha256 } from "js-sha256";
var buffer = require("buffer");

const ipsf = new IPFS({
  host: "ipfs.infura.io",
  port: 5001,
  protocol: "https"
});

function upload() {
  const reader = new FileReader();
  reader.onloadend = function() {
    const ipfs = new IPFS({
      host: "ipfs.infura.io",
      port: 5001,
      protocol: "https"
    });
    const buf = buffer.Buffer(reader.result); // Convert data into buffer
    console.log(sha256(buf));
    ipfs.add(buf).then(resAddFile => {
       ipfs.addJSON({ file_name: "mon_fichier.pdf", ipfs_file_hash: "https://ipfs.infura.io/ipfs/"+resAddFile }).then(resAddJson => { // or https://gateway.ipfs.io/ipfs/
        console.log(resAddFile);
        ipfs
          .catJSON(resAddJson)
          .then(resCatJson => {
            document.getElementById("url").innerHTML = resCatJson.file_name;
            document.getElementById("url").href = resCatJson.ipfs_file_hash;
            console.log(resCatJson);
          })
          .catch(err => {
            console.log(err)
          });

    }).catch(err => {
        console.error(err);
    });

    }).catch(err => {
        console.error(err);
    });

  };
  const photo = document.getElementById("photo");
  reader.readAsArrayBuffer(photo.files[0]); // Read Provided File

}
document.getElementById("uploadImage").addEventListener("click", upload);

```

For info the console.log(sha256(buf)); give me the proper hash which is _9bc7f6263686a03017d2b59f70f548d6b037ea305237c63e3adf2bd47b28d91f_ but the file downloaded from ipfs give me _880b7c72db8212320e4d347f3c71c76ec4ac70ada098da5712369c5ab78feb35_

---

<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 9, 2020, 1:23pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/6 "2020-03-09T13:23:43Z")

</div>

are you sure you are hashing what you think you are hashing? Try with a small text file and compare with other sha256 calculator.

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 9, 2020, 1:41pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/7 "2020-03-09T13:41:45Z")

</div>

Im quite sure because i check the hash of what i add on ipfs, before adding it and its the same hash as when i hash the same file in online tools. Then i later download the file from IPFS, hash it in same online tools and its a different hash. I made a small video to visualise. I notice that when i download the IPFS file then open it, for some reasons its asking me if i want save the changes, when i didnt do any changes.

Here the example: [https://youtu.be/T\_-9SzcwhZI](https://youtu.be/T_-9SzcwhZI)

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 9, 2020, 2:27pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/8 "2020-03-09T14:27:27Z")

</div>

It seem that the file end up not fully uploaded, for exemple i test with this PDF file on the left, and IPFS end up with this one on the right, where the background is missing:

 ![ipfsko](https://us1.discourse-cdn.com/flex020/uploads/ipfs1/original/2X/0/06dbcdd8fe94d04cae859f026022549876ef958e.jpeg)

Any idea what is wrong? the buffer i pass in ipsf.add is correct because it have the same hash as initial file.

---

<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 9, 2020, 2:31pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/9 "2020-03-09T14:31:27Z")

</div>

> [@crashbdx](#):
>
> [https://ipfs.infura.io/ipfs](https://ipfs.infura.io/ipfs/QmdZTvj9)

I’m running some tests and the infura gateway tends to stall and close the connection pretty early, but once I fully download my file, the hash was the expected one. Switch to the [ipfs.io](http://ipfs.io) and test again. Also, try with a smaller, text file to verify, and potentially check what sort of corruption is happening (i.e. truncation or something else).

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 9, 2020, 2:50pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/10 "2020-03-09T14:50:22Z")

</div>

Thank you for your time, here an example that show the file is truncated or incomplete:

I just tested with a test.txt file with just hello! inside (still using infura host) and the hash match correctly.

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 9, 2020, 2:51pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/11 "2020-03-09T14:51:50Z")

</div>

If i switch the host: “[ipfs.infura.io](http://ipfs.infura.io)” with host: “[ipfs.io](http://ipfs.io)” im getting a ERR\_CONNECTION\_TIMED\_OUT, what is the correct host please?

---

<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 9, 2020, 2:57pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/12 "2020-03-09T14:57:45Z")

</div>

Any of [https://ipfs.github.io/public-gateway-checker/](https://ipfs.github.io/public-gateway-checker/) . But sometimes gateways (any gateway), may timeout trying to find the content.

However, ERR\_CONNECTION\_TIMED\_OUT is not something I would expect, as that means that the gateway is not even reachable from your location.

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 9, 2020, 3:07pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/13 "2020-03-09T15:07:32Z")

</div>

gateway-pinata-cloud give me ERR\_CONNECTION\_REFUSED  
gateway-ipfs-io give me ERR\_CONNECTION\_TIMED\_OUT  
cloudflare-ipfs-com give me ERR\_CONNECTION\_TIMED\_OUT  
ipfs-eternum-io give me ERR\_CONNECTION\_TIMED\_OUT

Basically only infura respond but seem to close before the end of the add (but still give me a hash) and i end up with a truncated file. My internet seem to work correctly. Maybe there is a blacklist for Philippines ip? ☹

---

<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 9, 2020, 6:47pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/14 "2020-03-09T18:47:14Z")

</div>

> [@crashbdx](#):
>
> Maybe there is a blacklist for Philippines ip? ☹

That might be the case… is website blocking common practice there?

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 9, 2020, 7:05pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/15 "2020-03-09T19:05:31Z")

</div>

I faced that few time for online payment but for something like file sharing its the first time, will test tomorow from my digital ocean hosting since the droplet is from another country and see if it work, i was testing locally from Philippines, i’ll keep updated. Thanks for your time.

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 9, 2020, 7:18pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/16 "2020-03-09T19:18:14Z")

</div>

Hum well actually it might not change anything since its client side:/

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 10, 2020, 3:11am UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/17 "2020-03-10T03:11:41Z")

</div>

So i changed my code a bit while using a new lib, ipfs-http-client vs ipfs-mini i was using yesterday, now things seem to work well and final file have the correct hash, still, im unable to use any of the ipfs gateway checker list host except infura…

here the current code:

```
import { sha256 } from "js-sha256";
import ipfsClient from "ipfs-http-client";
import "regenerator-runtime/runtime";
var buffer = require("buffer");

const ipfs = ipfsClient({
  host: "ipfs.infura.io",
  port: "5001",
  protocol: "https"
});

let progress_func = function(len) {
  console.log("File progress:", len)
};

const addDile = async function(buffer) {
  for await (const result of ipfs.add(buffer, {progress: progress_func})) {
    console.log(result);
  }
}

function upload() {
  const reader = new FileReader();
  reader.onloadend = function() {
    const buf = buffer.Buffer(reader.result); // Convert data into buffer
    const files = [
      {
        content: buf
      }
    ];
    console.log(sha256(buf));
    addDile(files);
  };
  const photo = document.getElementById("photo");
  reader.readAsArrayBuffer(photo.files[0]); // Read Provided File
}

document.getElementById("uploadImage").addEventListener("click", upload);

```

I here have another problem with the progress not showing untill the end but that off topic for now.

Would still love to understand why i cant use any other host 😕 is it the right place to ask here?

---

<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 10, 2020, 9:06am UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/18 "2020-03-10T09:06:12Z")

</div>

I can’t debug from your location. It seems you cannot connect. You should try tracerouting, verifying DNS is correct etc to try to diagnose the problem, but seems essentially a network problem.

---

<div class="post-metadata">

**Author:** ![crashbdx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/crashbdx/32/3558_2.png) [@crashbdx](https://discuss.ipfs.tech/u/crashbdx)\
**Post date:** [March 10, 2020, 1:24pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/19 "2020-03-10T13:24:11Z")

</div>

Thanks for your reply, not sure how to traceroute that to be honest but i did put a quick test online where we can change the host for easy testing.

Would you mind try this: [https://test-ipfs-ph.herokuapp.com/](https://test-ipfs-ph.herokuapp.com/)

With infura, with some other public gateway host and let me know if work for you? On my side only infura work.

Sorry i didnt put any visual response so must also open the console when testing.

Ps: Some friends from France tried and same, only infura works.

---

<div class="post-metadata">

**Author:** ![achingbrain](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/achingbrain/32/3083_2.png) [@achingbrain](https://discuss.ipfs.tech/u/achingbrain)\
**Post date:** [March 10, 2020, 2:05pm UTC](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440/20 "2020-03-10T14:05:45Z")

</div>

The hashes are different because you’re comparing the hash of the file data vs the hash of the DAG that represents the file.

There’s a great blog post about it here: [https://medium.com/textileio/whats-really-happening-when-you-add-a-file-to-ipfs-ae3b8b5e4b0f](https://medium.com/textileio/whats-really-happening-when-you-add-a-file-to-ipfs-ae3b8b5e4b0f)

The gateways you’re trying to connect to are supposed to be used to download content from IPFS via HTTP, not as programatic APIs.

So, for example when I paste “gateway.pinata.cloud” into the gateway box on your heroku demo app, everything times out, but I can fetch IPFS content from that gateway by visiting the url [https://gateway.pinata.cloud/ipfs/Qmfoo](https://gateway.pinata.cloud/ipfs/Qmfoo) where Qmfoo is a hash I want to download.

[Next page](https://discuss.ipfs.tech/t/file-hash-is-different-from-original/7440.md?page=2)
