I believe Thomas Pornin's answer is by far superior to mine, but perhaps this answer can provide a simplification to his answer.
When you initially hash some data, the possible input is infinite/limitless. You could input "abcdefghi...", "123456...", etcetera. However, the resulting hash possibilities are finite/limited.
One of the beautiful things about most hashing functions is that their output is always a fixed length. In the case of MD5, the output is always 128-bits. This is great for handling the output programmatically and storing the hash output in a file/database, but this means there is a finite limit on the number of possible outputs. For MD5, the limit is $2^{128}$ (which is 340,282,366,920,938,463,463,374,607,431,768,211,456
). This might seem so high that it would never be reached, but we are dealing with theoretically hashing an infinite number of times.
Because there are only $2^{128}$ possible outputs for md5("Anything")
, the input of all subsequent rounds is finite and most hash functions will produce a collision by 2^(<Number Bits in Output>/2)
. ($2^{64}$ for MD5.)
Consider this code:
String nextInput = "Some initial text here";
while(true){ // Infinite loop
nextInput = md5(nextInput);
}
Because of collisions, we can expect that this will eventually enter a pattern where $round_{1}$, $round_{2}$, $round_{3}$, ... $round_{n}$ will result in $round_{n}$'s output being equal to $round_{1}$. So, the output will eventually enter a circle-like state where initialRoundOutput == currentRoundOutput
and thus the circle/pattern/cycle will repeat infinitely.
As far as a fixed point where md5(md5(<some data>)) == md5(<some data>) == <some data>
, I'll primarily leave that up to Thomas Pornin's answer to explain, but hash functions are designed so the input data is not the same as the output. However, it is theoretically possible that such an input exists. See this StackOverflow answer for a good explanation of that.