Best algorithm for hashing number values?

前端 未结 8 1339
后悔当初
后悔当初 2021-02-01 10:24

When dealing with a series of numbers, and wanting to use hash results for security reasons, what would be the best way to generate a hash value from a given series of digits?

8条回答
  •  独厮守ぢ
    2021-02-01 10:34

    For a non cryptographic approach you could take a look at the FNV hash it's fast with a low collision rate.

    As a very fast alternative, I've also used this algorithm for a few years and had few collision issues however I can't give you a mathematical analysis of it's inherent soundness but for what it's worth here it is

    =Edit - My code sample was incorrect - now fixed =

    In c/c++

    unsigned int Hash(const char *s)
    {
        int hash = 0;
    
        while (*s != 0)
        {
            hash *= 37;
                hash += *s;
            s++;
        }
    
        return hash;
    }
    

    Note that '37' is a magic number, so chosen because it's prime

提交回复
热议问题