Streaming-capable SipHash-1-3 and SipHash-2-4 Implementation
The siphash24 library provides a C-based, streaming-capable implementation of SipHash-1-3 and SipHash-2-4 variants for Python. It offers an interface compatible with Python's standard `hashlib` module, enhancing it with an `intdigest()` method for integer hash values. The current version is 1.8, with releases occurring several times a year to support new Python versions and address minor updates.
Warnings
- gotcha The `digest()` method returns a `bytes` object, while the `intdigest()` method returns a Python `int` object. Ensure you use the correct method based on your expected output type.
- breaking Prior to version 1.6, the `hexdigest()` method returned a `bytes` object. Starting with version 1.6, `hexdigest()` consistently returns a `str` object for consistency with `hashlib`.
- gotcha Instances of `siphash24` hash objects are not thread-safe. If a single hash object is modified concurrently by multiple threads, external synchronization primitives (e.g., `threading.Lock`) must be used to prevent race conditions.
- gotcha The SipHash algorithm requires a 16-byte key. While `siphash24` will zero-pad shorter keys, it is best practice to always provide a 16-byte `bytes-like` object for the key to ensure predictable and secure hashing behavior.
Install
-
pip install siphash24
Imports
- siphash24
import siphash24
- siphash13
import siphash24
Quickstart
import siphash24
import os
# SipHash-2-4 example (recommended variant)
key = os.environ.get('SIPHASH_KEY_24', 'a' * 16).encode('utf-8') # 16-byte key
message = b'hello world'
h = siphash24.siphash24(key=key)
h.update(message)
hash_digest = h.digest()
hash_int = h.intdigest()
hash_hex = h.hexdigest()
print(f"SipHash-2-4 digest: {hash_digest.hex()}")
print(f"SipHash-2-4 int digest: {hash_int}")
print(f"SipHash-2-4 hex digest: {hash_hex}")
# SipHash-1-3 example
key_13 = os.environ.get('SIPHASH_KEY_13', 'b' * 16).encode('utf-8') # 16-byte key
m_13 = siphash24.siphash13(key=key_13, data=b'another message')
print(f"SipHash-1-3 digest (one-shot): {m_13.digest().hex()}")