唯一 int 到 int 哈希 [英] Unique int to int hash

查看:15
本文介绍了唯一 int 到 int 哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇是否存在一些具有以下属性的简单和/或众所周知的哈希方法:

I'm curious as to whether or not there's some simple and/or well known hash method with the following properties:

  1. 它将一个 32 位 int 转换为另一个 32 位 int
  2. 没有两个不相等的输入产生相同的输出
  3. 从输出来看,两个输入是相似的(在差异和位掩码方面)不应立即显而易见,这意味着 hash(a) 和 hash(a+1) 应该有截然不同的输出,应该如此hash(a) 和 hash(a & 0x100000).(这排除了简单地与随机值进行异或.)

虽然理论上肯定存在这样的系统,但在实践中是否存在?

While such systems must obviously exist in theory, are there any in practice?

推荐答案

一个简单的解决方案是制作一个位序更改数组.一些加密函数就是基于这种方法的.

A simple solution would be to make a bit order change array. Some encryption functions are based on this method.

uint8_t arr[32]={4,7,24,9,15,3,...}; // an order you know
uint32_t orgVal;
uint32_t modVal =0;
uint32_t pos = 1;

for (int i=0; i<32;i++) {
  modVal += (orgVal&pos)? (1>>arr[i]):0;
  pos*=2;
}

(代码是从头开始编写的,没有 IDE 或测试;它可能无法工作)

(the code was made from scratch and without IDE or testing; it may not work)

正如评论中所指出的,如果您查看位,差异将很小:0 和 1 的数量将相同.为了解决这个问题,您可以考虑使用位序改变和异或.那么原始值和结果值之间的差异将更加显着.

As pointed in the comments, the difference will be minimal if you look at the bits: the amount of 0s and 1s will be the same. To solve this problem you may consider using both bit order change and xor. Then the difference between the original and resulting values will be more significant.

这篇关于唯一 int 到 int 哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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