对int散列唯一的int [英] Unique int to int hash

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

问题描述


  1. 我很好奇是否有一些简单和/或众所周知的散列方法将32位int转换为另一个32位int
  2. 没有两个不相等的输入产生相同的输出

  3. 不应该立即(a)和散列(a + 1)应该有非常不同的输出,就像散列(a)和散列(a& amp; ; 0x100000)。 (这只是简单地用一个随机值异或。)

尽管理论上显然存在这样的系统,但在实践中是否存在这种系统?

解决方案

简单的解决方案是创建一个位顺序变化数组。一些加密函数基于这种方法。

  uint8_t arr [32] = {4,7,24,9,15, 3,...}; //你知道的订单
uint32_t orgVal;
uint32_t modVal = 0;
uint32_t pos = 1;

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





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



正如评论所指出的那样,如果你看一下这些比特,差异将是最小的:0和1的数量是相同的。为了解决这个问题,你可以考虑使用位顺序改变和xor 。那么原始值和结果值之间的差异将更加显着。


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

  1. It transforms a 32-bit int into another 32-bit int
  2. No two non-equal inputs produce the same output
  3. It shouldn't be immediately obvious from looking at the output that two inputs were similar (in terms of difference and bitmask), meaning that hash(a) and hash(a+1) should have vastly different outputs, as should hash(a) and hash(a & 0x100000). (This rules out simply XORing with a random value.)

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;
}

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

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天全站免登陆