How to store hash of an Excel file in Blockchain?

2018-05-16 17:22:22

I'm sorry if this question does not seem to be logic.

I want to store hash of an Excel file in Blockchain by following function :

//byte fileHash;

event LogsetFileHash(byte _hashValue);

function setFileHash(byte hashValue) {

emit LogsetFileHash(hashValue);

//fileHash = hashValue; // In case storing hash value in a variable is NOT expensive, we store hash in a variable, otherwise, we save it only as an event log.


The question is how can I use Mathematical and Cryptographic Functions to convert the content of my Excel file to a hash value, regarding this fact that the content of an Excel file is not a string/text like a pdf file.

In fact what is not clear for me is that how to use one of this function like : keccak256(...) returns (bytes32) or sha256(...) returns (bytes32) to return _hashValue variable ? And also which one is better to use ? (if there is a difference in size of hash.)

You can convert your file in base64 and calc

  • You can convert your file in base64 and calculate the hash of the string sequence. You need to perform this operation off-chain before submitting the transaction to the smart contract.

    For example index.js:

    var fs = require('fs');

    var keccak256 = require('js-sha3').keccak256;

    var hash1 = hashFile('sample1.xlsx');

    console.log("sample1.xlsx: " + hash1);

    var hash2 = hashFile('sample2.xlsx');

    console.log("sample2.xlsx: " + hash2);

    function hashFile(file) {

    var body = fs.readFileSync(file);

    return keccak256(body.toString('base64'));


    $ node index.js

    sample1.xlsx: 6f908b0facccb5008eb05214a7406300192a59db69bf7c2d979bfa8404b094b1

    sample2.xlsx: 3d4e1062085c24da7af7c955d54fc89fa5670dc74bc0521db46ec3ceb4ef9109

    Then the hash can be casted in a bytes32 variable:

    event LogsetFileHash(bytes32 _hashValue);

    function setFileHash(bytes32 _hashValue) {

    emit LogsetFileHash(_hashValue);


    Sample Code

    2018-05-16 17:35:53
  • I would use Greg's method but if really you want the hash function to be in the contract you could convert the file to csv or json and pass it to the contract as a string, to be converted.

    It's not possible to read a file directly using a contract. Ethereum needs everyone to be able to independently validate the outcome, this would require every validator to have access to the file, which is unfeasible.

    2018-05-16 17:45:15