hbutils.encoding.int_hash

Non-cryptographic hash algorithms module.

This module provides implementations of various fast, non-cryptographic hash algorithms commonly used for hash tables, checksums, and data fingerprinting. These algorithms prioritize speed over cryptographic security.

Available hash algorithms:

  • FNV-1a (32-bit and 64-bit): Fowler-Noll-Vo hash function

  • DJB2: Daniel J. Bernstein’s hash algorithm

  • SDBM: Hash function from the SDBM database library

  • MurmurHash3 (32-bit): Fast non-cryptographic hash by Austin Appleby

  • CRC32 Variant: Modified CRC32 algorithm

  • xxHash32: Extremely fast hash algorithm

  • xs: A tiny and fast but reliable hash algorithm

The module provides a unified interface through the int_hash function, which allows selecting different hash algorithms via the method parameter. Custom hash functions can be registered using the register_int_hash decorator.

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

register_int_hash

hbutils.encoding.int_hash.register_int_hash(name: str, func: Callable[[str | bytes | bytearray], int] | None = None) Callable | 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 function parameters.

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

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

Returns:

The decorator function if func is None, otherwise None.

Return type:

Optional[Callable]

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")
1335831723
>>> int_hash("hello", method='FNV-1a-32')
1335831723
>>> int_hash(b"world", method='DJB2')
279393645