REGEXVAULTv2.0
Security/Password Formats
Verified Safe

Argon2 Hash (Argon2i, Argon2d, Argon2id) Regex for Go

/^\$(argon2(?:i|d|id))\$v=19\$m=([0-9]+),t=([0-9]+),p=([0-9]+)(?:,keyid=[A-Za-z0-9+/]{0,11}(?:=*))?(?:,data=[A-Za-z0-9+/]{0,43}(?:=*))?\$([A-Za-z0-9+/]{11,64}(?:={0,2}))\$([A-Za-z0-9+/]{16,86}(?:={0,2}))$/

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching argon2 hash (argon2i, argon2d, argon2id), 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

Go
// Argon2 Hash (Argon2i, Argon2d, Argon2id)
// ReDoS-safe | RegexVault — Security > Password Formats

package validation

import "regexp"

var argon2HashArgon2iArgon2dArgon2idRe = regexp.MustCompile(`^\$(argon2(?:i|d|id))\$v=19\$m=([0-9]+),t=([0-9]+),p=([0-9]+)(?:,keyid=[A-Za-z0-9+/]{0,11}(?:=*))?(?:,data=[A-Za-z0-9+/]{0,43}(?:=*))?\$([A-Za-z0-9+/]{11,64}(?:={0,2}))\$([A-Za-z0-9+/]{16,86}(?:={0,2}))$`)

func ValidateArgon2HashArgon2iArgon2dArgon2id(s string) bool {
    return argon2HashArgon2iArgon2dArgon2idRe.MatchString(s)
}

// Example
// fmt.Println(ValidateArgon2HashArgon2iArgon2dArgon2id("$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG")) // true

Test Cases

Matches (Valid)
Rejects (Invalid)
$argon2id$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG$argon2id$v=18$m=65536,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub
$argon2$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG

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

Argon2 is the winner of the Password Hashing Competition (2015) and is preferred over bcrypt for new systems. Ensure the memory parameter is high enough — low memory values defeat the memory-hardness property.

Technical Notes

Argon2id is the recommended variant (memory-hard + side-channel resistant). Parameters: m=memory in KiB, t=time iterations, p=parallelism. OWASP recommends argon2id with m=19456 (19 MiB), t=2, p=1 as minimum. v=19 is the current version.

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