防止碰撞而非攻击的哈希函数(产生随机UUID大小的结果空间)
使用SHA1算法将较大的字符串进行哈希处理,这样它们就可以作为数据库中的键。
我想从原始字符串生成一个UUID大小的字符串,这个字符串要足够随机和大,以防止碰撞,但又比原始字符串小得多。
这个用途与安全性无关。
示例:
# Take a very long string, hash it down to a smaller string behind the scenes and use
# the hashed key as the data base primary key instead
def _get_database_key(very_long_key):
return hashlib.sha1(very_long_key).digest()
SHA1算法适合用来做这个吗?还是有其他更合适的选择?