Python xxHash Code Example (Online Runner)

Python xxHash examples for XXH32, XXH64, XXH3-64, and XXH3-128 with seeds and hex/decimal output.

Online calculator: use the site xxHash text tool.

Note: This snippet requires locally installed dependencies and will not run in the online runner.

Calculation method

The xxHash tools expose four variants plus seed controls. XXH32 uses a 32-bit seed, and XXH64/XXH3 use a 64-bit seed provided as low/high 32-bit words. Output can be hex or decimal.

Python requires the xxhash package: pip install xxhash.

Implementation notes

  • Package: xxhash provides XXH32, XXH64, XXH3-64, and XXH3-128.
  • Implementation: seeds are combined from low/high 32-bit words to match the UI and results are formatted to the correct bit width.
  • Notes: xxHash is not cryptographic. Use it for fast checksums or hashing, not for security.
python
from pathlib import Path
from typing import Literal
import xxhash

Variant = Literal["xxhash32", "xxhash64", "xxh3_64", "xxh3_128"]
OutputFormat = Literal["hex", "decimal"]

UINT32_MASK = 0xFFFFFFFF
UINT64_MASK = 0xFFFFFFFFFFFFFFFF
UINT128_MASK = (1 << 128) - 1


def parse_seed(value: str) -> int:
    if not value.strip():
        return 0
    cleaned = value.strip()
    base = 16 if cleaned.startswith(("0x", "0X")) else 10
    return int(cleaned, base) & UINT32_MASK


def combine_seed(seed_low: int, seed_high: int) -> int:
    return ((seed_high & UINT32_MASK) << 32) | (seed_low & UINT32_MASK)


def format_output(value: int, bits: int, output_format: OutputFormat) -> str:
    if output_format == "decimal":
        return str(value)
    width = bits // 4
    return f"{value:0{width}x}"


def xxhash_text(
    text: str,
    variant: Variant = "xxhash32",
    seed32: int = 0,
    seed_low: int = 0,
    seed_high: int = 0,
    output_format: OutputFormat = "hex",
) -> str:
    data = text.encode("utf-8")
    if variant == "xxhash32":
        value = xxhash.xxh32(data, seed=seed32 & UINT32_MASK).intdigest() & UINT32_MASK
        return format_output(value, 32, output_format)
    if variant == "xxhash64":
        seed = combine_seed(seed_low, seed_high)
        value = xxhash.xxh64(data, seed=seed).intdigest() & UINT64_MASK
        return format_output(value, 64, output_format)
    if variant == "xxh3_64":
        seed = combine_seed(seed_low, seed_high)
        value = xxhash.xxh3_64(data, seed=seed).intdigest() & UINT64_MASK
        return format_output(value, 64, output_format)
    seed = combine_seed(seed_low, seed_high)
    value = xxhash.xxh3_128(data, seed=seed).intdigest() & UINT128_MASK
    return format_output(value, 128, output_format)


def xxhash_file(
    path: Path,
    variant: Variant = "xxhash32",
    seed32: int = 0,
    seed_low: int = 0,
    seed_high: int = 0,
    output_format: OutputFormat = "hex",
) -> str:
    data = path.read_bytes()
    if variant == "xxhash32":
        value = xxhash.xxh32(data, seed=seed32 & UINT32_MASK).intdigest() & UINT32_MASK
        return format_output(value, 32, output_format)
    if variant == "xxhash64":
        seed = combine_seed(seed_low, seed_high)
        value = xxhash.xxh64(data, seed=seed).intdigest() & UINT64_MASK
        return format_output(value, 64, output_format)
    if variant == "xxh3_64":
        seed = combine_seed(seed_low, seed_high)
        value = xxhash.xxh3_64(data, seed=seed).intdigest() & UINT64_MASK
        return format_output(value, 64, output_format)
    seed = combine_seed(seed_low, seed_high)
    value = xxhash.xxh3_128(data, seed=seed).intdigest() & UINT128_MASK
    return format_output(value, 128, output_format)

# Example usage
seed32 = parse_seed("1234")
seed_low = parse_seed("0x1234")
seed_high = parse_seed("0xabcd")

print(xxhash_text("hello", variant="xxhash32", seed32=seed32, output_format="hex"))
print(xxhash_text("hello", variant="xxh3_128", seed_low=seed_low, seed_high=seed_high, output_format="decimal"))

File hashing example

python
from pathlib import Path
import tempfile
from typing import Literal
import xxhash

Variant = Literal["xxhash32", "xxhash64", "xxh3_64", "xxh3_128"]
OutputFormat = Literal["hex", "decimal"]

UINT32_MASK = 0xFFFFFFFF
UINT64_MASK = 0xFFFFFFFFFFFFFFFF
UINT128_MASK = (1 << 128) - 1


def combine_seed(seed_low: int, seed_high: int) -> int:
    return ((seed_high & UINT32_MASK) << 32) | (seed_low & UINT32_MASK)


def format_output(value: int, bits: int, output_format: OutputFormat) -> str:
    if output_format == "decimal":
        return str(value)
    width = bits // 4
    return f"{value:0{width}x}"


def xxhash_file(
    path: Path,
    variant: Variant = "xxhash32",
    seed32: int = 0,
    seed_low: int = 0,
    seed_high: int = 0,
    output_format: OutputFormat = "hex",
) -> str:
    data = path.read_bytes()
    if variant == "xxhash32":
        value = xxhash.xxh32(data, seed=seed32 & UINT32_MASK).intdigest() & UINT32_MASK
        return format_output(value, 32, output_format)
    if variant == "xxhash64":
        seed = combine_seed(seed_low, seed_high)
        value = xxhash.xxh64(data, seed=seed).intdigest() & UINT64_MASK
        return format_output(value, 64, output_format)
    if variant == "xxh3_64":
        seed = combine_seed(seed_low, seed_high)
        value = xxhash.xxh3_64(data, seed=seed).intdigest() & UINT64_MASK
        return format_output(value, 64, output_format)
    seed = combine_seed(seed_low, seed_high)
    value = xxhash.xxh3_128(data, seed=seed).intdigest() & UINT128_MASK
    return format_output(value, 128, output_format)

with tempfile.TemporaryDirectory() as temp_dir:
    sample_path = Path(temp_dir) / "sample.bin"
    sample_path.write_bytes(b"hello")
    print(xxhash_file(sample_path, variant="xxhash64", seed_low=0, seed_high=0))

Complete script (implementation + tests)

python
import xxhash

UINT32_MASK = 0xFFFFFFFF
UINT64_MASK = 0xFFFFFFFFFFFFFFFF


def run_tests() -> None:
    value32 = xxhash.xxh32(b"hello", seed=0).intdigest() & UINT32_MASK
    value64 = xxhash.xxh64(b"hello", seed=0).intdigest() & UINT64_MASK
    assert f"{value32:08x}" == "fb0077f9"
    assert f"{value64:016x}" == "26c7827d889f6da3"
    print("xxHash tests passed")


if __name__ == "__main__":
    run_tests()