edited posted due to solved problem
This is because when you reach the innermost loop for the first time, your array is already four characters. Now for all the permutations you will have four characters in the array. This is the reason it works only with four character hashed passwords. Only if the password was 'a' or 'aa' or 'aaa' would it work. I tried to make it work using four different arrays for four diff length of characters. I used four diff loops to permute over those four arrays.