简短的Python字母数字哈希,具有最小的冲突 [英] Short Python alphanumeric hash with minimal collisions

查看:101
本文介绍了简短的Python字母数字哈希,具有最小的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用某种哈希函数为表设置非整数主键. md5()似乎很长(32个字符).

I'd like to set non-integer primary keys for a table using some kind of hash function. md5() seems to be kind of long (32-characters).

还有哪些替代散列函数可以使用字母表中的每个字母,也可以使用字符串长度较短且冲突率较低的整数?

What are some alternative hash functions that perhaps use every letter in the alphabet as well as integers that are perhaps shorter in string length and have low collision rates?

谢谢!

推荐答案

为什么不截断SHA1或MD5?如果不进行截断,就会有更多的冲突,但是它比设计自己的要好.请注意,您可以对截断的哈希值进行base64编码,而不是使用十六进制.例如

Why don't you just truncate SHA1 or MD5? You'll have more collisions then if you didn't truncate, but it's still better than designing your own. Note that you can base64-encode the truncated hash, rather than using hexadecimal. E.g.

import base64
import hashlib
hasher = hashlib.sha1("The quick brown fox")
base64.urlsafe_b64encode(hasher.digest()[:10])

只要您能了解取舍,就可以截短(包括不截断)或任意截断.

You can truncate as little (including not at all) or as much as you want, as long as you understand the tradeoffs.

由于您提到了URL安全,因此可以使用 urlsafe_b64encode urlsafe_b64decode ,它使用-_而不是+/.

Since you mentioned URL-safe, you can use urlsafe_b64encode and urlsafe_b64decode, which uses - and _ rather than + and /.

这篇关于简短的Python字母数字哈希,具有最小的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆