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.)

  • 1
    This is probably a better question to ask on stack overflow. It's more a general coding problem than an ethereum specific one – Joe May 16 '18 at 16:44

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

| improve this answer | |
  • What's the purpose of converting to base64 before hashing? – user19510 May 16 '18 at 17:04

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.

| improve this answer | |
  • Thank you. interesting approach. I converted my Excel file to csv file. Would you please tell me how to pass this csv file to smart contract as a string ? Because when csv file is open, it has a format similar to Excel file. (Consider this csv file is saved for example in this directory : C:\\example_csv_file) Thanks – Questioner May 16 '18 at 21:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.