The keccak256 hash function for ethers.
npx @tessl/cli install tessl/npm-ethersproject--keccak256@5.8.0@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.
npm install @ethersproject/keccak256import { keccak256 } from "@ethersproject/keccak256";For CommonJS:
const { keccak256 } = require("@ethersproject/keccak256");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"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;/**
* 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:
[0x12, 0x34]