SHA-1 Hash (Deprecated — Detection Only) Regex for Python
/^[a-f0-9]{40}$/iWhat this pattern does
This page provides a lightweight, single-purpose regular expression for matching sha-1 hash (deprecated — detection only), ported and verified for Python. 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 Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.
Python Implementation
# SHA-1 Hash (Deprecated — Detection Only)
# ReDoS-safe | RegexVault — Security > Password Formats
import re
sha1_hash_deprecated_detection_only_pattern = re.compile(r'^[a-f0-9]{40}$')
def validate_sha1_hash_deprecated_detection_only(value: str) -> bool:
return bool(sha1_hash_deprecated_detection_only_pattern.fullmatch(value))
# Example
print(validate_sha1_hash_deprecated_detection_only("da39a3ee5e6b4b0d3255bfef95601890afd80709")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
da39a3ee5e6b4b0d3255bfef95601890afd80709 | da39a3ee5e6b4b0d3255bfef95601890afd8070 |
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d | da39a3ee5e6b4b0d3255bfef95601890afd807090 |
| — | 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 Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. 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