Python FarmHash Fingerprint64 Code Example (Online Runner)

Python FarmHash fingerprint64 examples with unsigned decimal output and file hashing.

Online calculator: use the site FarmHash text tool.

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

Calculation method

FarmHash fingerprint64 returns a signed 64-bit integer in most bindings. The online tool displays the unsigned decimal value, so we mask to 64 bits.

Python requires the farmhash package: pip install farmhash.

Implementation notes

  • Package: farmhash wraps Google FarmHash.
  • Implementation: fingerprint64 is used and masked to 64 bits to match the unsigned decimal UI output.
  • Notes: FarmHash is non-cryptographic. It is optimized for hash tables and fingerprinting, not for security.
python
from pathlib import Path
import farmhash

UINT64_MASK = (1 << 64) - 1


def farmhash64_text(text: str, encoding: str = "utf-8") -> str:
    value = farmhash.fingerprint64(text.encode(encoding)) & UINT64_MASK
    return str(value)


def farmhash64_file(path: Path) -> str:
    value = farmhash.fingerprint64(path.read_bytes()) & UINT64_MASK
    return str(value)

# Example usage
print(farmhash64_text("hello"))

File hashing example

python
from pathlib import Path
import tempfile
import farmhash

UINT64_MASK = (1 << 64) - 1


def farmhash64_file(path: Path) -> str:
    value = farmhash.fingerprint64(path.read_bytes()) & UINT64_MASK
    return str(value)

with tempfile.TemporaryDirectory() as temp_dir:
    sample_path = Path(temp_dir) / "sample.bin"
    sample_path.write_bytes(b"hello")
    print(farmhash64_file(sample_path))

Complete script (implementation + tests)

python
import farmhash

UINT64_MASK = (1 << 64) - 1


def farmhash64_text(text: str) -> str:
    return str(farmhash.fingerprint64(text.encode("utf-8")) & UINT64_MASK)


def run_tests() -> None:
    raw_empty = farmhash.fingerprint64(b"") & UINT64_MASK
    raw_hello = farmhash.fingerprint64(b"hello") & UINT64_MASK
    assert farmhash64_text("") == str(raw_empty)
    assert farmhash64_text("hello") == str(raw_hello)
    print("FarmHash tests passed")


if __name__ == "__main__":
    run_tests()