将 17 个字符的密钥哈希为 4 个字节的值
我有一位同事正在努力解决哈希问题。
有一个 17 位字母数字值的密钥(VIN 代码)需要转换为 4 字节值(也可以是字母数字)。知道这 4 个字节会限制键的数量,您会看到什么完美哈希算法来解决这个问题?
I have a colleague struggling with a hashing problem.
There is a 17-alphanumeric valued key (a VIN code) that needs to be converted into a 4 byte value (could be alphanumeric as well). Knowing that those 4 bytes will limit the number of keys, what perfect-hash algorithm would you see for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
快速浏览一下 Wikipedia 后,我认为您可以先“压缩”密钥,或者换句话说,你分两个阶段进行哈希处理。
第一阶段:按照标准将密钥分解为各个部分,并独立进行定制哈希。
第 2 阶段:将哈希值放在一起,然后进行普通哈希。
一个简单的例子:
如果你的数据仅限于美国,那么前 2 个字节只有 27 种可能性,因此前 2 个字节可以哈希为 0 - 26。(假设我们在这里得到
a
.)然后假设其他字节有N种可能性,并且可以哈希为0 - N-1。 (假设我们在这里得到
b
。)组合结果可以是
a * N + b
。然后做一个普通的哈希(如果26 * N > 4个字节可以表达什么)。After a quick look at Wikipedia, I think you could first "compress" the key, or in other word, you do hash in 2 stages.
Stage 1: break down the key to individual parts according to the standard, and do customized hash independently.
Stage 2: Get hashes together, and do a normal hash.
An naive example:
If your data is limited to United States, there is only 27 possibility of the first 2 bytes, so the first 2 bytes can be hashed to 0 - 26. (Suppose we get
a
here.)Then suppose other bytes have N possibilities, and can be hashed to 0 - N-1. (Suppose we get
b
here.)The combinational result can be
a * N + b
. Then do a normal hash (if 26 * N > what 4 bytes can express).您正在谈论哈希函数,因此 f(x0) == f(x1) 且 x0 != x1 是可以的。
一个好的哈希函数应该具有均匀分布的哈希值。
例如,您可以将组成 17 位值的 4 个字节组添加在一起,并仅保留权重最低的剩余 4 个字节。
You are talking about a Hash function, so it is ok to have f(x0) == f(x1) with x0 != x1.
A good hash function should have the hashed values homogeneously distributed.
You can add the groups of 4 bytes which compose the 17-digit value together, and only keep the remaining 4 bytes with the lowest weight, for example.