REGEXVAULTv2.0
Security/Password Formats
Verified Safe

SHA-1 Hash (Deprecated — Detection Only) Regex for Java

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

What this pattern does

This page provides a lightweight, single-purpose regular expression for matching sha-1 hash (deprecated — detection only), 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-1 Hash (Deprecated — Detection Only)
// ReDoS-safe | RegexVault — Security > Password Formats

import java.util.regex.Pattern;

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

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

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

Test Cases

Matches (Valid)
Rejects (Invalid)
da39a3ee5e6b4b0d3255bfef95601890afd80709da39a3ee5e6b4b0d3255bfef95601890afd8070
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434dda39a3ee5e6b4b0d3255bfef95601890afd807090
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

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

Git historically used SHA-1 for commit hashing. GitHub and major git hosts have enabled SHA-256 object hashing for new repositories. SHA-1 used in TLS certificates was deprecated in 2017.

Technical Notes

SHA-1 is formally deprecated by NIST. Collision attacks were demonstrated in 2017 (SHAttered attack). Never use for digital signatures, certificates, or password hashing. Use SHA-256 or SHA-3 minimum for all new applications.

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