Streaming-capable SipHash-1-3 and SipHash-2-4 Implementation

1.8 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

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.

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()}")

view raw JSON →