我試圖做的Fowler–Noll–Vo hash function任何人都可以請檢查,如果我這樣做的散列正確
中的僞像這樣
hash = FNV_offset_basis
for each byte_of_data to be hashed
hash = hash × FNV_prime
hash = hash XOR byte_of_data
return hash
實現這是我對於
uint8_t byte_of_data;
uint16_t hash;
uint16_t FNV_offset_basis;
uint16_t FNV_prime;
void computeHash(std::string p)
{
FNV_offset_basis = 0xcbf29ce484222325;
FNV_prime = 0x100000001b3;
hash = FNV_offset_basis;
//Iterate through the string
for(int i=0 ; i<p.size();i++)
{
hash = hash * FNV_prime;
hash = hash^p.at(i);
}
std::cout << hash; //output 2983
std::cout << std::hex << hash ; //ba7
}
代碼
現在我正在使用它作爲此
int main()
{
computeHash("Hello");
}
我測試我的結果here和我得到的結果作爲0d47307150c412cf
更新:
我固定我的類型
uint8_t byte_of_data;
uint64_t hash;
uint64_t FNV_offset_basis;
uint64_t FNV_prime;
,我得到的結果fa365282a44c0ba7仍然與結果不符 0d47307150c412cf
我如何能解決這個問題
是否沒有發佈測試向量,您可以編寫單元測試? –
a)您需要64位整數,而不是16 – deviantfan
如果您的散列的類型爲'uint16_t',那麼您希望如何獲得64位長的結果? –