[英]How to reverse engineer hash function
我正在嘗試創建一個函數,該函數根據希望值在哈希表中的位置生成哈希鍵。
我的哈希函數是(a + b * (key) ) % c = hash value
。 我在SO上看到了與此類似的問題 ,我嘗試用d
替換b * (key)
並執行以下操作:
private int ReverseModulus(int a, int b, int c, int hashValue)
{
if(hashValue >= c)
return -1;
if(a < hashValue)
return (hashValue - a) / b;
return (c + hashValue - a) / b;
}
但似乎大多數時候hashValue != Hash(ReverseModulus(a,b,c, hashValue))
。
我想知道方法是否錯誤或代碼中是否只有錯誤。
您使用的是錯誤的划分方式。 您正在執行整數除法,但是您需要進行模塊化除法。 在Java中,您可以使用BigInteger
:
bh = new BigInteger(hashValue);
ba = new BigInteger(a);
bc = new BigInteger(c);
bn = bh.subtract(ba);
return bn.modInverse(bc).intValue();
C#大概具有類似的庫函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.