Collision on self made hashing function
(Actually, this is about second-preimage resistance, not collision resistance)


  I expected the last two ones because they exceed the range of the hash-length. The first one however was unexpected and I simply cannot explain it. Is it because of bad chance or is it because of the function?


Actually, even for a good hash function, that has a relatively good chance at happening.

Let us assume that our hash function $F$ with an $n$-bit output is "good", that is, it acts like a totally random function.

Then, we evaluate $F(1)$ and get a specific value $z$.

Then, we evaluate $F(x)$ for $1 &lt; x &lt; 2^n$, and see if $z$ happens to occur a second time.

As $F(x)$ is random, then for any particular $x$, it will happen to be the value $z$ with probability $2^{-n}$; that is, it will be a value other than $z$ with probability $1 - 2^{-n}$

So, for $F(x)$ never to be $z$ for any of those $2^n - 2$ values of $x$, each probability is independent, and so the total probability is the product of the individual probabilities, namely, $(1 - 2^{-n})^{2^n-2} \approx 1/e \approx 0.368$ (where $e$ is Euler's constant 2.718281828459...)

That is, there is an about 63% chance of $F(1)$ appearing at least once somewhere else, even if you limit the range of the input to the hash-length.