hashMessage
Hashes a message in EIP-191 format
hashMessage
Calculates an Ethereum-specific hash in EIP-191 format: keccak256("\x19Ethereum Signed Message:\n" + len(message) + message)).
Import
import "github.com/ChefBingbong/viem-go/utils/signature"Usage
import "github.com/ChefBingbong/viem-go/utils/signature"
// Hash a string messagehash := signature.HashMessage(signature.NewSignableMessage("hello world"))// "0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68"
// Hash hex datahash = signature.HashMessage(signature.NewSignableMessageHex("0x68656c6c6f20776f726c64"))// "0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68"
// Get hash as byteshashBytes := signature.HashMessageBytes(signature.NewSignableMessage("hello world"))// []byte{...}Returns
- Type:
stringforHashMessage,[]byteforHashMessageBytes
The hashed message. HashMessage returns a hex string with 0x prefix, HashMessageBytes returns raw bytes.
Parameters
message (required)
- Type:
signature.SignableMessage
The message to hash. Use signature.NewSignableMessage() for string messages or signature.NewSignableMessageHex() for hex data.
// Hash string messagehash := signature.HashMessage(signature.NewSignableMessage("hello world"))
// Hash hex datahash = signature.HashMessage(signature.NewSignableMessageHex("0x68656c6c6f20776f726c64"))Functions
HashMessage
Hashes a message and returns a hex string:
hash := signature.HashMessage(signature.NewSignableMessage("hello world"))
// "0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68"
HashMessageBytes
Hashes a message and returns raw bytes:
hashBytes := signature.HashMessageBytes(signature.NewSignableMessage("hello world"))
// []byte{...}
Message Format
The message hash is computed as:
keccak256("\x19Ethereum Signed Message:\n" + len(message) + message)
This format ensures that messages signed for Ethereum cannot be replayed on other platforms.