Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the hash of 2 different inputs be the same?

According to my understanding, Hashing is a process of producing a unique fixed length (let's assume 64bit) output to an input of ANY length. (correct me if am wrong)

So if I take all the (x) possible 64bit hash values that a hash function can produce and append a 0 or 1 at the end of it. I get a list of size 2x (where each hash is 65bit long).

If I give all the 2x combinations as the input to the same hash function, how can it generate a unique hash for all the inputs?

like image 399
Shankar Thyagarajan Avatar asked Oct 18 '25 06:10

Shankar Thyagarajan


2 Answers

You are correct. This is called a hash collision, and it's a real thing. The reason it's not a bigger deal is that the number of hashes is so overwhelmingly large that these types of collisions are rare. Your example of 64 bits is a little unrealistic, though. 256 bits or 512 bits is a more likely scenario. (Even 128 is no longer considered strong enough.) And the range of hashes in this case is so large that finding inputs that create a hash collision is very difficult.

like image 154
jwismar Avatar answered Oct 21 '25 08:10

jwismar


By the Pigeonhole principle, hash collisions are inevitable. That is it is inevitable to find two distinct messages m1 != m2 such that their hash are equal H(m1) = H(m2)

Therefore, one cannot generate unique hashes for the inputs. With a very very small probability ( negligible ), there will be a collision. Even, inside of 264 possible values, there can be a collision for a hash function with 64-bit output.

Better use a Hash function like SHA3-512 or BLAKE2b and if you really want them unique, compare them with previous hashes that you generate. If you find a collision, you will be famous.

  • SHA3 family can generate 224, 256, 384, or 512-bit outputs.
like image 32
kelalaka Avatar answered Oct 21 '25 07:10

kelalaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!