Python MurmurHash3 Code Example (Online Runner)

Python MurmurHash3 examples with 32-bit/128-bit variants, seeds, and number/hex output to match the tools.

Online calculator: use the site MurmurHash3 text tool.

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

Calculation method

The MurmurHash3 tool lets you choose 32-bit (x86) or 128-bit (x86/x64) output and switch output format between number and hex. The helper below mirrors those parameters.

Python requires the mmh3 package: pip install mmh3.

Implementation notes

  • Package: mmh3 provides MurmurHash3 32-bit and 128-bit variants.
  • Implementation: the snippet forces unsigned output (signed=False) and masks to match the tool’s decimal/hex output.
  • Notes: MurmurHash3 is not cryptographic. Seed changes the output; keep it consistent across services.
python
from pathlib import Path
from typing import Literal
import mmh3
import tempfile

BitLength = Literal[32, 128]
Architecture = Literal["x86", "x64"]
OutputFormat = Literal["number", "hex"]

UINT32_MASK = 0xFFFFFFFF
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 format_murmur(value: int, bits: int, output_format: OutputFormat) -> str:
    if output_format == "number":
        return str(value)
    width = bits // 4
    return f"{value:0{width}x}"


def murmur3_text(
    text: str,
    bit_length: BitLength = 32,
    architecture: Architecture = "x86",
    seed: int = 0,
    output_format: OutputFormat = "number",
) -> str:
    data = text.encode("utf-8")
    if bit_length == 32:
        value = mmh3.hash(data, seed=seed, signed=False) & UINT32_MASK
        return format_murmur(value, 32, output_format)
    value = mmh3.hash128(data, seed=seed, signed=False, x64arch=(architecture == "x64")) & UINT128_MASK
    return format_murmur(value, 128, output_format)


def murmur3_file(path: Path, bit_length: BitLength = 32, architecture: Architecture = "x86", seed: int = 0, output_format: OutputFormat = "number") -> str:
    text = path.read_bytes().decode("utf-8", errors="replace")
    return murmur3_text(text, bit_length, architecture, seed, output_format)

# Example usage
seed = parse_seed("0xdeadbeef")

print(murmur3_text("hello", bit_length=32, seed=seed, output_format="number"))
print(murmur3_text("hello", bit_length=128, architecture="x64", seed=seed, output_format="hex"))

with tempfile.TemporaryDirectory() as temp_dir:
    sample_path = Path(temp_dir) / "sample.txt"
    sample_path.write_text("hello", encoding="utf-8")
    print(murmur3_file(sample_path, bit_length=32, seed=seed, output_format="hex"))

Complete script (implementation + tests)

python
import mmh3


def run_tests() -> None:
    assert mmh3.hash(b"hello", seed=0, signed=False) == 613153351
    assert mmh3.hash128(b"hello", seed=0, signed=False, x64arch=True) == 148608770782657113559975676744815460580
    print("MurmurHash3 tests passed")


if __name__ == "__main__":
    run_tests()