How do hashes really ensure uniqueness?
The simple answer is that hashes don't ensure uniqueness. Very broadly, hashes behave like "deterministic random numbers"&nbsp;&ndash; deterministic in the sense that hashing the same data always gives the same answer; random in the sense that the value of the hash is basically unpredictable without actually computing it. And sufficiently unpredictable that, for a good cryptographic hash, we don't know any way of finding a string with a particular hash, except for trial and error.

In particular, cryptographic hash values are close enough to truly random that the birthday paradox applies. That is, if there are $k$&nbsp;different values the hash can take and they're all equally likely, you have to hash roughly $\sqrt{k}$&nbsp;documents before the probability of finding two with the same hash is greater than a half.  So, if you're using a 256-bit hash, you'd need to look at about $\sqrt{2^{256}}=2^{128}\approx 3\times 10^{38}$ documents to even have a 50/50 chance of finding two with the same hash. For comparison, that's about 200 documents for every nanogram of mass in the Earth.