Python BKDR Hash Code Example (Online Runner)
Python BKDR hash examples with seed parsing, number/hex output, and file hashing to match the BKDR tool.
Online calculator: use the site BKDR text tool.
Calculation method
BKDRHash is a fast rolling hash that multiplies the running value by a seed and adds each byte. The online tool lets you set the seed (decimal or hex) and choose number or hex output.
Implementation notes
- Package: no external dependencies; this is a pure Python implementation.
- Implementation: the hash is computed over UTF-8 bytes (or file bytes) and masked to 32 bits to match the tool output.
- Notes: BKDR is not cryptographic. Different seeds produce different hashes, so keep the seed consistent across systems.
python
from pathlib import Path
from typing import Literal
OutputFormat = Literal["number", "hex"]
DEFAULT_SEED = 131
UINT32_MASK = 0xFFFFFFFF
def parse_seed(value: str, fallback: int = DEFAULT_SEED) -> int:
if not value.strip():
return fallback
cleaned = value.strip()
base = 16 if cleaned.startswith(("0x", "0X")) else 10
return int(cleaned, base) & UINT32_MASK
def bkdr_hash_bytes(data: bytes, seed: int = DEFAULT_SEED) -> int:
if seed <= 0:
raise ValueError("Seed must be positive")
value = 0
for byte in data:
value = (value * seed + byte) & UINT32_MASK
return value
def format_bkdr(value: int, output_format: OutputFormat = "number") -> str:
if output_format == "hex":
return f"{value:08x}"
return str(value)
def bkdr_text(text: str, seed: int = DEFAULT_SEED, encoding: str = "utf-8", output_format: OutputFormat = "number") -> str:
value = bkdr_hash_bytes(text.encode(encoding), seed)
return format_bkdr(value, output_format)
def bkdr_file(path: Path, seed: int = DEFAULT_SEED, output_format: OutputFormat = "number") -> str:
data = path.read_bytes()
value = bkdr_hash_bytes(data, seed)
return format_bkdr(value, output_format)
# Example usage
message = "hello"
seed = parse_seed("0x83") # 131 in hex
print(bkdr_text(message, seed=seed, output_format="number"))
print(bkdr_text(message, seed=seed, output_format="hex"))
File hashing example
python
from pathlib import Path
import tempfile
DEFAULT_SEED = 131
UINT32_MASK = 0xFFFFFFFF
def bkdr_hash_bytes(data: bytes, seed: int = DEFAULT_SEED, initial: int = 0) -> int:
value = initial & UINT32_MASK
for byte in data:
value = (value * seed + byte) & UINT32_MASK
return value
def bkdr_file(path: Path, seed: int = DEFAULT_SEED, output_format: str = "hex") -> str:
value = bkdr_hash_bytes(path.read_bytes(), seed)
return f"{value:08x}" if output_format == "hex" else str(value)
with tempfile.TemporaryDirectory() as temp_dir:
sample_path = Path(temp_dir) / "sample.txt"
sample_path.write_text("hello", encoding="utf-8")
print(bkdr_file(sample_path, seed=131, output_format="hex"))Complete script (implementation + tests)
python
from pathlib import Path
import tempfile
DEFAULT_SEED = 131
UINT32_MASK = 0xFFFFFFFF
def bkdr_hash_bytes(data: bytes, seed: int = DEFAULT_SEED) -> int:
value = 0
for byte in data:
value = (value * seed + byte) & UINT32_MASK
return value
def bkdr_text(text: str, seed: int = DEFAULT_SEED) -> str:
return f"{bkdr_hash_bytes(text.encode('utf-8'), seed):08x}"
def run_tests() -> None:
assert bkdr_text("") == "00000000"
assert bkdr_text("abc") == "001998f2"
with tempfile.TemporaryDirectory() as temp_dir:
sample_path = Path(temp_dir) / "sample.txt"
sample_path.write_bytes(b"abc")
assert bkdr_text("abc") == f"{bkdr_hash_bytes(sample_path.read_bytes()):08x}"
print("BKDR tests passed")
if __name__ == "__main__":
run_tests()