Skip to main content

How to Use Keccak256 Hash Function with Solidity

Updated on
Jul 3, 2023

4 min read

Overview​

Hash functions are essential tools in modern cryptography and blockchain technology. They enable us to securely store and transmit data by converting it into a fixed-length string of characters, known as a hash. One such hash function that has gained popularity in the blockchain community is Keccak256.

Keccak256 is a cryptographic hash function that takes an input of an arbitrary length and produces a fixed-length output of 256 bits. It is the function used to compute the hashes of Ethereum addresses, transaction IDs, and other important values in the Ethereum ecosystem. In this guide we will teach you more about the Keccak256 hash function and its use within Solidity and Ethereum.

What is Keccak256?​

Keccak256 is a member of the SHA-3 family of hash functions, which was selected as the winner of the NIST hash function competition in 2012. It was developed by a team of cryptographers led by Guido Bertoni, Joan Daemen, and Gilles Van Assche.

Keccak256 is designed to be secure against a wide range of attacks, including preimage attacks, collision attacks, and length extension attacks. It is also efficient, with a relatively low computational cost and a simple implementation.

Solidity and Keccak256​

Solidity is a programming language used to write smart contracts on the Ethereum blockchain. It includes built-in functions for computing hash values, including Keccak256.

To use Keccak256 in Solidity, you can call the built-in function keccak256 and pass in the data you want to hash as an argument. The function returns the hash value as a bytes32 object.

Here's an example of how to use Keccak256 in Solidity:

function hash(string memory _message) public {
messageHash = keccak256(bytes(_message));
}

In this example, the Solidity function hash takes a string input _message and calculates its keccak256 hash value, storing the result in the variable messageHash.

A complete example of a contract with hashing functionality:

pragma solidity ^0.8.0;

contract HashContract {
bytes32 private messageHash;

function hash(string memory _message) public {
messageHash = keccak256(bytes(_message));
}

function getMessageHash() public view returns (bytes32) {
return messageHash;
}
}

The contract above has two functions: hash to calculate the hash value of a message and getMessageHash to return the hash value stored in the contract. Feel free to copy and paste the code into Remix.IDE to test it out.

Things to Keep in Mind​

When using Keccak256, there are several important things to be aware of:

  1. Collision resistance: Keccak256 is designed to be collision-resistant, meaning finding two inputs that produce the same hash output should be extremely difficult. However, it's important to note that collision resistance is not absolute, and there is always a small chance of collision. Therefore, it's recommended to use a combination of unique input parameters (e.g., block hash + block timestamp + contract nonce) for a lower probability of collision.
  2. Pre-image resistance: Keccak256 is also designed to be pre-image resistant, meaning it should be nearly impossible to determine the original input from the hash output. However, it's important to note that brute-force attacks can still be attempted, and stronger passwords or keys will increase security.
  3. Key length: Keccak256 outputs a 256-bit hash value, which means that it has a very large output space. This makes it resistant to brute-force attacks, but it's important to ensure that the key length is also sufficient for the application.
  4. Implementation: It's important to ensure that the implementation of Keccak256 used is secure and free from vulnerabilities. Additionally, the implementation should be updated regularly to ensure that any discovered vulnerabilities are patched.
  5. Side-channel attacks: Keccak256 is vulnerable to side-channel attacks, which means that attackers can potentially exploit weaknesses in the hardware or software used to perform the hash function. It's important to use hardware and software that is resistant to such attacks to ensure the security of the hash function.

More Use Cases​

Keccak256 can be used in many different ways in Solidity, depending on your needs. Some common use cases include:

  • Generating random numbers: You can use Keccak256 to generate random numbers by hashing a seed value with a block timestamp or block hash.
  • Verifying digital signatures: You can use Keccak256 to verify digital signatures by hashing the signed message and comparing it to the expected hash value.
  • Creating unique identifiers: You can use Keccak256 to create unique identifiers for entities in your smart contract, such as tokens or accounts.

Final Thoughts​

Keccak256 is a powerful tool for securing and manipulating data in Solidity smart contracts. Its efficient implementation and strong security guarantees have become a popular choice for developers in the Ethereum ecosystem.

We hope this guide has been helpful in getting you started with using Keccak256 in your Solidity code. If you're having trouble, have questions, or just want to talk about what you're building, drop us a line on Discord or Twitter!

We <3 Feedback!​

If you have any feedback on this guide, let us know. We'd love to hear from you!

Share this guide