SHA-256 Hash Regex for Python
/^[a-f0-9]{64}$/iWhat this pattern does
This page provides a lightweight, single-purpose regular expression for matching sha-256 hash, 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-256 Hash
# ReDoS-safe | RegexVault — Security > Password Formats
import re
sha256_hash_pattern = re.compile(r'^[a-f0-9]{64}$')
def validate_sha256_hash(value: str) -> bool:
return bool(sha256_hash_pattern.fullmatch(value))
# Example
print(validate_sha256_hash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 | e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85 |
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 | e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855X |
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
SHA-256 of a password without salt is equivalent to MD5 for dictionary attacks — it is fast to compute and has no iteration cost. Always use a proper KDF (Key Derivation Function) for password storage.
Technical Notes
SHA-256 is part of the SHA-2 family, still cryptographically secure. Used for certificates, code signing, TLS, and file integrity. For password hashing, always use SHA-256 within a proper password hashing function (PBKDF2, Argon2) — raw SHA-256 alone is not suitable for passwords.
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