Skip to main content

Python binding for xxHash

Project description

Travis CI Build Status Appveyor Build Status Latest Version Supported Python versions License

xxhash is a Python binding for the xxHash library by Yann Collet.

Installation

$ pip install xxhash

Installing From Source

$ pip install --no-binary xxhash xxhash

Prerequisites

On Debian/Ubuntu:

$ apt-get install python-dev gcc

On CentOS/Fedora:

$ yum install python-devel gcc redhat-rpm-config

Linking to libxxhash.so

By default python-xxhash will use bundled xxHash, we can change this by specifying ENV var XXHASH_LINK_SO:

$ XXHASH_LINK_SO=1 pip install --no-binary xxhash xxhash

Usage

Module version and its backend xxHash library version can be retrieved using the module properties VERSION AND XXHASH_VERSION respectively.

>>> import xxhash
>>> xxhash.VERSION
'1.4.2'
>>> xxhash.XXHASH_VERSION
'0.7.1'

This module is hashlib-compliant, which means you can use it in the same way as hashlib.md5.

update() – update the current digest with an additional string
digest() – return the current digest value
hexdigest() – return the current digest as a string of hexadecimal digits
intdigest() – return the current digest as an integer
copy() – return a copy of the current xxhash object
reset() – reset state

md5 digest returns bytes, but the original xxh32 and xxh64 C APIs return integers. While this module is made hashlib-compliant, intdigest() is also provided to get the integer digest.

Constructors for hash algorithms provided by this module are xxh32() and xxh64().

For example, to obtain the digest of the byte string b'Nobody inspects the spammish repetition':

>>> import xxhash
>>> x = xxhash.xxh32()
>>> x.update(b'Nobody inspects')
>>> x.update(b' the spammish repetition')
>>> x.digest()
b'\xe2);/'
>>> x.digest_size
4
>>> x.block_size
16

More condensed:

>>> xxhash.xxh32(b'Nobody inspects the spammish repetition').hexdigest()
'e2293b2f'
>>> xxhash.xxh32(b'Nobody inspects the spammish repetition').digest() == x.digest()
True

An optional seed (default is 0) can be used to alter the result predictably:

>>> import xxhash
>>> xxhash.xxh64('xxhash').hexdigest()
'32dd38952c4bc720'
>>> xxhash.xxh64('xxhash', seed=20141025).hexdigest()
'b559b98d844e0635'
>>> x = xxhash.xxh64(seed=20141025)
>>> x.update('xxhash')
>>> x.hexdigest()
'b559b98d844e0635'
>>> x.intdigest()
13067679811253438005

Be careful that xxh32 takes an unsigned 32-bit integer as seed, while xxh64 takes an unsigned 64-bit integer. Although unsigned integer overflow is defined behavior, it’s better not to make it happen:

>>> xxhash.xxh32('I want an unsigned 32-bit seed!', seed=0).hexdigest()
'f7a35af8'
>>> xxhash.xxh32('I want an unsigned 32-bit seed!', seed=2**32).hexdigest()
'f7a35af8'
>>> xxhash.xxh32('I want an unsigned 32-bit seed!', seed=1).hexdigest()
'd8d4b4ba'
>>> xxhash.xxh32('I want an unsigned 32-bit seed!', seed=2**32+1).hexdigest()
'd8d4b4ba'
>>>
>>> xxhash.xxh64('I want an unsigned 64-bit seed!', seed=0).hexdigest()
'd4cb0a70a2b8c7c1'
>>> xxhash.xxh64('I want an unsigned 64-bit seed!', seed=2**64).hexdigest()
'd4cb0a70a2b8c7c1'
>>> xxhash.xxh64('I want an unsigned 64-bit seed!', seed=1).hexdigest()
'ce5087f12470d961'
>>> xxhash.xxh64('I want an unsigned 64-bit seed!', seed=2**64+1).hexdigest()
'ce5087f12470d961'

digest() returns bytes of the big-endian representation of the integer digest:

>>> import xxhash
>>> h = xxhash.xxh64()
>>> h.digest()
b'\xefF\xdb7Q\xd8\xe9\x99'
>>> h.intdigest().to_bytes(8, 'big')
b'\xefF\xdb7Q\xd8\xe9\x99'
>>> h.hexdigest()
'ef46db3751d8e999'
>>> format(h.intdigest(), '016x')
'ef46db3751d8e999'
>>> h.intdigest()
17241709254077376921
>>> int(h.hexdigest(), 16)
17241709254077376921

Besides xxh32/xxh64 mentioned above, oneshot functions are also provided, so we can avoid allocating XXH32/64 state on heap:

xxh32_digest(bytes, seed=0)
xxh32_intdigest(bytes, seed=0)
xxh32_hexdigest(bytes, seed=0)
xxh64_digest(bytes, seed=0)
xxh64_intdigest(bytes, seed=0)
xxh64_hexdigest(bytes, seed=0)
>>> import xxhash
>>> xxhash.xxh64('a').digest() == xxhash.xxh64_digest('a')
True
>>> xxhash.xxh64('a').intdigest() == xxhash.xxh64_intdigest('a')
True
>>> xxhash.xxh64('a').hexdigest() == xxhash.xxh64_hexdigest('a')
True
>>> xxhash.xxh64_hexdigest('xxhash', seed=20141025)
'b559b98d844e0635'
>>> xxhash.xxh64_intdigest('xxhash', seed=20141025)
13067679811253438005L
>>> xxhash.xxh64_digest('xxhash', seed=20141025)
'\xb5Y\xb9\x8d\x84N\x065'
In [1]: import xxhash

In [2]: %timeit xxhash.xxh64_hexdigest('xxhash')
268 ns ± 24.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [3]: %timeit xxhash.xxh64('xxhash').hexdigest()
416 ns ± 17.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Caveats

SEED OVERFLOW

xxh32 takes an unsigned 32-bit integer as seed, and xxh64 takes an unsigned 64-bit integer as seed. Make sure that the seed is greater than or equal to 0.

ENDIANNESS

As of python-xxhash 0.3.0, digest() returns bytes of the big-endian representation of the integer digest. It used to be little-endian.

DONT USE XXHASH IN HMAC

Though you can use xxhash as an HMAC hash function, but it’s highly recommended not to.

xxhash is NOT a cryptographic hash function, it is a non-cryptographic hash algorithm aimed at speed and quality. Do not put xxhash in any position where cryptographic hash functions are required.

Project details


Download files

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

Files for xxhash, version 1.4.2
Filename, size File type Python version Upload date Hashes
Filename, size xxhash-1.4.2-cp27-cp27m-macosx_10_6_intel.whl (67.6 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp27-cp27m-manylinux1_i686.whl (95.5 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp27-cp27m-manylinux1_x86_64.whl (118.5 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp27-cp27mu-manylinux1_i686.whl (95.4 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp27-cp27mu-manylinux1_x86_64.whl (118.4 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp27-cp27m-win32.whl (30.8 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp27-cp27m-win_amd64.whl (26.4 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp34-cp34m-macosx_10_6_intel.whl (68.2 kB) File type Wheel Python version cp34 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp34-cp34m-manylinux1_i686.whl (96.3 kB) File type Wheel Python version cp34 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp34-cp34m-manylinux1_x86_64.whl (119.7 kB) File type Wheel Python version cp34 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp34-cp34m-win32.whl (31.5 kB) File type Wheel Python version cp34 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp34-cp34m-win_amd64.whl (26.8 kB) File type Wheel Python version cp34 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp35-cp35m-macosx_10_6_intel.whl (68.2 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp35-cp35m-manylinux1_i686.whl (96.5 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp35-cp35m-manylinux1_x86_64.whl (119.9 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp35-cp35m-win32.whl (16.7 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp35-cp35m-win_amd64.whl (17.5 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp36-cp36m-macosx_10_6_intel.whl (68.2 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp36-cp36m-manylinux1_i686.whl (96.7 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp36-cp36m-manylinux1_x86_64.whl (120.1 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp36-cp36m-win32.whl (16.7 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp36-cp36m-win_amd64.whl (17.4 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp37-cp37m-macosx_10_6_intel.whl (68.2 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp37-cp37m-manylinux1_i686.whl (97.7 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp37-cp37m-manylinux1_x86_64.whl (121.1 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp37-cp37m-win32.whl (16.7 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size xxhash-1.4.2-cp37-cp37m-win_amd64.whl (17.5 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size xxhash-1.4.2.tar.gz (43.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page