In general, you do not want to try and roll your own encryption since there are many excellent algorithms out there and an algorithm that is home brewed is more likely to have bugs or weaknesses.
In your particular case the two major weaknesses are taking a substring and using md5.
Substring
Taking a substring of the md5 hash reduces the possible hashes produced by your algorithm which increases the chance of a collision. Take the extreme case where we only use the first character of the md5 hash. We now have a situation where any password has a 1 in 16 chance of having the same hash as any other password! Worse brute forcing this would take very little time.
Your example uses a length of 5 but this is still very easy to brute force and has a high chance of collisions. Here's output from a script that creates 1,000,000 hashes (using uniqid
to generate the values):
Number of hashes created 1000000
Number of unique hashes 481247
Collisions 518753
MD5
MD5 is fast. For some applications this can be a good thing - say checking files for exact duplicates. However for passwords this means that it's also fast for an attacker to check lots of passwords. In addition there are a number of vulnerabilities in MD5.
Alternatives
PHP 5.5 provides a password api that simplifies generating passwords and looks easy to use (admittedly I haven't used it myself). This also takes care of choosing a strong algorithm. http://www.php.net/manual/en/book.password.php
This question has plenty of options for other PHP versions.