Lightweight 8 byte hash function algorithm

后端 未结 4 2265
旧巷少年郎
旧巷少年郎 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:08

    As AndrewTomazos-Fathomling said, it's impossible to do a secure hash in 64 bits, so if that's your intention then my advice is STOP, pick up a book and read about cryptographically secure hashing.

    If you don't plan on using this as a secure hash and you do not care about collisions or attacks, then the answer he gave you works just fine and you can tweak the primes P1 and P2 as necessary. I will give you another alternative which allows you to do tagged hashing and mixes things up more.

    // Disclaimer: I make no claims about the quality of this particular hash - it's 
    // certainly not a cryptographically secure hash, nor should it *ever* be 
    // construed as such. 
    
    unsigned long long quickhash64(const char *str, unsigned long long mix = 0)
    { // set 'mix' to some value other than zero if you want a tagged hash          
        const unsigned long long mulp = 2654435789;
    
        mix ^= 104395301;
    
        while(*str)
            mix += (*str++ * mulp) ^ (mix >> 23);
    
        return mix ^ (mix << 37);
    }
    

提交回复
热议问题