用于xxhash的python绑定
xxhash的Python项目详细描述
xxhash是由Yann Collet为xxHash库绑定的python。
安装
$ pip install xxhash
从源安装
$ pip install --no-binary xxhash xxhash
先决条件
在debian/ubuntu上:
$ apt-get install python-dev gcc
在CentOS/Fedora上:
$ yum install python-devel gcc redhat-rpm-config
链接到libxxhash.so
默认情况下,python xxhash将使用捆绑的xxhash, 我们可以通过指定env varXXHASH_LINK_SO:
$ XXHASH_LINK_SO=1 pip install --no-binary xxhash xxhash
用法
模块版本及其后端xxhash库版本可以使用 模块属性分别为VERSION和XXHASH_VERSION。
>>>importxxhash>>>xxhash.VERSION'1.4.0'>>>xxhash.XXHASH_VERSION'0.7.1'
这个模块与hashlib兼容,这意味着您可以用与hashlib.md5相同的方式使用它。
update() – update the current digest with an additional stringdigest() – return the current digest valuehexdigest() – return the current digest as a string of hexadecimal digitsintdigest() – return the current digest as an integercopy() – return a copy of the current xxhash objectreset() – reset state
MD5摘要返回字节,但原始的XXH32和XXH64 C API返回整数。 当这个模块与hashlib兼容时,intdigest()也被提供给 获取整数摘要。
此模块提供的哈希算法的构造函数是xxh32()和xxh64()。
例如,要获取字节字符串的摘要b'Nobody inspects the spammish repetition':
>>>importxxhash>>>x=xxhash.xxh32()>>>x.update(b'Nobody inspects')>>>x.update(b' the spammish repetition')>>>x.digest()b'\xe2);/'>>>x.digest_size4>>>x.block_size16
更精简:
>>>xxhash.xxh32(b'Nobody inspects the spammish repetition').hexdigest()'e2293b2f'>>>xxhash.xxh32(b'Nobody inspects the spammish repetition').digest()==x.digest()True
可选种子(默认值为0)可用于按预期更改结果:
>>>importxxhash>>>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()返回big endian的字节整数表示 摘要:
>>>importxxhash>>>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外,还提供了一炮功能。 所以我们可以避免在堆上分配xxh32/64状态:
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)
>>>importxxhash>>>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]:importxxhashIn[2]:%timeitxxhash.xxh64_hexdigest('xxhash')268ns±24.1nsperloop(mean±std.dev.of7runs,1000000loopseach)In[3]:%timeitxxhash.xxh64('xxhash').hexdigest()416ns±17.3nsperloop(mean±std.dev.of7runs,1000000loopseach)
注意事项
种子溢出
xxh32采用无符号32位整数作为种子,xxh64采用 作为种子的无符号64位整数。确保种子大于 或等于0。
端部
从python xxhash 0.3.0开始,digest()返回 big endian整数摘要的表示。它使用 做一个小恩迪亚。
在hmac中不要使用xxhash
虽然可以使用xxhash作为HMAC哈希函数,但是 强烈建议不要。
xxhash是not加密哈希函数,它是 以速度和质量为目标的非加密散列算法。 不要将xxhash放在加密哈希的任何位置 功能是必需的。