Skip to Content
oxcrypto

Hashes

Hash a byte string, stream a file, or find the digest named by a protocol.

Hash bytes

Select a variant, then call digest(data). Use .hex() when you need to display or store the digest as text.

from oxcrypto.hash.sha2 import SHA2

print(SHA2.SHA256.digest(b"hello").hex())

See SHA-2 for the available SHA-2 variants.

Hash a file in chunks

Pass an iterable of bytes to digest_chunks(chunks). It hashes the joined byte sequence while reading the file one chunk at a time.

from pathlib import Path
from oxcrypto.hash.blake3 import BLAKE3

def file_digest(path: Path) -> bytes:
    with path.open("rb") as stream:
        chunks = iter(lambda: stream.read(64 * 1024), b"")
        return BLAKE3.BLAKE3.digest_chunks(chunks)

Hash incrementally

Call new() on a variant to get a Hasher. update(data) feeds bytes, digest() returns the digest so far without consuming the state, copy() forks the state, and reset() returns to the state new() created, keeping its parameters. name, digest_size, block_size and oid describe the algorithm; oid is None when the upstream crate defines no OID.

from oxcrypto.hash.sha2 import SHA2

hasher = SHA2.SHA256.new()
hasher.update(b"he")
prefix = hasher.copy()
hasher.update(b"llo")
assert hasher.digest() == SHA2.SHA256.digest(b"hello")
assert prefix.digest() == SHA2.SHA256.digest(b"he")
print(hasher.name, hasher.digest_size, hasher.oid)  # SHA256 32 2.16.840.1.101.3.4.2.1

Keyed and parameterized hashing

BLAKE2 and BLAKE3 accept keyword-only parameters on digest, digest_chunks and new: an output digest_size, a key for keyed hashing (a MAC), and BLAKE2 salt/person or a BLAKE3 derive_key_context. Grøstl and Kupyna accept digest_size on their variable-length variant.

from oxcrypto.hash.blake3 import BLAKE3

tag = BLAKE3.BLAKE3.digest(b"message", key=bytes(32))
subkey = BLAKE3.BLAKE3.digest(b"input key material", derive_key_context="example.com 2026 session keys")

Secret handling

Keys passed to digest, digest_chunks or new are Python bytes objects owned by the interpreter. oxcrypto cannot erase them, the digest it returns (a MAC tag or a BLAKE3 derived key), or copies Python makes elsewhere. On the Rust side, oxcrypto enables the zeroize support of the blake2, digest and blake3 crates, which are written to clear the key block, chaining values, buffered input and counters when a keyed Hasher, each copy() of it, or the temporary state used to produce a digest is dropped. oxcrypto has not verified this independently and there is no published audit. Stack temporaries and CPU registers inside the upstream compression functions are not cleared, and other copies of a key can remain in process memory.

Match an existing digest

Choose the exact algorithm and variant specified by the file format or protocol. For example, Ethereum specifies Keccak-256. A SHA-3 protocol can specify SHA3-256. Each reference page shows the import, available variants, and runnable code.

Hash families

SHA-2 · SHA-3 · BLAKE2 · BLAKE3 · Ascon-Hash256 · Keccak

Format and protocol compatibility

Bao (experimental) · Bash · BelT · FSB · GOST94 · Grøstl · JH · Kupyna · RIPEMD · Shabal · Skein · SM3 · Streebog · Tiger · Whirlpool

Legacy compatibility

MD2 · MD4 · MD5 · SHA-1 · Collision-checked SHA-1

For a caller-selected output length, see extendable-output functions. For passwords, see password hashing.

On this page