{"id":5481,"library":"siphash24","title":"Streaming-capable SipHash-1-3 and SipHash-2-4 Implementation","description":"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.","status":"active","version":"1.8","language":"en","source_language":"en","source_url":"https://github.com/dnicolodi/python-siphash24/","tags":["hashing","cryptography","siphash","hashlib-compatible","streaming"],"install":[{"cmd":"pip install siphash24","lang":"bash","label":"Latest stable version"}],"dependencies":[{"reason":"Runtime environment","package":"python","optional":false}],"imports":[{"note":"Imports the module itself, which provides functions and hashlib-compatible classes.","symbol":"siphash24","correct":"import siphash24"},{"note":"The siphash13 function is accessed via the top-level siphash24 module, e.g., siphash24.siphash13().","symbol":"siphash13","correct":"import siphash24"}],"quickstart":{"code":"import siphash24\nimport os\n\n# SipHash-2-4 example (recommended variant)\nkey = os.environ.get('SIPHASH_KEY_24', 'a' * 16).encode('utf-8') # 16-byte key\nmessage = b'hello world'\n\nh = siphash24.siphash24(key=key)\nh.update(message)\nhash_digest = h.digest()\nhash_int = h.intdigest()\nhash_hex = h.hexdigest()\n\nprint(f\"SipHash-2-4 digest: {hash_digest.hex()}\")\nprint(f\"SipHash-2-4 int digest: {hash_int}\")\nprint(f\"SipHash-2-4 hex digest: {hash_hex}\")\n\n# SipHash-1-3 example\nkey_13 = os.environ.get('SIPHASH_KEY_13', 'b' * 16).encode('utf-8') # 16-byte key\nm_13 = siphash24.siphash13(key=key_13, data=b'another message')\nprint(f\"SipHash-1-3 digest (one-shot): {m_13.digest().hex()}\")","lang":"python","description":"Demonstrates how to use both SipHash-2-4 (streaming) and SipHash-1-3 (one-shot) variants. It highlights the `hashlib`-compatible interface methods (`update`, `digest`, `hexdigest`) and the library's extended `intdigest` method. Keys are managed via environment variables for security best practice."},"warnings":[{"fix":"Use `hash_obj.digest()` for `bytes` output or `hash_obj.intdigest()` for `int` output.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Update your code to expect a `str` return type from `hexdigest()`. If you need `bytes`, encode the string manually (e.g., `.encode('utf-8')`).","message":"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`.","severity":"breaking","affected_versions":"<1.6"},{"fix":"Use a separate hash object per thread or implement explicit locking mechanisms when sharing hash objects across threads.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always provide a 16-byte key as a `bytes-like` object. For example, `b'your_16_byte_key'`.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}