Lightweight 8 byte hash function algorithm

后端 未结 4 2281
旧巷少年郎
旧巷少年郎 2021-01-02 13:27

I need to extract an 8 byte digest from a variable length string so I\'m looking for such an algorithm that I will implement in c/c++. That will be part of a digital signatu

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-02 14:07

    There is no chance to do a secure hash in 64 bits. Even SHA-1 at 160 bit is considered theoretically broken. You should use SHA2-256 if you really care about secure digital signing. If you don't care about security and just want a hash function that avoids non-adversarial collisions just use the following, it is fine:

    constexpr uint64 P1 = 7;
    constexpr uint64 P2 = 31;
    
    uint64 hash = P1;
    for (const char* p = s; *p != 0; p++) {
        hash = hash * P2 + *p;
    }
    

提交回复
热议问题