hbutils.encoding.int_hash

Non-cryptographic integer hash utilities.

This module provides a collection of fast, non-cryptographic hash functions commonly used for hash tables, checksums, and lightweight data fingerprinting. The algorithms prioritize speed and distribution quality over cryptographic security, and they are exposed through a unified interface.

The module contains the following main components:

Supported algorithm names include:

  • FNV-1a-32 - 32-bit Fowler-Noll-Vo hash

  • FNV-1a-64 - 64-bit Fowler-Noll-Vo hash

  • DJB2 - Daniel J. Bernstein’s hash algorithm

  • SDBM - Hash function from the SDBM database library

  • MurmurHash3-32 - 32-bit MurmurHash3 (simplified)

  • CRC32-Variant - Modified CRC32-based hash

  • xxHash32-Simple - 32-bit xxHash (simplified)

  • xs - Minimal polynomial hash with basic mixing

Note

These algorithms are not cryptographically secure and should not be used for security-sensitive applications.

Example:

>>> from hbutils.encoding import int_hash
>>> int_hash("hello", method='FNV-1a-32')
>>> int_hash(b"world", method='DJB2')

__all__

hbutils.encoding.int_hash.__all__ = ['register_int_hash', 'int_hash']

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

register_int_hash

hbutils.encoding.int_hash.register_int_hash(name: str, func: Callable[[str | bytes | bytearray], int] | None = None) Callable[[Callable[[str | bytes | bytearray], int]], Callable[[str | bytes | bytearray], int]] | None[source]

Register an integer hash function decorator.

This function can be used as a decorator to register hash functions, or called directly with both name and func parameters.

Parameters:
  • name (str) – The name to register the hash function under.

  • func (Optional[_IntHashTyping]) – The hash function to register (optional).

Returns:

A decorator that registers the function if func is None, otherwise None.

Return type:

Optional[Callable[[_IntHashTyping], _IntHashTyping]]

Example:

>>> @register_int_hash('my_hash')
... def my_hash_func(data):
...     return hash(data)
>>> register_int_hash('another_hash', lambda x: hash(x))

int_hash

hbutils.encoding.int_hash.int_hash(data: str | bytes | bytearray, method: str = 'FNV-1a-32') int[source]

Compute integer hash using the specified method.

This is the main entry point for computing hashes. It dispatches to the appropriate hash function based on the method parameter.

Parameters:
  • data (Union[str, bytes, bytearray]) – Input data to hash.

  • method (str) – Hash algorithm to use (default: 'FNV-1a-32'). Available methods: 'FNV-1a-32', 'FNV-1a-64', 'DJB2', 'SDBM', 'MurmurHash3-32', 'CRC32-Variant', 'xxHash32-Simple', 'xs'.

Returns:

Hash value computed by the specified method.

Return type:

int

Raises:

KeyError – If the specified method is not registered.

Example:

>>> int_hash("hello")
>>> int_hash("hello", method='FNV-1a-32')
>>> int_hash(b"world", method='DJB2')