an encryption function not retruning correctly

Hi buddies
I tried to make an md5 function more secure by iterating with a loop several times

function:
[php]
function pwhash($password,$iterations = 13)
{
$hash =md5($password);

for ($i = 0; $i < $iterations; ++$i)
{
	$hash = md5($hash.$password);
	return $hash;
}

}
[/php]

to call it:
[php]
echo pwhash(“mypassword”,9);
[/php]

the problem is no matter what number I put for the secord parameter the function return the same result.

any Idea?

I found your hash routine interesting. The md5($hash . $password) is a nice way to create a more complicted hash. I like that.

But, your code is quite messed up. First, you use a function and pass a number to it for the iterations, but, in the function you hard-code it to 13, so that argument is useless. Also, you only create one pass thru the for loop and then return the results. This for loop does not loop at all, so it is useless too. I suggest trying something more like this:
[php]
function pwhash($password,$iterations) // no hard coding, number of iterations is entered in the calling code
{
$hash =md5($password);
for ($i = 0; $i < $iterations; ++$i)
{
$hash = md5($hash . $password);
}
return $hash;
}
[/php]
I did not test this, but, it should work better this way. Good luck, let us know…

thank for the reply ErnieAlex,

that worked pretty well thanks you

you can mark this thread solved

it might be useless but sometimes i might just use the default value and i dont have include a iterations arguement

thank you very much for pointing me out the mistake.

this is what i ended up with at the end

[php]

<?php //iterations has default value that means when i call it the second argument is optional function pwhash($password,$iterations=13) { $hash =md5($password); for ($i = 0; $i < $iterations; ++$i) { $hash = md5($hash . $password); } return $hash; } //here i dont include the number of iterations it wont give me an error since the default value it set which is 13 echo pwhash("mypassword")."
"; ?>

[/php]

Glad I could help… Nice multi-encryption routine…

Sponsor our Newsletter | Privacy Policy | Terms of Service