Keccak256 hash function in Solidity

You can use Keccak256 hashing to authenticate payloads of data. For example, consider an object that generates a unique hash. This hash can be saved on the blockchain along with information such as who saved it, when it was saved, etc. Later, when someone is presented with the object from some other source, they can compute the hash themselves and then consult the blockchain for authenticity. The blockchain would either respond that there is no known object with that hash (inauthentic), or it would be able to respond with the recorded details. This is just one of the many use cases for hashing.

Remember you can create a unique hash from an input but you can not determine the input from the hash. It is a one way street that provides unique authenticity.

Keccak is a family of cryptographic hash functions that eventually got standardized to SHA-3. Ethereum called it Keccak instead of SHA-3 as it has slightly different parameters than the current SHA-3. Keccak256 is a cryptographic function built into solidity. This function takes in any amount of inputs and converts it to a unique 32 byte hash.

The sample contract below takes a text value, a number and an Ethereum address and converts it to a unique 32 byte hash using Keccak256. It is a simple demonstration on how values can be hashed into a unique id.

Try out with the sample contract below. Input text, a number and an Ethereum address into the function and note the bytes32 output (hash). Then change one of the parameters, regenerate the bytes32 output and you will notice that the hash changes. Finally change all parameters back to what you started with and the bytes32 output (hash) will be the same as the original hash.

pragma solidity ^0.6.0;

//input text, number and address
//output is a unique 32 byte hash
contract hashtest {
    function hash(string memory _text, uint _num, address _addr) public pure returns (bytes32) {
        return keccak256(abi.encode(_text, _num, _addr));
    }
}

Try it in Remix

Resources

Blockchain Networks

Below is a list of EVM compatible Mainnet and Testnet blockchain networks. Each link contains network configuration, links to multiple faucets for test ETH and tokens, bridge details, and technical resources for each blockchain. Basically everything you need to test and deploy smart contracts or decentralized applications on each chain. For a list of popular Ethereum forums and chat applications click here.

Ethereum test network configuration and test ETH faucet information
Optimistic Ethereum Mainnet and Testnet configuration, bridge details, etc.
Polygon network Mainnet and Testnet configuration, faucets for test MATIC tokens, bridge details, etc.
Binance Smart Chain Mainnet and Testnet configuration, faucets for test BNB tokens, bridge details, etc.
Fanton networt Mainnet and Testnet configuration, faucets for test FTM tokens, bridge details, etc.
Kucoin Chain Mainnet and Testnet configuration, faucets for test KCS tokens, bridge details, etc.

Web3 Software Libraries

You can use the following libraries to interact with an EVM compatible blockchain.

Next Review – More resources

Ledger Nano X - The secure hardware wallet

Leave a Reply