Skip to main content

A Python wrapper for SpookyHash version 2

Project description

spookyhash

A Python wrapper of Bob Jenkins' SpookyHash version 2. Offers 32- 64- and 128-bit oneshot and incremental hashes.

License

Licensed under the MIT license. See the LICENSE file in the repository root for more details.

Usage

Installation

Available through spookyhash - PyPI using pip install spookyhash.

Oneshot Hashes

>>> import spookyhash

>>> spookyhash.hash32(b'hello world')
2617184861
>>> spookyhash.hash32(b'hello world', seed=0x12345678)
3380090220

>>> spookyhash.hash64(b'hello world')
14865987102431973981
>>> spookyhash.hash64(b'hello world', seed=123)
5719863273689036421

>>> spookyhash.hash128(b'hello world')
185933735475381961281710998418114941533
>>> spookyhash.hash128(b'hello world', seed1=123_000, seed2=456_000)
144121310386202441278894605216246194925

>>> # For a more comparable result to other libraries
>>> spookyhash.hash128_pair(b'hello world')
(14865987102431973981, 10079487997037711397)
>>> spookyhash.hash128_pair(b'hello world', seed1=123_000, seed2=456_000)
(12678109464562819821, 7812831891108919044)

Incremental Hashes

>>> import spookyhash

>>> sh = spookyhash.Hash32()
>>> sh.update(b'hello')
>>> sh.update(b' ')
>>> sh.update(b'world')
>>> sh.final()
2617184861
>>> sh.hexdigest()
'5d12ff9b'

>>> spookyhash.Hash64(b'hello ', seed=123).update(b'world').final()
5719863273689036421

>>> spookyhash.Hash64(b'hello ', seed=123).update(b'world').hexdigest()
'85b609a05709614f'

>>> sh = spookyhash.Hash128(seed1=123_000, seed2=456_000)
>>> sh.update(b'hello world')
>>> sh.final()
144121310386202441278894605216246194925
>>> sh.final_pair()
(12678109464562819821, 7812831891108919044)
>>> sh.hexdigest()
'ede2c8f262b1f1af04f763f735c16c6c'

memoryview Support

Includes memoryview compatible types, such as NumPy arrays.

>>> import spookyhash
>>> import numpy as np

>>> spookyhash.Hash64(np.arange(100)).hexdigest()
'43ab5363ad362c74'

>>> spookyhash.Hash64(memoryview(b'hello world')).hexdigest()
'5d12ff9b81984ece'

Platform Independence

If run on a big-endian system, the code would produce different hashes, but of equal quality.

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

spookyhash-2.1.0.tar.gz (121.4 kB view hashes)

Uploaded source

Built Distributions

spookyhash-2.1.0-cp310-cp310-win_amd64.whl (65.6 kB view hashes)

Uploaded cp310

spookyhash-2.1.0-cp39-cp39-win_amd64.whl (66.9 kB view hashes)

Uploaded cp39

spookyhash-2.1.0-cp38-cp38-win_amd64.whl (66.7 kB view hashes)

Uploaded cp38

spookyhash-2.1.0-cp37-cp37m-win_amd64.whl (65.8 kB view hashes)

Uploaded cp37

Supported by

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