跳转到主要内容

xxHash的Python绑定

项目描述

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

xxhash-cffi是xxHash库的Python绑定,由Yann Collet编写。

安装

$ pip install xxhash-cffi

安装前提条件

如果您从源代码安装xxhash-cffi,您可能想要安装以下软件包。

在Debian/Ubuntu上

$ apt-get install libcffi-dev python-dev gcc

在CentOS/Fedora上

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

用法

可以使用模块属性VERSIONXXHASH_VERSION分别检索模块版本及其后端xxHash库版本。

>>> import xxhash_cffi as xxhash
>>> xxhash.VERSION
'1.0.1'
>>> xxhash.XXHASH_VERSION
'0.6.2'

此模块符合hashlib规范,这意味着您可以使用与hashlib.md5相同的方式使用它。

update() – 使用附加字符串更新当前摘要
digest() – 返回当前摘要值
hexdigest() – 返回当前摘要的十六进制数字字符串
intdigest() – 返回当前摘要的整数
copy() – 返回当前xxhash对象的副本
reset() – 重置状态

md5摘要返回字节,但原始xxh32和xxh64 C API返回整数。虽然此模块已符合hashlib规范,但仍然提供了intdigest()以获取整数摘要。

此模块提供的哈希算法构造函数是xxh32()xxh64()

例如,为了获取字节字符串b'Nobody inspects the spammish repetition'的摘要

>>> import xxhash_cffi as 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

更简洁的

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

可以使用一个可选的种子(默认为0)来有预测性地改变结果

>>> import xxhash_cffi as 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

请注意,xxh32函数接受无符号32位整数作为种子,而xxh64函数接受无符号64位整数。虽然无符号整数溢出是定义良好的行为,但最好避免这种情况发生。

>>> 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() 返回整数摘要的大端表示形式的字节。

>>> import xxhash_cffi as 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

除了上述xxh32/xxh64函数之外,还提供了oneshot函数。通过使用oneshot函数,我们可以避免在堆上分配XXH32/64_state。

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_cffi as 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'

注意事项

种子溢出

xxh32函数接受无符号32位整数作为种子,xxh64函数接受无符号64位整数作为种子。请确保种子大于或等于0

不要在HMAC中使用XXHASH

虽然可以将xxhash用作HMAC哈希函数,但强烈建议不要这样做。

xxhash不是加密哈希函数,它是一个旨在速度和质量的非加密哈希算法。不要将xxhash放在需要加密哈希函数的任何位置。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

xxhash-cffi-1.3.0.tar.gz (19.6 kB 查看哈希值)

上传时间 源代码

构建的分发

xxhash_cffi-1.3.0-cp37-cp37m-win_amd64.whl (17.7 kB 查看哈希值)

上传时间 CPython 3.7m Windows x86-64

xxhash_cffi-1.3.0-cp37-cp37m-win32.whl (18.0 kB 查看哈希值)

上传时间 CPython 3.7m Windows x86

xxhash_cffi-1.3.0-cp37-cp37m-manylinux1_x86_64.whl (38.7 kB 查看哈希值)

上传时间 CPython 3.7m

xxhash_cffi-1.3.0-cp37-cp37m-manylinux1_i686.whl (40.7 kB 查看哈希值)

上传时间 CPython 3.7m

xxhash_cffi-1.3.0-cp37-cp37m-macosx_10_6_intel.whl (23.2 kB 查看哈希值)

上传时间: CPython 3.7m macOS 10.6+ intel

xxhash_cffi-1.3.0-cp36-cp36m-win_amd64.whl (17.7 kB 查看哈希值)

上传时间: CPython 3.6m Windows x86-64

xxhash_cffi-1.3.0-cp36-cp36m-win32.whl (18.0 kB 查看哈希值)

上传时间: CPython 3.6m Windows x86

xxhash_cffi-1.3.0-cp36-cp36m-manylinux1_x86_64.whl (38.6 kB 查看哈希值)

上传时间: CPython 3.6m

xxhash_cffi-1.3.0-cp36-cp36m-manylinux1_i686.whl (40.7 kB 查看哈希值)

上传时间: CPython 3.6m

xxhash_cffi-1.3.0-cp36-cp36m-macosx_10_6_intel.whl (23.2 kB 查看哈希值)

上传时间: CPython 3.6m macOS 10.6+ intel

xxhash_cffi-1.3.0-cp35-cp35m-win_amd64.whl (17.7 kB 查看哈希值)

上传时间: CPython 3.5m Windows x86-64

xxhash_cffi-1.3.0-cp35-cp35m-win32.whl (18.0 kB 查看哈希值)

上传时间: CPython 3.5m Windows x86

xxhash_cffi-1.3.0-cp35-cp35m-manylinux1_x86_64.whl (38.6 kB 查看哈希值)

上传时间: CPython 3.5m

xxhash_cffi-1.3.0-cp35-cp35m-manylinux1_i686.whl (40.7 kB 查看哈希值)

上传时间: CPython 3.5m

xxhash_cffi-1.3.0-cp35-cp35m-macosx_10_6_intel.whl (23.2 kB 查看哈希值)

上传时间: CPython 3.5m macOS 10.6+ intel

xxhash_cffi-1.3.0-cp34-cp34m-win_amd64.whl (14.7 kB 查看哈希值)

上传于 CPython 3.4m Windows x86-64

xxhash_cffi-1.3.0-cp34-cp34m-win32.whl (15.6 kB 查看哈希值)

上传于 CPython 3.4m Windows x86

xxhash_cffi-1.3.0-cp34-cp34m-manylinux1_x86_64.whl (38.6 kB 查看哈希值)

上传于 CPython 3.4m

xxhash_cffi-1.3.0-cp34-cp34m-manylinux1_i686.whl (40.7 kB 查看哈希值)

上传于 CPython 3.4m

xxhash_cffi-1.3.0-cp34-cp34m-macosx_10_6_intel.whl (23.2 kB 查看哈希值)

上传于 CPython 3.4m macOS 10.6+ intel

xxhash_cffi-1.3.0-cp27-cp27mu-manylinux1_x86_64.whl (41.9 kB 查看哈希值)

上传于 CPython 2.7mu

xxhash_cffi-1.3.0-cp27-cp27mu-manylinux1_i686.whl (43.9 kB 查看哈希值)

上传于 CPython 2.7mu

xxhash_cffi-1.3.0-cp27-cp27m-win_amd64.whl (14.9 kB 查看哈希值)

上传于 CPython 2.7m Windows x86-64

xxhash_cffi-1.3.0-cp27-cp27m-win32.whl (15.7 kB 查看哈希值)

上传于 CPython 2.7m Windows x86

xxhash_cffi-1.3.0-cp27-cp27m-manylinux1_x86_64.whl (41.9 kB 查看哈希值)

上传于 CPython 2.7m

xxhash_cffi-1.3.0-cp27-cp27m-manylinux1_i686.whl (43.9 kB 查看哈希值)

上传于 CPython 2.7m

xxhash_cffi-1.3.0-cp27-cp27m-macosx_10_6_intel.whl (23.1 kB 查看哈希值)

上传于 CPython 2.7m macOS 10.6+ intel

支持