任何人都可以检查我是否正确地进行了哈希处理

Can anyone please check if I am doing this hashing correctly

我正在尝试执行 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

关于如何解决这个问题的任何建议

这是问题所在:

uint16_t          FNV_offset_basis;
uint16_t          FNV_prime;
void computeHash(std::string p)
{
    FNV_offset_basis =  0xcbf29ce484222325;
    FNV_prime        =  0x100000001b3;

FNV_primeFNV_offset_basis 在您的代码中都是 16 位整数,但您莫名其妙地向它们分配了长 64 位整数,您的 C++ 编译器应该警告您不正确的文字赋值。

如果将类型更改为 uint64_t 会怎样?

根据官方参考,您当前的结果fa365282a44c0ba7是正确的
源代码(C 语言)和手动计算...这使测试站点出错。

已链接参考源文件here: C file and H file
我删除了 longlong.h 的包含并添加了以下两个代码部分:

/*before the reference code*/

#include <stdint.h>
#define HAVE_64BIT_LONG_LONG
typedef uint64_t u_int64_t;
typedef uint32_t u_int32_t;

/*after it*/

#include<stdio.h>
int main()
{
    printf("%llx\n", fnv_64_str("Hello", FNV1_64_INIT));
}

使其与gcc -std=c11 source.c
一起编译 (gcc (i686-posix-sjlj-rev0, Built by MinGW-W64 project) 4.9.1)

输出:fa365282a44c0ba7.
And Ideone says so too