Skip to main content

Pure python implementation xxhash algorithms

Project description

ppxxh is a Pure Python implementation of the XXHASH family of hash algorithms.

This pure python implementation of Yann Collet’s XXHASH family of non-cryptographic hash algorithms with no dependencies outside of the python standard library is significantly slower than version 0.8.0 of the reference implementation of these hash algorithms, but they produce the same outputs given the same inputs. This equality of outputs is checked with tests that duplicate portions of xsum_sanity_check.c from the reference implementation.

ppxxh is compatible with Python 3.0 and newer.

The interface and use of ppxxh is similar to the Python standard library module hashlib, while providing XXHASH hash algorithms. Thus the various hash objects may be created using the ppxxh.new() function or directly instantiated from their classes.

Hash Methods

The hash objects have these common methods:

update(data)

Update the state of the hash object. Data may be passed to the hash object at initialization and/or by an arbitrary number of calls to update().

digest()

Return the hash digest as a bytes object.

hexdigest()

Return the hash digest as a string of hexidecimal digits.

intdigest()

Return the hash digest as an unsigned integer.

This method is not part of the hashlib interface but is the typical output format of the reference implementation (except for xxh3_128)

copy()

Return a copy (clone) of the hash object.

The xxh3_128 hash objects also have a intdigest2() method that returns the hash digest as a pair of 64-bit unsigned integers matching the typical output format of the reference implementation for this hash algorithm

As listed in ppxxh.algorithms_guaranteed and ppxxh.algorithms_guaranteed, the hash algorithms provided are xxh32, xxh64, xxh3_64, and xxh3_128.

The ppxxh.generate_secret() function duplicates the XXH3_generateSecret() function provided by the reference implementation to use a low entropy bytes like object of arbitrary length to generate a high entropy secret well suited to use as the secret parameter of xxh3_64 and xxh3_128.

Example

Calculate the digest in various forms for the bytes object, b'This is a bytes object, not a string!'. Specify a seed value. If this optional value were not provided, the hash object would be initialized differently and the various resulting digest values would all be different.

>>> from ppxxh import xxh64
>>> m = xxh64(seed=14414669413082423462)  # seed is optional
>>> m.name
'xxh64'
>>> m.digest_size
8
>>> m.block_size
32
>>> m.update(b'This is a bytes')
>>> m.hexdigest()  # a digest may be requested at any time
'938907256708f46c'
>>> m.update(b' object, not a string!')
>>> m.digest()
b'!^>f7\x84Ii'
>>> m.hexdigest()
'215e3e6637844969'
>>> m.intdigest()
2404427859801426281

A more condensed way to get the same result.

>>> xxh64(b'This is a bytes object, not a string!',
...       seed=14414669413082423462).hexdigest()
'215e3e6637844969'

The reference implementation of the XXHASH family are available at https://github.com/Cyan4973/xxHash

(Python code formatted by black -l 79)

Licensed under MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ppxxh-1.0.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ppxxh-1.0.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file ppxxh-1.0.0.tar.gz.

File metadata

  • Download URL: ppxxh-1.0.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for ppxxh-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1c5b82b0ae1356f8fa32e3f1d45fd059eef6ecf491de2061f169d9998ce59f3e
MD5 16f543f23d6d9de17f545a13c8c6d200
BLAKE2b-256 5c33e3bc3a3734ac304ffa055e11c3c73f7ce9464d97af66d842ff4ba8faac50

See more details on using hashes here.

File details

Details for the file ppxxh-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ppxxh-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for ppxxh-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e43a13456fadf5d80f7ff70c17a2087a846f924d4ee7be41478191a27252d3e4
MD5 37d002d00ef6ba7335aa0cfa68f23add
BLAKE2b-256 124af6996785e0bd70b8d47405170ee68c7294be34d9a81614bf353a3dd1bd8f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page