I am trying to do an implementation of Fowler–Noll–Vo hash function
The Pseudocode looks like this
hash = FNV_offset_basis
for each byte_of_data to be hashed
hash = hash × FNV_prime
hash = hash XOR byte_of_data
return hash
This is my code for that
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
}
Now I am using it as this
int main()
{
computeHash("Hello");
}
I am testing my result here and I get the result as 0d47307150c412cf
Update:
I fixed my types to
uint8_t byte_of_data;
uint64_t hash;
uint64_t FNV_offset_basis;
uint64_t FNV_prime;
and I get the result fa365282a44c0ba7 which still does not match the result 0d47307150c412cf
Any suggestions on how I can fix this