SHA-3 / Keccak Hash (256 and 512) Regex for Go
/^[a-f0-9]{64}$|^[a-f0-9]{128}$/iWhat this pattern does
This page provides a well-structured, multi-part regular expression for matching sha-3 / keccak hash (256 and 512), ported and verified for Go. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.
Go Implementation
// SHA-3 / Keccak Hash (256 and 512)
// ReDoS-safe | RegexVault — Security > Password Formats
package validation
import "regexp"
var sha3KeccakHash256And512Re = regexp.MustCompile(`^[a-f0-9]{64}$|^[a-f0-9]{128}$`)
func ValidateSha3KeccakHash256And512(s string) bool {
return sha3KeccakHash256And512Re.MatchString(s)
}
// Example
// fmt.Println(ValidateSha3KeccakHash256And512("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")) // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a | short |
0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e | not_hex_chars!! |
| — | a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434 |
When to use this pattern
This pattern is drawn from the Security > Password Formats category and carries a ReDoS-safe certification. That matters for Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Ethereum uses Keccak-256 (a variant of SHA-3 that predates the final FIPS 202 standardization). Keccak-256 ≠ SHA3-256. Ethereum addresses use the Keccak-256 variant, which produces different output than standard SHA3-256.
Technical Notes
SHA-3 (Standardized 2015, FIPS 202) uses a completely different internal construction (Keccak sponge) from SHA-2. Resistant to length-extension attacks that affect SHA-2. Note: SHA3-256 and SHA-256 produce the same output length (64 hex) — distinguish by context/system documentation.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern