keccak256 hash in Solidity - Converter

The keccak256 algorithm can hash an input to a fixed-length output of 32 bytes. It is a one-way cryptographic hash function, which means that the input can be converted to a hash. But it's not possible to reverse keccak256 to find the input for a given hash. keccak256 reacts also case-sensitive, Hello Word is not the same as hello world and therefore the result will be massively different. The keccak256 hashing algorithm belongs to the SHA-3 family.

// keccak256 of Hello World
// keccak256 of hello world

keccak hash converter

Use our keccak256 hash converter to convert an input into the corresponding keccak256 hash. Just paste some input into the field below and you will receive the corresponding keccak-256 hash. It is not possible to revert the operation so inserting an output to receive the input does not work.

What is keccak256 used for?

The keccak256 hashing algorithm is commonly used in the Solidity programming language for signature hashing. It can turn a larger input into a fixed-sized hash of 32 bytes. It can then be stored conveniently in the bytes 32 data type. It can also be used to protect against commit-reveal schemes and to create a deterministic one-of-a-kind ID of a data set. Let's say we have a big input of data and only one character changes then the keccak-256 hash of the data will be completely different. The keccak-256 algorithm can therefore be used to detect changes in data.

example of keccak256 in Solidity

In the following example, we create a contract named keccakExample and a function named get_keccak256 within the contract. The function accepts a string and returns keccak256 hash as bytes32 data type. If we compile and deploy the contract we can test it using the Hello World example. In case everything works fine it should return the keccak256 hash in bytes 32 (with 0x prefix): 0x592fa743889fc7f92ac2a37bb1f5ba1daf2a5c84741ca0e0061d243a2e6707ba It is recommended to encode the data first instead of hashing the raw data input. This is done using abi.encodePacked. The difference between abi.encode and abiencodePacked is that the encoded data is packed and therefore its data size is smaller. // SPDX-License-Identifier: MIT pragma solidity ^0.8.7; contract keccakExample { // convert string to keccak256 and return bytes32 function get_keccak256(string memory _str) public pure returns(bytes32) { return keccak256(abi.encodePacked(_str)); } } Code explanation: In line 1 we specify the spdx-license identifier as MIT license In line 2 we declare the pragma directive. It allows our smart contract to compile only on solidity version 0.8.7 and above but below version 0.9 In line 4 we declare a contract named keccakExample In line 6 we comment with an explanation of what the function will do In line 7 we declare a function named get_keccak256 as public and pure. It accepts a string data type in memory and returns a bytes32 data type. In line 8 we declare the return statement and encode the input using abi.encodePacked. The encoded input is then hashed using keccak256().



Click to jump to section