Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Keccak256
  • Keccak256 - Web3js
  1. STEP 5

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: 0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1
  • Example 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:

  • 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:

  • 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:

Further Reading


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

// Generate a hash of a password
var passwordHash = web3.utils.keccak256("password");
console.log(passwordHash); 
// Output: 0x91e7c79f3ea3d8f7b77d0e0ce7cc9f9d8b7cddd16fbeec717e2c3b5f426b2a5a

// Generating an Ethereum Address

// Generate an Ethereum address from a public key
var publicKey = "0x04de9c3f3ece1f2c3f7fab41c3e8ffd4d4b6b4bf9ea9f8b7e22d28f6a7f013930c8be4f4bb55d4c724a9a937d7e8f0d0f788f32c12f6ccd8cda9a6e9f9c9a3a3f"; 
var address = web3.utils.keccak256(publicKey);
console.log(address); 
// Output: 0xC05D5F5F5f991E719EDAA0fFcc7fcd9F9b8D8d6a

That's it for the lesson 47! In the next lesson, Function Signature Hash

Previous46: ABI DecodedNext48: Function Signature Hash

Last updated 1 year ago

For more information on Keccak256 and its usage in Solidity, please see the official .

Solidity documentation