python非加密哈希库
pyhash的Python项目详细描述
简介
pyhash
是一个python非加密哈希库。
为性能和兼容性提供了几种常用的用C/C++实现的哈希算法。
>>>importpyhash>>>hasher=pyhash.fnv1_32()>>>hasher('hello world')2805756500L>>>hasher('hello',' ','world')2805756500L>>>hasher('world',seed=hasher('hello '))2805756500L
它也可以用来产生没有种子的指纹。
>>>importpyhash>>>fp=pyhash.farm_fingerprint_64()>>>fp('hello')>>>13009744463427800296L>>>fp('hello','world')>>>[13009744463427800296L,16436542438370751598L]
注释
hasher('hello', ' ', 'world')
是hasher('world', seed=hasher(' ', seed=hasher('hello')))
的语法糖,可能不等于hasher('hello world')
,因为某些哈希算法使用不同的hash
和seed
大小。
例如,metro
哈希始终对64/128位哈希值使用32位种子。
>>>importpyhash>>>hasher=pyhash.metro_64()>>>hasher('hello world')>>>5622782129197849471L>>>hasher('hello',' ','world')>>>16402988188088019159L>>>hasher('world',seed=hasher(' ',seed=hasher('hello')))>>>16402988188088019159L
安装
$ pip install pyhash
注释
如果pip
安装失败并出现类似错误,#27
/usr/lib/gcc/x86_64-linux-gnu/6/include/smmintrin.h:846:1: error: inlining failed in call to always_inline 'long long unsigned int _mm_crc32_u64(long long unsigned int, long long unsigned int)': target specific option mismatch
_mm_crc32_u64 (unsigned long long __C, unsigned long long __V)
^~~~~~~~~~~~~
src/smhasher/metrohash64crc.cpp:52:34: note: called from here
v[0] ^= _mm_crc32_u64(v[0], read_u64(ptr)); ptr += 8;
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
请将pip
和setuptools
升级到最新版本,然后重试
$ pip install --upgrade pip setuptools
注释
如果pip
在macos上安装失败,并出现类似错误#28
creating build/temp.macosx-10.6-intel-3.6
...
/usr/bin/clang -fno-strict-aliasing -Wsign-compare -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -g -c src/smhasher/metrohash64crc.cpp -o build/temp.macosx-10.6-intel-3.6/src/smhasher/metrohash64crc.o -msse4.2 -maes -mavx -mavx2
src/smhasher/metrohash64crc.cpp:52:21: error: use of undeclared identifier '_mm_crc32_u64'
v[0] ^= _mm_crc32_u64(v[0], read_u64(ptr)); ptr += 8;
^
您可以尝试
$ CFLAGS="-mmacosx-version-min=10.13" pip install pyhash
注释
pyhash
只支持pypy
v6.0或更新版本,请download and install最新的pypy
。
算法
pyhash支持以下哈希算法
- FNV(fowler noll vo)哈希
- fnv1_32
- fnv1a_32
- FNV1_64
- FNV1A_64
- MurmurHash
- 杂音1/32
- 杂音1_对齐_32
- 杂音2_32
- 杂音2a_32
- 杂音2对齐
- 杂音2_中性_32
- 杂音2_x64_64a
- 杂音2_x86_64b
- 杂音3_32
- 杂音3_x86_128
- 杂音3*64*128
- lookup3
- 查找3
- 查找3_little
- 查找3_大
- SuperFastHash
- 超级快速哈希
- City Hash
_城市32
- 城市64
- 城市128
- 城市轨道交通128
- 城市指纹
- Spooky Hash
- 诡异的32
- 诡异的
- 恐怖的128
- FarmHash
- 农场32
- 农场64
- 农场128
- 农场指纹
- 农场指纹
- 农场指纹
- MetroHash
- 地铁64
- 地铁128
- 地铁64
- 地铁128
- MumHash
- 妈妈64
- T1Ha
- t1ha2(64位小尾数)
- t1ha2_128(128位小尾数)
- t1ha1(64位本机结束符)
- t1ha1 le(64位小尾数)
- t1ha1_be(64位大端)
- t1ha0(64位,运行时选择最快的函数。)
t1_32t1_32_bet1 64t1_64_be
- XXHash
- xx_32
- xx_64
字符串和字节文本
python有两种类型可以用来表示字符串文本,这两种类型的散列值绝对不同。
- 对于python 2.x String literals,默认情况下将使用
str
,unicode
可以与u
前缀一起使用。 - 对于python 3.x String and Bytes literals,默认情况下将使用
unicode
,bytes
可以与b
前缀一起使用。
例如,
$ python2
Python 2.7.15 (default, Jun 17 2018, 12:46:58)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyhash
>>> hasher = pyhash.murmur3_32()
>>> hasher('foo')
4138058784L
>>> hasher(u'foo')
2085578581L
>>> hasher(b'foo')
4138058784L
$ python3
Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyhash
>>> hasher = pyhash.murmur3_32()
>>> hasher('foo')
2085578581
>>> hasher(u'foo')
2085578581
>>> hasher(b'foo')
4138058784
您还可以导入unicode_literals以在python 2.x中使用unicode文本
from__future__importunicode_literals
In general, it is more compelling to use unicode_literals when back-porting new or existing Python 3 code to Python 2/3 than when porting existing Python 2 code to 2/3. In the latter case, explicitly marking up all unicode string literals with u'' prefixes would help to avoid unintentionally changing the existing Python 2 API. However, if changing the existing Python 2 API is not a concern, using unicode_literals may speed up the porting process.