1

I'm developing my first dapp with solidity and I was wondering in which cases should I use hashing functions like keccak. I know how they work but it is not clear to me when should I use them and to do what. Thank you so much

closed as off-topic by user19510, Achala Dissanayake, eth Aug 31 '18 at 6:41

  • This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

1

They can be used to authenticate large payloads/documents stored in other places. For example, consider a large object that produces a certain hash. That hash can be stored in a contract along with details such as who stored it, when it was stored, and other application-specific details.

Later, when someone is presented with the document from some other source, they can compute the hash themselves and then consult the contract. The contract would either respond that there is no known object with that hash (inauthentic), or it would be able to respond with the recorded details.

Hope it helps.

1

One use case is using it for string comparisons. In Solidity, you cannot directly compare two strings:

function compareStrings() returns(bool) {
    string a = "test";
    return a == a;
}

The above code does not compile, and gives an error that states TypeError: Operator == not compatible with types string storage ref and string storage ref

The way to properly compare strings is to compare the hash of the two strings. The following returns true:

function compareStrings() returns(bool) {
    string a = "test";
    return keccak256(a) == keccak256(a);
}

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