REGEXVAULTv2.0
Security/Password Formats
Verified Safe

SHA-512 Hash Regex for Python

/^[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 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

Python
# SHA-512 Hash
# ReDoS-safe | RegexVault — Security > Password Formats

import re

sha512_hash_pattern = re.compile(r'^[a-f0-9]{128}$')

def validate_sha512_hash(value: str) -> bool:
    return bool(sha512_hash_pattern.fullmatch(value))

# Example
print(validate_sha512_hash("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 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-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