Skip to main content

Try it Live

Run Keccak256 examples in the interactive playground
Future Plans: This page is planned and under active development. Examples are placeholders and will be replaced with accurate, tested content.
View executable examples: hash-bytes.ts | hash-string.ts | hash-hex.ts | merkle-tree.ts
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 branded Uint8Array 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:
Type structure:
  • Base: Uint8Array (32 bytes)
  • Brand: { readonly [brand]: "Bytes32" }
  • Size: { readonly size: 32 }
  • Semantic: { readonly [Symbol.for("keccak256")]: true }
This layered branding provides:
  • 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:
When to use each:
  • Keccak256Hash - Transaction hashes, block hashes, merkle nodes, keccak256 outputs
  • Bytes32 - Storage slots, generic 32-byte values, numeric conversions
See Bytes32 documentation for generic 32-byte operations.

Migration from Hash Primitive

Old Hash primitive replaced by Keccak256Hash for clarity:
Migration steps:
  1. Replace Keccak256.hash() with Keccak256.hash()
  2. Replace HashType type with Keccak256Hash
  3. All operations remain the same (same 32-byte structure)
The new approach is more explicit about which hash algorithm produced the value.

When to Use Keccak256Hash vs Bytes32

Use Keccak256Hash when:

Use Bytes32 when:

Constructors

All Keccak256Hash values come from Keccak256 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):
See Keccak256 index for all hash methods.

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:
See Bytes32 documentation for all operations.

Ethereum Use Cases

Transaction Hashes

Block Hashes

Merkle Tree Nodes

Event Topics

Address Derivation

Storage Proofs