Python BLAKE3 Hash Code Example (Online Runner)
Python BLAKE3 hashing examples with digest length control, keyed hashing, and context strings matching the online tool.
Online calculator: use the site BLAKE3 text tool.
Note: This snippet requires locally installed dependencies and will not run in the online runner.
Calculation method
BLAKE3 supports variable digest lengths, keyed hashing, and context-based derivation. The online tool exposes
digest length, optional key, and optional context. Only one of key or context can be set at a time.
Python requires the blake3 package: pip install blake3.
Implementation notes
- Package:
blake3implements the official BLAKE3 algorithm with keyed and context modes. - Implementation: BLAKE3 is an XOF, so
digest_lengthcontrols output size in bytes. Keys must be exactly 32 bytes. - Notes: keyed mode is a MAC; context mode is for domain separation (
derive_key_context) and cannot be combined with a key.
python
from typing import Optional
from blake3 import blake3
DEFAULT_DIGEST_LENGTH = 32
MAX_DIGEST_LENGTH = 64
KEY_LENGTH = 32
def blake3_text(
text: str,
digest_length: Optional[int] = None,
key: Optional[str] = None,
context: Optional[str] = None,
encoding: str = "utf-8",
) -> str:
if key and context:
raise ValueError("Use either key or context, not both")
digest_length = digest_length or DEFAULT_DIGEST_LENGTH
if digest_length < 1 or digest_length > MAX_DIGEST_LENGTH:
raise ValueError("Digest length must be between 1 and 64 bytes")
key_bytes = key.encode(encoding) if key else None
if key_bytes is not None and len(key_bytes) != KEY_LENGTH:
raise ValueError("BLAKE3 keys must be exactly 32 bytes")
hasher = blake3(text.encode(encoding), key=key_bytes, derive_key_context=context)
return hasher.hexdigest(digest_length)
# Example usage
payload = "hello world"
print(blake3_text(payload))
print(blake3_text(payload, digest_length=16))
print(blake3_text(payload, key="k" * 32))
print(blake3_text(payload, context="my-app-v1"))
File hashing example
python
from pathlib import Path
import tempfile
from blake3 import blake3
def blake3_file(path: Path, digest_length: int = 32) -> str:
hasher = blake3()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
hasher.update(chunk)
return hasher.hexdigest(digest_length)
with tempfile.TemporaryDirectory() as temp_dir:
sample_path = Path(temp_dir) / "example.bin"
sample_path.write_bytes(b"example payload\n")
print(blake3_file(sample_path))Test vectors
| Input | Expected BLAKE3 (32-byte) |
|---|---|
| (empty string) | af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262 |
Complete script (implementation + tests)
python
from blake3 import blake3
TEST_VECTOR_EMPTY = "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262"
def blake3_text(text: str, digest_length: int = 32) -> str:
hasher = blake3(text.encode("utf-8"))
return hasher.hexdigest(digest_length)
def run_tests() -> None:
assert blake3_text("") == TEST_VECTOR_EMPTY
print("All BLAKE3 test vectors passed.")
if __name__ == "__main__":
run_tests()