What is the best way to generate a unique 64 bit numeric database key from a string?
For example: if the string is a URL, you could generate a 64 bit key using SipHash or by truncating a SHA256 hash to 64 bits, but collisions are likely at $2^{32}$ (~ 4 billion) entries.
However, if instead you truncated the hash to (64 bits - X bits)
, and concatenated the resulting hash with the first X
bits of the input string, you would reduce the chance of collisions since two similar strings are unlikely to produce the same hash code:
key_bits = 64
x_bits = 8
hash_code = sha256(url_string)
key = truncate(hash_code, key_bits - x_bits) + truncate(url_string, x_bits)
Is this correct? Is there a better way?