MATLAB: Best method to check all possible combinations

I need a MATLAB Script to find 6 letter string of characters that contains only letters from @#!abcdefghijkzxyuvlwprsqmnot
such that hash(string) is
154310566
function h = hash(string)
h = 6;
letters = '@#!abcdefghijkzxyuvlwprsqmnot';
for i = 1:length(string)
h = (h * 17 + find(letters == (string(i))) - 1);
end
end

Best Answer

This seems to work in most cases:
function w = reversehash(h)
letters = '@#!abcdefghijkzxyuvlwprsqmnot';
numletters = floor(log(h)/log(17));
h = h - 6*17^numletters;
w = blanks(numletters);
for l = numletters-1:-1:0
lvalue = floor(h/17^l);
w(numletters - l) = letters(lvalue + 1);
h = h - lvalue*17^l;
end
end
eg:
>>reversehash(2956342116)
ans = wf!dife
>>hash(ans)
ans = 2956342116
>>hash('aaaaaa')
ans = 149351208
>>reversehash(ans)
'aaaaaa'
There are some cases where it fails (e.g. 'tttttt'), but it should be fairly easy to adjust (something like if lvalue > numel(letters) increase previous letter by one and readjust current hash)