0

In Solidity, let's say there are two contracts A and B with the same code as in the following example. By the way, do they return the same bytes32 value when retrieving ROLE? Why do we use keccak256 if they do?

// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

contract Role {
    bytes32 public constant ROLE = keccak256("UNIQUE_ROLE");
}

1 Answer 1

0

Yes, they do return the same byte32 value (0x9771d926084b13c57fdc8a6aa0191a193b7073d41abe0530248e4210f095c4e4) when retrieving ROLE (I've tested this in Remix). The way hash functions work is if you hash the same input with the same function, you will always get the same result.

You can use Keccak256 hashing in the following scenario:

  • To create a deterministic, one-of-a-kind ID from a set of data.

  • Commit-reveal scheme

  • Cryptographic signature with a small size (by signing the hash instead of a larger input)

See:

https://medium.com/0xcode/hashing-functions-in-solidity-using-keccak256-70779ea55bb0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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