IPFS hash verification in nodejs

I want to develop a nodejs app in which a user stores a file on IPFS and gives the hash to the app.
What I would like to do is to verify that the contents under this given hash produce the same hash, something similar to verifying the checksum.
Is there a way to do it using the ipfs-api module?

For example:

A user writes a “Hello world” string in the “data.txt” file, adds the file on IPFS and gets the hash: QmWpXFXgii2CfYMBXj9jihPcokpFhwCjGQqOipZ1LVGgTU

Then he runs my app with the IPFS hash:
node myapp.js QmWpXFXgii2CfYMBXj9jihPcokpFhwCjGQqOipZ1LVGgTU

I would like to verify that the contents under this hash are “Hello world” and he didn’t provide another hash.

I’m not sure I really understand what you are after.

Does your app know “Hello world” and you would like to know that hash of it? If yes you could try the “onlyHash” option.

Basically I want to read from an IPFS hash and get the contents of the file stored under this address, then I want to verify that the contents of the file produce actually the given hash.

In other words, a user claims that a file with “Hello world” gives this hash: QmWpXFXgii2CfYMBXj9jihPcokpFhwCjGQqOipZ1LVGgTU but when I read under this hash there is another file with “Hello”.
Actually I want to make sure that the hash that the user gives me is what he claims to be.

PS. In ipfs-api npm package I didn’t find an “onlyHash” option…

So it sounds like you want the “only hash” option. You would take the file and then add with onlyHash: true and you’ll get the hash of the file back. Then you can verify if it’s the same.

Though that shouldn’t actually be needed as if you retrieve a file with a certain hash from IPFS it’s guaranteed that the contents hashes to that hash.

If you run jsipfs add --help you’ll see the --only-hash option which maps to onlyHash when you use the module.