Python SHA3-256 Hash Code Example (Online Runner)
Python SHA3-256 hashing code example with runnable snippets to calculate SHA3-256 and verify results online.
Online calculator: use the site SHA-3 text tool.
Calculation method
Use hashlib.sha3_256, encode text to bytes, and call hexdigest() for a 64-character hex digest.
Implementation notes
- Package: built-in
hashlib. - Implementation: SHA3-256 is the standardized SHA-3 variant (not the older Keccak variants) and always outputs 32 bytes.
- Notes: SHA-3 and SHA-2 produce different digests for the same input; pick one and stay consistent across systems.
python
import hashlib
def sha3_256_text(text: str, encoding: str = "utf-8") -> str:
return hashlib.sha3_256(text.encode(encoding)).hexdigest()
# Example usage
from hashlib import sha3_256
payload = "hello world"
digest = sha3_256(payload.encode("utf-8")).hexdigest()
print(digest)
File hashing example
python
from pathlib import Path
import hashlib
import tempfile
def sha3_256_file(path: Path, chunk_size: int = 1024 * 1024) -> str:
hasher = hashlib.sha3_256()
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(sha3_256_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.
SHA3-256 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 SHA3-256
SHA3-256 is part of the Keccak family and offers strong security properties. Use it when you want SHA-3 compatibility or need diversity beyond SHA-2.
Test vectors
| Input | Expected SHA3-256 |
|---|---|
| (empty string) | a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a |
abc | 3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532 |
message digest | edcdb2069366e75243860c18c3a11465eca34bce6143d30c8665cefcfd32bffd |
abcdefghijklmnopqrstuvwxyz | 7cab2dc765e21b241dbc1c255ce620b29f527c6d5e7f5f843e56288f0d707521 |
Complete script (implementation + tests)
python
import hashlib
TEST_VECTORS = {
"": "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
"abc": "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532",
"message digest": "edcdb2069366e75243860c18c3a11465eca34bce6143d30c8665cefcfd32bffd",
"abcdefghijklmnopqrstuvwxyz": "7cab2dc765e21b241dbc1c255ce620b29f527c6d5e7f5f843e56288f0d707521",
}
def sha3_256_hex(data: bytes) -> str:
return hashlib.sha3_256(data).hexdigest()
def sha3_256_text(text: str, encoding: str = "utf-8") -> str:
return sha3_256_hex(text.encode(encoding))
def run_tests() -> None:
for text, expected in TEST_VECTORS.items():
actual = sha3_256_text(text)
assert actual == expected, f"SHA3-256 mismatch for {text!r}: {actual} != {expected}"
print("All SHA3-256 test vectors passed.")
if __name__ == "__main__":
run_tests()