REGEXVAULTv2.0
Security/Password Formats
Verified Safe

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

/^\$(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 Java. 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 Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.

Java Implementation

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

import java.util.regex.Pattern;

public class Argon2HashArgon2iArgon2dArgon2idValidator {
    private static final Pattern PATTERN =
        Pattern.compile("^\\$(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}))$");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("$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 Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. 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