Python SHA-512 Hash Code Example (Online Runner)

Python SHA-512 hashing code example with runnable snippets to calculate SHA-512 and verify results online.

Online calculator: use the site SHA-512 text tool.

Calculation method

Use hashlib.sha512, encode text to bytes, and call hexdigest() for a 128-character hex digest.

Implementation notes

  • Package: built-in hashlib.
  • Implementation: SHA-512 processes bytes; text encoding and newline differences affect output. File hashing uses chunked reads for large files.
  • Notes: SHA-512 is unkeyed; use HMAC for integrity with a shared key or a password KDF for password storage.
python
import hashlib


def sha512_text(text: str, encoding: str = "utf-8") -> str:
    return hashlib.sha512(text.encode(encoding)).hexdigest()

# Example usage
from hashlib import sha512

payload = "hello world"
digest = sha512(payload.encode("utf-8")).hexdigest()
print(digest)

File hashing example

python
from pathlib import Path
import hashlib
import tempfile


def sha512_file(path: Path, chunk_size: int = 1024 * 1024) -> str:
    hasher = hashlib.sha512()
    with path.open("rb") as handle:
        for chunk in iter(lambda: handle.read(chunk_size), b""):
            hasher.update(chunk)
    return hasher.hexdigest()


if __name__ == "__main__":
    with tempfile.TemporaryDirectory() as temp_dir:
        sample_path = Path(temp_dir) / "example.bin"
        sample_path.write_bytes(b"example payload\n")
        print(sha512_file(sample_path))

If you create files with echo, note it appends a newline by default. Use echo -n or include a \n byte to match this digest.

SHA-512 file hashes are computed from the file bytes only. The filename or path is not included unless you explicitly hash it as part of the input.

When to use SHA-512

SHA-512 provides a longer digest for high-integrity checks and signatures. It is widely supported and secure for general-purpose cryptographic hashing.

Test vectors

InputExpected SHA-512
(empty string)cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
abcddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f
message digest107dbf389d9e9f71a3a95f6c055b9251bc5268c2be16d6c13492ea45b0199f3309e16455ab1e96118e8a905d5597b72038ddb372a89826046de66687bb420e7c
abcdefghijklmnopqrstuvwxyz4dbff86cc2ca1bae1e16468a05cb9881c97f1753bce3619034898faa1aabe429955a1bf8ec483d7421fe3c1646613a59ed5441fb0f321389f77f48a879c7b1f1

Complete script (implementation + tests)

python
import hashlib

TEST_VECTORS = {
    "": "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
    "abc": "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
    "message digest": "107dbf389d9e9f71a3a95f6c055b9251bc5268c2be16d6c13492ea45b0199f3309e16455ab1e96118e8a905d5597b72038ddb372a89826046de66687bb420e7c",
    "abcdefghijklmnopqrstuvwxyz": "4dbff86cc2ca1bae1e16468a05cb9881c97f1753bce3619034898faa1aabe429955a1bf8ec483d7421fe3c1646613a59ed5441fb0f321389f77f48a879c7b1f1",
}


def sha512_hex(data: bytes) -> str:
    return hashlib.sha512(data).hexdigest()


def sha512_text(text: str, encoding: str = "utf-8") -> str:
    return sha512_hex(text.encode(encoding))


def run_tests() -> None:
    for text, expected in TEST_VECTORS.items():
        actual = sha512_text(text)
        assert actual == expected, f"SHA-512 mismatch for {text!r}: {actual} != {expected}"
    print("All SHA-512 test vectors passed.")


if __name__ == "__main__":
    run_tests()