47: Keccak256
Keccak256
Keccak256 is a hashing algorithm used in Ethereum Blockchain. It is a member of the SHA-2 family and was developed by the team behind the SHA-3 standard. Keccak256 is used to hash transactions and data stored on the Ethereum blockchain and is used as part of the consensus algorithm to determine the validity of transactions.
bytes32 hash = keccak256(abi.encode(data1, data2));Example
contract MyContract {
// Generate the Keccak256 hash of a string
function keccak256Hash(string memory _string) public pure returns (bytes32) {
return keccak256(abi.encode(_string));
}
}
// input: "Hello World"
// output: 0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1Example 1 The following example will demonstrate how Keccak256 works:
Let's say we have a string Hello World and we want to hash it using Keccak256.
The steps are as follows:
Calculate the SHA-3 hash of the string using the Keccak-256 algorithm. The result is the hash: 0x36a9e7f1c95b82ffb99743e0c5c4ce95d83c9a430aac59f84ef3cbfab6145068
Example 2 Let's take another example, this time with a hexadecimal string:
Calculate the SHA-3 hash of the string 0x1234 using the Keccak-256 algorithm. The result is the hash: 0x1cb9d9f0cb8f7c8b4de1b7e5b5f8c37f5ebf64a86a9f965a5db5c5b5f2f2e1e2
Example 3 Finally, let's take an example with a longer string:
Calculate the SHA-3 hash of the string Hello Ethereum! using the Keccak-256 algorithm. The result is the hash: 0x7f5f5c5f25d5bcf8c25ba15f2a9f912fb5e5c4f4b3413fcee50f99a4d234c6f4
Further Reading
For more information on Keccak256 and its usage in Solidity, please see the official Solidity documentation.
Keccak256 - Web3js
Keccak256 is a cryptographic hash function used to generate a fixed-size string of characters from a given input. It is used in Ethereum to generate the address of an account from the public key.
Usage Keccak256 is used to generate cryptographic hash values from input data. It can be used to process data such as passwords, private keys, and Merkle root hashes. It is also used to generate the address of an Ethereum account from a public key.
Examples Here are some examples of how to use the Keccak256 function:
Hashing a Password
That's it for the lesson 47! In the next lesson, Function Signature Hash
Last updated