Try it Live
Run Keccak256 examples in the interactive playground
Semantic Hash Type - Keccak256Hash extends Bytes32 with a keccak256 semantic flag. Same runtime representation (32 bytes), different compile-time meaning for type safety.
Overview
Keccak256Hash is a brandedUint8Array that extends Bytes32 with additional semantic meaning. It represents the output of a keccak256 hash operation through TypeScript branding.
While Bytes32 is a generic 32-byte value, Keccak256Hash explicitly communicates “this came from a keccak256 operation” at compile-time with zero runtime overhead.
Type Definition
Keccak256Hash extends Bytes32 with a keccak256 semantic symbol:- Base:
Uint8Array(32 bytes) - Brand:
{ readonly [brand]: "Bytes32" } - Size:
{ readonly size: 32 } - Semantic:
{ readonly [Symbol.for("keccak256")]: true }
- Runtime: Plain Uint8Array (zero overhead)
- Compile-time: Type safety preventing mixing hash types
- Semantic: Documents this value came from keccak256
Relationship to Bytes32
Keccak256Hash and Bytes32 are the same size (32 bytes) but convey different semantics:- Keccak256Hash - Transaction hashes, block hashes, merkle nodes, keccak256 outputs
- Bytes32 - Storage slots, generic 32-byte values, numeric conversions
Migration from Hash Primitive
OldHash primitive replaced by Keccak256Hash for clarity:
- Replace
Keccak256.hash()withKeccak256.hash() - Replace
HashTypetype withKeccak256Hash - All operations remain the same (same 32-byte structure)
When to Use Keccak256Hash vs Bytes32
Use Keccak256Hash when:
Use Bytes32 when:
Constructors
All Keccak256Hash values come fromKeccak256 module methods:
Keccak256.hash
Direct hashing of bytes:Keccak256.hashString
Hash UTF-8 string:Keccak256.hashHex
Hash hex-encoded string:Keccak256.hashMultiple
Hash multiple chunks:Keccak256.topic
Event topic (full 32-byte hash):Conversions
Keccak256Hash can be converted using Bytes32 operations:toHex
Convert to hex string:toBytes
Convert to plain Uint8Array:toBigint
Convert to bigint (big-endian):toAddress
Extract address (last 20 bytes):Operations
equals
Check equality:compare
Compare two hashes:isZero
Check if all zeros (rare for cryptographic hashes):clone
Create independent copy:Ethereum Use Cases
Transaction Hashes
Block Hashes
Merkle Tree Nodes
Event Topics
Address Derivation
Storage Proofs
Related
- Keccak256 Overview - Main keccak256 documentation
- Bytes32 - Generic 32-byte type
- Hash Methods - All keccak256 hash functions
- Ethereum Methods - Ethereum-specific operations
- Implementations - Performance comparison
- Transaction - Transaction hashing
- Address - Address derivation

