REGEXVAULTv2.0
Security/Password Formats
Verified Safe

SHA-512 Hash Regex for Java

/^[a-f0-9]{128}$/i

What this pattern does

This page provides a lightweight, single-purpose regular expression for matching sha-512 hash, 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
// SHA-512 Hash
// ReDoS-safe | RegexVault — Security > Password Formats

import java.util.regex.Pattern;

public class Sha512HashValidator {
    private static final Pattern PATTERN =
        Pattern.compile("^[a-f0-9]{128}$");

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

    // Example
    public static void main(String[] args) {
        System.out.println(validate("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")); // true
    }
}

Test Cases

Matches (Valid)
Rejects (Invalid)
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3ecf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9c
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3eXX

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

SHA-512 is computationally slightly faster on 64-bit systems than SHA-256 due to internal 64-bit operations. For general hashing, SHA-256 is more widely supported.

Technical Notes

SHA-512 produces 512-bit (128 hex char) digests. Part of the SHA-2 family. Commonly used for HMAC signatures, certificate fingerprints, and file integrity. For passwords, use within PBKDF2-SHA512 or Argon2.

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