hashing function guaranteed to be unique?

hash, md5

Solution

It's logically impossible to get a 32 byte code from a 200 byte source which is unique among all possible 200 byte sources, since you can store more information in 200 bytes than in 32 bytes.

They only exception would be that the information stored in these 200 bytes would also fit into 32 bytes, in which case your source date format would be extremely inefficient and space-wasting.

Problem

In our app we're going to be handed png images along with a ~200 character byte array. I want to save the image with a filename corresponding to that bytearray, but not the bytearray itself, as i don't want 200 character filenames. So, what i thought was that i would save the bytearray into the database, and then MD5 it to get a short filename. When it comes time to display a particular image, i look up its bytearray, MD5 it, then look for that file. So far so good. The problem is that potentially two different bytearrays could hash down to the same MD5. Then, one file would effectively overwrite another. Or could they? I guess my questions are - Could two ~200 char bytearrays MD5-hash down to the same string? - If they could, is it a once-per-10-ages-of-the-universe sort of deal or something that could conceivably happen in my app? - Is there a hashing algorithm that will produce a (say) 32 char string that's guaranteed to be unique?

Original source