# Unique/Fast Hash for fixed size strings

I have a need to generate a unique hash for millions of Strings of same length. Would prefer a non cryptographic hashing algorithms for performance reasons. The hash can be of length upto 24 bytes, but hash collisions are a strict no.

A dumb implementation I can think of is to use the String.hashCode() method in java as 4 bytes [of the allowed 24 bytes]

• First 4 bytes = String.hashCode(string)
• Second 4 bytes = String.hashCode(reverse of string)
• Third 4 bytes = String.hashCode(first half of string)
• Fourth 4 bytes = String.hashCode(last half of string)

Thats just 16 bytes of hashes... and as the size of the string is fixed, would this give a unique 16byte hash for every different string? This is similar to using a bloom filter, but I need to ensure there are absolutely NO hash collisions.

Keeping this hash is memory if more efficient than keeping the entire string in memory.

• Well, how long are your strings? Jul 31, 2020 at 15:24
• "I need to ensure there are absolutely NO hash collisions"; unless you know all the strings before hand (and there are no more than $2^{192}$ of them), this is impossible to ensure. Would "there are NO hash collisions with extremely high probability" suffice? Also, would the strings possibly be generated by an adversary (who is trying to trick you into producing a collision)? Jul 31, 2020 at 15:32
• If no collision and non cryptographic See Perfect hash function Jul 31, 2020 at 15:53
• Size of string = 10K characters. I do NOT know all the strings before hand.. I was thinking if I can use more than 2 or 3 hash functions to make the possibility of a hash collision as 0. The strings will NOT be generated by any adversary.
– ESri
Jul 31, 2020 at 17:27
• If the number of possible strings is large ($\ggg 2^{192}$), then if you select two random strings from the list of possible strings, then those two strings will hash the same with probability at least $2^{-192}$, no matter how you design your hash function. This is not zero. Jul 31, 2020 at 19:15

Side note : not sure if it matters in your case, but note that in the case of expanding space, or even identical space, it's possible to guarantee no collision with some hash functions. For example, XXH64 and XXH3_64bits guarantee no collision for 8-bytes->8-bytes hashes. Similarly, XXH3_128bits guarantees no collision for 16->16 hashes. It's even easier when expanding space, for example for mapping 6-bytes input into 64-bit hashes results in no collision. Not all hash functions guarantee this outcome though. Select the ones advertising bijective transformations.