viem-goviem-go

isHash

Checks if a string is a valid 32-byte hash

isHash

Checks if a string is a valid 32-byte hash (64 hex characters with 0x prefix).

Import

import "github.com/ChefBingbong/viem-go/utils/hash"

Usage

import "github.com/ChefBingbong/viem-go/utils/hash"
// Valid hash
valid := hash.IsHash("0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad")
// true
// Invalid (too short)
invalid := hash.IsHash("0x1234")
// false
// Invalid (no prefix)
invalid = hash.IsHash("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad")
// false

Returns

  • Type: bool

true if the string is a valid 32-byte hash, false otherwise.

Parameters

hash (required)

  • Type: string

The string to validate. Must be exactly 66 characters (0x + 64 hex characters).

valid := hash.IsHash("0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad")
// true
invalid := hash.IsHash("0x1234")
// false

Notes

  • Validates that the string has exactly 64 hex characters after the 0x prefix
  • Only accepts lowercase and uppercase hex characters (0-9, a-f, A-F)
  • Used to validate transaction hashes, block hashes, and other 32-byte values