eth-hash: Ethereum Keccak256 Hashing Function
eth-hash is a low-level Python library that provides the Ethereum hashing function, Keccak256. While sometimes erroneously referred to as SHA3, it implements the specific Keccak variant used in Ethereum. It is primarily intended for internal use by other Ethereum development tools. The current version is 0.8.0, with releases driven by the needs of the broader Ethereum ecosystem.
Warnings
- gotcha Ethereum's hashing algorithm is Keccak256, not SHA3. While often colloquially referred to as SHA3, it is a distinct function. Using libraries or functions explicitly named 'SHA3' might lead to incorrect hash values for Ethereum contexts.
- breaking The `eth-hash` library requires a backend to be installed for the `keccak` function to be available. Installing just `eth-hash` will not provide the hashing functionality out-of-the-box.
- gotcha eth-hash is a low-level utility. If you're looking for a more convenient and feature-rich hashing tool that integrates with other Ethereum utilities, `eth_utils.keccak()` is often a friendlier alternative built on top of `eth-hash`.
Install
-
pip install "eth-hash[pycryptodome]" -
pip install eth-hash
Imports
- keccak
from eth_hash.auto import keccak
- Keccak256
from eth_hash import Keccak256 from eth_hash.backends import pysha3 keccak = Keccak256(pysha3)
Quickstart
from eth_hash.auto import keccak
# Hash a byte string
data_to_hash = b'hello world'
hashed_data = keccak(data_to_hash)
print(f"Hashed data: {hashed_data.hex()}")
# Incremental hashing
preimage = keccak.new(b'part-a')
preimage.update(b'part-b')
incremental_hash = preimage.digest()
print(f"Incremental hash: {incremental_hash.hex()}")