Best algorithm for hashing number values?

前端 未结 8 1338
后悔当初
后悔当初 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:43

    This seems to be a case for key derivation functions. Have a look at PBKDF2.

    Just using cryptographic hash functions (like the SHA family) will give you the desired distribution, but for very limited input spaces (like credit card numbers) they can be easily attacked using brute force because this hash algorithms are usually designed to be as fast as possible.

    UPDATE

    Okay, security is no concern for your task. Because you have already a numerical input, you could just use this (account) number modulo your hash table size. If you process it as string, you might indeed encounter a bad distribution, because the ten digits form only a small subset of all possible characters.

    Another problem is probably that the numbers form big clusters of assigned (account) numbers with large regions of unassigned numbers between them. In this case I would suggest to try highly non-linear hash function to spread this clusters. And this brings us back to cryptographic hash functions. Maybe good old MD5. Just split the 128 bit hash in four groups of 32 bits, combine them using XOR, and interpret the result as a 32 bit integer.

    While not directly related, you may also have a look at Benford's law - it provides some insight why numbers are usually not evenly distributed.

    0 讨论(0)
  • 2021-02-01 10:50

    By definition, a cryptographic hash will work perfectly for your use case. Even if the characters are close, the hash should be nicely distributed.

    So I advise you to use any cryptographic hash (SHA-256 for example), with a salt.

    0 讨论(0)
提交回复
热议问题