keccak256 โ
Calculates the Keccak256 hash of a byte array or hex value.
This function is a re-export of keccak_256 from @noble/hashes โ an audited & minimal JS hashing library.
Install โ
ts
import { keccak256 } from 'viem'Usage โ
ts
import { keccak256 } from 'viem'
keccak256(new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
// 0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0
keccak256('0xdeadbeef')
// 0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1
// hash utf-8 string
keccak256(toHex('hello world'))
// 0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0Returns โ
Hex | ByteArray
The hashed value.
Parameters โ
value โ
- Type: Hex | ByteArray
The hex value or byte array to hash.
to โ
- Type: "bytes" | "hex"
- Default: "hex"
The output type.
ts
import { keccak256 } from 'viem'
keccak256(
  new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33],
  'bytes' 
)
// Uint8Array [62, 162, 241, 208, 171, 243, 252, 102, 207, 41, 238, 187, 112, 203, 212, 231, 254, 118, 46, 248, 160, 155, 204, 6, 200, 237, 246, 65, 35, 10, 254, 192] 
