Skip to main content

djb2-pure

A single-file pure-Python implementation of djb2, the classic non-cryptographic 32-bit hash function designed by Daniel J. Bernstein (1991).

Pure stdlib. Zero dependencies. Single file.

What is djb2?

djb2 is one of the simplest and most widely used string-hashing functions. It appears in Berkeley DB, SDBM-derived libraries, the crc32-substitute discussions in comp.lang.c, and Bernstein's own historical notes. Its algebraic form is:

h = 5381
for c in bytes:
    h = (h * 33 + c) mod 2**32
return h

The constant 33 and seed 5381 are the canonical Bernstein choices — no derivation or "improved" variant. This module implements that exact algorithm verbatim.

Install

pip install -e .

Quick start

from djb2_pure import djb2, djb2_hex

djb2(b"")           # 5381          (the canonical seed)
djb2(b"a")          # 177670
djb2(b"abc")        # 193485963
djb2(b"hello world")  # 894552257
djb2_hex(b"hello world")  # "3551c8c1"

Public API

  • djb2(data: bytes) -> int — returns the 32-bit hash as an unsigned integer
  • djb2_hex(data: bytes) -> str — returns the 8-character lowercase hex string
  • Djb2 — streaming class for incremental hashing of large/chunked inputs

Limitations

  • NOT cryptographic. djb2 has known collisions and bias; do not use for security, integrity, or anywhere adversarial.
  • NOT 64-bit. djb2 returns a 32-bit hash. For a 64-bit variant see djb2a (Bernstein's later proposal: seed 5381 with hash h = h * 33 ^ c, mod 2^64).
  • Streaming class wraps the same algorithm. No novel variant; the class exists for parity with hashlib ergonomics.

License

CC0-1.0 — public domain dedication.

Metadata

Release files for djb2-pure 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djb2-pure 0.1.0
File Size Uploaded
djb2_pure-0.1.0.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for djb2-pure 0.1.0
File Interpreter ABI Platform
djb2_pure-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.6 kB

Release files / djb2_pure-0.1.0.tar.gz

Download URL djb2_pure-0.1.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f3f1ebf0bed3b7bedf1698966b057aff008a73e28034dafbb1dcf19ec1810871
BLAKE2b-256 checksum
How to use checksums
47249d6f61ca401c35d7e6ee501e84319e29c2c379af2bfa9262c701b8f1af9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

Release files / djb2_pure-0.1.0-py3-none-any.whl

Download URL djb2_pure-0.1.0-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
83bc1756bda77f7045fd9df38c4370b5492d88a7e5b751e0f72932356e8703d6
BLAKE2b-256 checksum
How to use checksums
b18cdb7521a43810db478dfe9187a86d3e4e3e98833c3e2975c3547661dddf17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page