Is it known whether the MD5 algorithm is surjective?

Does MD5 map to all possible 128 bit numbers? To put it another way: For every 128 bit number y, does there exist at least one x for which md5(x) = y?

The answer probably is yes, for arbitrary x, and probably no proof exists. However, feel free to prove me wrong :)

But what if I impose some kind of limit on x, e.g. x <= x_max? Of course, for very small x_max the answer is no. But what is the minimum value of x_max for which the answer likely becomes yes? Or the maximum value for which the answer can be shown to be no?

I'm not really interested in the exact value for x_max, rather in the approximate order of magnitude.

migrated from stackoverflow.comJun 3 '15 at 11:26

This question came from our site for professional and enthusiast programmers.

• See stackoverflow.com/questions/2658601/…. In particular, the part in the accepted answer that talks about the "random oracle." It seems clear that the only way to prove surjectivity of a hash function is by exhaustive search. – Jim Mischel Jun 2 '15 at 16:16
• @jim-mischel: I did see that, and the second comment to that answer claims that its main argument is wrong. I can't verify that, or else I hadn't asked that question here. The question/answer also is five years old and doesn't mention MD5, so I thought I might try to get a more specific response :) – Daniel Sk Jun 3 '15 at 7:08
• I wonder if Computer Science was the best migration target. Information Security, Cryptography (because of the use of MD5 lies there) or even Mathematics (because you ask about mathematical properties of a function) may have been better. – Raphael Jun 3 '15 at 12:04

Q: Does MD5 map to all possible 128 bit numbers?

Taking into account the facts that MD5 accepts input of the arbitrary length, the output is "only" 128 bits, and the algorithm itself which behaves similarly to random functions, I would expect that the all outputs are possible. However, without any strong mathematical evidence, this is merely a speculation, or in the best case, the hunch.

Q: The answer probably is yes, for arbitrary x, and probably no proof exists.

Exactly, there is no proof (currently). Proving that MD5 is surjective would require either one of these:

• Brute force check - taking into account coupon's collector problem, it takes something like 10^40 inputs to hit all 128-bit outputs. Assuming again that MD5 behaves similarly to random function, that should be enough to verify in the case that MD5 is surjective.
• Concrete mathematical proof - eventual proof of surjectivity should be able for arbitrary 128-bit number y prove that there exists some input x such that MD5(x) = y. That should require deep mathematical analysis of MD5 function and it would probably shed some light on reversing this hash function. Even though MD5 is far from desirable hash function, it is still not known how to predict the input based on the given output.
• "the existence of [a proof] would probably require breaking the hash function" -- how so? Popular PRNGs are surjective and you don't need to "break" them in order to show that. (However can you break a hash function, anyway?) – Raphael Jun 3 '15 at 12:06
• @DavidRicherby You are aware that MD5 is far from this pathological example that you provided. More convincing example would be f(x)=2x, which at least takes x into account :) However, I got your point so I edited my answer. And I am curious, how would you represent 1000 with nine bits? – Miljen Mikic Jun 5 '15 at 7:37
• @Raphael - nice example with PRNGs, thanks. – Miljen Mikic Jun 5 '15 at 7:39
• "That should require deep mathematical analysis of MD5 function and it would probably shed some light on reversing this hash function." -- I still don't follow this train of thought. The simple hash function $x \mod n$ for $x$ a number encoding of your data is trivially surjective, assuming $x$ does not have a very weird domain. I don't see how this knowledge makes is worse as a hash function. (Security by obscurity is no security at all!) – Raphael Jun 5 '15 at 8:09
• Thanks for the edit. Nine bits? It's not computer science if there aren't off-by-one errors. *cough* – David Richerby Jun 5 '15 at 8:25
{"ft_lang_label":"__label__en","ft_lang_prob":0.927747,"math_prob":0.87548614,"size":631,"snap":"2019-43-2019-47","text_gpt3_token_len":162,"char_repetition_ratio":0.12599681,"word_repetition_ratio":0.0,"special_character_ratio":0.2646593,"punctuation_ratio":0.13380282,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9808499,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-10-20T08:39:47Z\",\"WARC-Record-ID\":\"<urn:uuid:95024a48-4ff4-4eca-b0ed-4c731ccef6f2>\",\"Content-Length\":\"146898\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b5d7de50-99ad-4584-a908-4f761c5ff0bf>\",\"WARC-Concurrent-To\":\"<urn:uuid:5dc88a80-481f-4b39-bbc0-b565bb0e18aa>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://cs.stackexchange.com/questions/43205/is-it-known-whether-the-md5-algorithm-is-surjective/43206\",\"WARC-Payload-Digest\":\"sha1:ZMY52OXUFPCSLRJX3YUHGAV5EAC3QLR6\",\"WARC-Block-Digest\":\"sha1:3AGGFK2FA5AN75NPCZFSNKM26URYJBI2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-43/CC-MAIN-2019-43_segments_1570986705411.60_warc_CC-MAIN-20191020081806-20191020105306-00551.warc.gz\"}"}