ap-pure
Pure-stdlib Python implementation of Arash Partow's AP Hash Function (§08 of the General Hash Function Library, originally released 2002 under MIT).
The implementation is a bit-exact translation of the original C reference: identical inputs produce identical 32-bit unsigned hashes on every platform, with no I/O, no clock, and no random involvement.
Installation
pip install -e .
Requires Python ≥ 3.8. No runtime dependencies (pure stdlib). The only
dev dependency is pytest.
Usage
from ap_pure import ap_hash
ap_hash(b"abc") # 633864072 (0x25C7FF88)
ap_hash(b"") # 2863311530 (0xAAAAAAAA) — initial value
ap_hash(b"The quick brown fox…") # 2709835459 (0xA18CAEC3)
API:
def ap_hash(data: bytes) -> int: ...
- Input:
bytes. RaisesTypeErrorforstr,int,None,bytearray,memoryview, etc. —bytesonly. - Output: 32-bit unsigned integer in
[0, 0xFFFFFFFF]. - Side effects: none. Pure function.
To pass a bytearray explicitly, convert it first:
ap_hash(bytes(b)).
Tests
pip install pytest
pytest -q
Currently 262 tests, all passing in <0.5s. Coverage spans the 13 canonical vectors from the spec, plus determinism, avalanche, type safety, edge cases, and a LOC-guard.
Algorithm
Bit-exact Python translation of:
unsigned int APHash(const char* str, unsigned int length)
{
unsigned int hash = 0xAAAAAAAA;
for (unsigned int i = 0; i < length; ++str, ++i)
hash ^= ((i & 1) == 0)
? ( (hash << 7) ^ (*str) * (hash >> 3))
: (~((hash << 11) + ((*str) ^ (hash >> 5))));
return hash;
}
See docs/CANONICAL_REFERENCE.md for the original C source and the
full MIT notice.
License
- This implementation: CC0-1.0 (public domain).
- Original algorithm: MIT, © Arash Partow — preserved verbatim in
docs/CANONICAL_REFERENCE.mdper the MIT terms.
See LICENSE.
Limitations / non-goals
- Non-cryptographic. AP Hash is a general-purpose hash function intended for hash tables, Bloom-filter seeding, and similar applications. It is not suitable for security, integrity, or adversarial-input settings.
- Single function, single width. One function, one 32-bit output. No streaming variant, no class hierarchy, no CLI.
- No comparative benchmarks vs.
mmh3,fnv, etc. — AP Hash is provided for users who want this specific algorithm. - Determinism is contractual. The output of
ap_hash(x)will match the C reference output ofAPHash(x, len(x))for any byte stringx, on any platform, forever.
Release files for ap-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)
| File | Size | Uploaded | |
|---|---|---|---|
| ap_pure-0.1.0.tar.gz | 9.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ap_pure-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.3 kB
Release files / ap_pure-0.1.0.tar.gz
| Download URL | ap_pure-0.1.0.tar.gz |
|---|---|
| Size | 9.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6f9d7af7731d64fe9dcec56c335016aa514f521238787495941b09ea1816f692
|
|
BLAKE2b-256 checksum How to use checksums |
982f3459197945e2ad922e0d30a86675638bddfed1e755070d16da08d097d755
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / ap_pure-0.1.0-py3-none-any.whl
| Download URL | ap_pure-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3137daaa29f00c25309362657c10ac23c7693b0790fcd1d24ad7d642eab73922
|
|
BLAKE2b-256 checksum How to use checksums |
c4488cae598e742adf1a6b2bafc2241d6fdbf2bf53cda0e5e9d423e731e4183d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|