# Avoiding collisions by combining MD5 and SHA-2

It is often suggested that hashing of long character columns can be used for indexing in databases, but the possibility of collisions is an issue for Unique indexes. Whilst I know that both MD5 and SHA-256 can (rarely) produce collisions, I was wondering what the possibility of them both clashing at the same point with the same input? In other words, if I produce a Unique key by running both MD5 and SHA-2 against the same character field and combining them to make the Unique key, is this collision free.

• If you find a collision for SHA256 you will be famous. The collision probability is $2^{128}$ with 50%. If you fear just use a 512 bit hash like SHA-512. And note that there question and anwers for this in this site. – kelalaka Sep 3 '20 at 16:11
• Perhaps you meant SHA-1 instead of SHA-256? If so, see crypto.stackexchange.com/questions/36988/… which asks about the effort needed to find a simultaneous collision... – poncho Sep 3 '20 at 18:07
• – Richie Frame Sep 3 '20 at 20:24
• @kelalaka to be pedantic, the probability of a collision after $2^{128}$ (uniformly random) hashes is approximately $e^{-k^2 / 2n}$ (with $k = 2^{128}, n = 2^{256}$), which is slightly over $60\%$. Of course, your heuristic is close enough for practical purposes. – Daniel Lubarov Sep 4 '20 at 6:58
• @DanielLubarov Yes, If we provide better approaches. It was a mobile comment, anyway, I should use $\approx$ – kelalaka Sep 4 '20 at 8:27

Generally speaking if we have two different hash functions and $$f$$ and $$g$$ and you hash some input $$x$$ with both and use the concatenation $$f(x)||g(x)$$ the collision resistance of this will be better than either function individually and in some cases much better. If the functions are unrelated and you are only worried about chance collision the likelihood of a collision will be the product of the likelihood of collision in each function individually. However this last statement will not hold in face of an attacker exploiting weakness in these function. It's still harder that either individually but may not be noticeably harder to find a collision than finding a collision in just one of the two.
Other techniques of combining the functions such as $$f(g(x))$$ are ill advised and won't add security, since any collision of $$g$$ is also a collision in the composition.
• @kelalaka A collision in $g$ directly implies a collision in $f\circ g$ – Mark Sep 4 '20 at 2:35