keccak256
Calculates the Keccak256 hash of a byte array or hex value
keccak256
Calculates the Keccak256 hash of a byte array or hex value. This is the primary hash function used in Ethereum.
Import
import "github.com/ChefBingbong/viem-go/utils/hash"Usage
import "github.com/ChefBingbong/viem-go/utils/hash"
// Hash bytesh := hash.Keccak256([]byte("hello world"))// "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
// Hash hex stringh = hash.Keccak256("0x68656c6c6f20776f726c64")// "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
// Hash string (UTF-8)h = hash.Keccak256("hello world")// "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
// Get raw bytesbytes := hash.Keccak256Bytes([]byte("hello world"))// []byte{...}Returns
- Type:
stringforKeccak256,[]byteforKeccak256Bytes
The hashed value. Keccak256 returns a hex string with 0x prefix, Keccak256Bytes returns raw bytes.
Parameters
value (required)
- Type:
any(supports[]byteorstring)
The hex value or byte array to hash. If a string is provided without 0x prefix, it's treated as UTF-8.
// Hash byteshash := hash.Keccak256([]byte("hello"))
// Hash hex stringhash = hash.Keccak256("0x68656c6c6f")
// Hash UTF-8 stringhash = hash.Keccak256("hello")Functions
Keccak256
Computes Keccak256 hash and returns a hex string:
hash := hash.Keccak256([]byte("hello"))
// "0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"
Keccak256Bytes
Computes Keccak256 hash and returns raw bytes:
bytes := hash.Keccak256Bytes([]byte("hello"))
// []byte{0x1c, 0x8a, 0xff, ...}