or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

@ethersproject/keccak256

@ethersproject/keccak256 provides the KECCAK256 hash function implementation used throughout the Ethereum ecosystem. It serves as a thin wrapper around the js-sha3 library, specifically providing the keccak_256 variant (distinct from SHA-3) that Ethereum uses as its primary hash function for operations like generating addresses, creating merkle trees, or verifying data integrity.

Package Information

  • Package Name: @ethersproject/keccak256
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @ethersproject/keccak256

Core Imports

import { keccak256 } from "@ethersproject/keccak256";

For CommonJS:

const { keccak256 } = require("@ethersproject/keccak256");

Basic Usage

import { keccak256 } from "@ethersproject/keccak256";

// Hash a hexadecimal string
const hash1 = keccak256("0x1234");

// Hash a Uint8Array
const hash2 = keccak256(new Uint8Array([0x12, 0x34]));

// Hash an array of numbers
const hash3 = keccak256([0x12, 0x34]);

// All return 64-character hex strings prefixed with "0x"
console.log(hash1); // e.g., "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"

// Empty input produces the known empty hash
console.log(keccak256("0x")); // "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"

Capabilities

KECCAK256 Hash Function

Computes the KECCAK256 hash of the provided data, returning a hexadecimal string representation prefixed with "0x".

/**
 * Computes the KECCAK256 hash of the provided data
 * @param data - Input data to hash (can be string, Uint8Array, or other byte-like formats)
 * @returns Hexadecimal string representation of the hash prefixed with "0x"
 * @throws Error if the input data is invalid (e.g., malformed hex strings, odd-length hex)
 */
function keccak256(data: BytesLike): string;

Types

/**
 * BytesLike represents data that can be converted to bytes
 * From @ethersproject/bytes package
 */
type BytesLike = ArrayLike<number> | string;

/**
 * ArrayLike represents array-like structures with numeric indices
 */
interface ArrayLike<T> {
  readonly length: number;
  readonly [n: number]: T;
}

The BytesLike type accepts:

  • Strings: Hexadecimal strings (with or without "0x" prefix)
  • Arrays: Regular arrays of numbers (0-255), e.g., [0x12, 0x34]
  • Typed Arrays: Uint8Array, Uint16Array, etc.
  • Array-like objects: Any object with length property and numeric indices