What is Keccak-256?
- Keccak-256 is a mathematical function that takes any input (like text, numbers, or data). And gives you a fixed 256-bit output (which looks like a long random string of letters and numbers).
- It’s a kind of hash function.
👉 Example:
- Input:
"hello"
- Output:
06d0...a7c3
(a 64-character hex string)
No matter how big or small the input is, the output length is always the same.
Why is it used in Ethereum?
Ethereum uses Keccak-256 for a lot of important things:
- To create addresses from public keys.
- To make sure data can’t be easily faked.
- To index and look up events and logs in smart contracts.
- To secure digital signatures.
It’s like the fingerprint system of Ethereum—unique, fixed, and impossible to reverse.
Why not just say SHA-256?
- SHA-256 (used in Bitcoin) and Keccak-256 are cousins.
- Keccak actually won the competition to become SHA-3, but Ethereum started using it before the final SHA-3 version was standardized.
- So, Ethereum’s version is slightly different from official SHA-3. That’s why we say Keccak-256, not SHA3-256.
Key Properties
- One-way street: You can go from input → output, but not back.
- Unique fingerprints: Even a tiny change in input makes a totally different output.
- Fast and secure: Perfect for blockchain.
✅ In short:
Keccak-256 is the hashing function Ethereum uses to secure data, create unique IDs, and make addresses. Think of it as the magic blender that turns any data into a unique, unbreakable fingerprint.
🚀Let’s see Keccak-256 in action with some tiny code examples
🔹 1. In Python
You can use the pysha3
library:
import sha3 # part of pysha3
# input
data = b"hello"
# create keccak-256 object
keccak = sha3.keccak_256()
keccak.update(data)
# output hash (hex format)
print(keccak.hexdigest())
👉 Output (always the same for “hello”):
06d0...a7c3
🔹 2. In Solidity (Ethereum Smart Contract)
Solidity has keccak256
built-in:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Example {
function getHash(string memory input) public pure returns (bytes32) {
return keccak256(abi.encodePacked(input));
}
}
👉 If you call getHash("hello")
, you’ll get the same 32-byte hash value.
🔹 3. In JavaScript (Node.js / ethers.js)
With ethers.js library:
const { keccak256, toUtf8Bytes } = require("ethers");
let data = "hello";
let hash = keccak256(toUtf8Bytes(data));
console.log(hash);
👉 Output:
0x06d0...a7c3
💡 Notice:
- No matter what language you use, the hash result is always the same for the same input.
- Change just one letter → the hash becomes completely different.
😃 Let’s go step by step and see how Ethereum creates an address from a public key using Keccak-256
🔹 Step 1: Start with a private key
- A private key is just a random 256-bit number (a big secret number).
- Example (hexadecimal, shortened):
0xa1b2c3...ff99
This private key must be kept secret.
🔹 Step 2: Generate the public key
- From the private key, we do Elliptic Curve Cryptography (ECC) math (specifically secp256k1 curve).
- This gives us a public key (a long 512-bit number).
- Example (shortened):
0x04bc...d932
⚡ Public key can be shared, private key must stay hidden.
🔹 Step 3: Apply Keccak-256
- Take the public key and run it through Keccak-256 (Ethereum’s hash function).
👉 Example:
keccak256(public_key) = 0x87a1f3c6...b45c
🔹 Step 4: Take the last 20 bytes
- The Keccak-256 output is 32 bytes (64 hex characters).
- An Ethereum address is only 20 bytes (40 hex characters).
- So we keep the last 20 bytes of the hash.
👉 Example:
0x87a1f3c6...b45c → 0xf3c6...b45c (Ethereum address)
✅ Final Result: Ethereum Address
- You now have an Ethereum address derived from your private key.
- Example:
0xf3c6d932...b45c
This is what you share with others to receive ETH or tokens.
🔎 Recap in plain words:
- Start with a private key (secret).
- Use math (ECC) to get the public key.
- Hash the public key with Keccak-256.
- Take the last 20 bytes → Ethereum address.