d1rk
5/14/2012 - 9:21 AM

blowfish hashing using PHP's crypt and mcrypt library

blowfish hashing using PHP's crypt and mcrypt library

<?php

# From my question and answer at SO:
# http://stackoverflow.com/questions/10183103/security-of-generating-hash-salts-using-phps-mt-rand

function blowfish($string, $salt = NULL, $iterations = '12')
{
	return crypt($string, $salt ?: "$2a\$$iterations$" . md5(mcrypt_create_iv(22, MCRYPT_DEV_URANDOM)));
}

$password = 'password';
$hash = blowfish($password);

if($hash == blowfish($password, $hash))
{
	print "Matches\n";
	print $hash . "\n";
}




print "Running a hashing loop...\n";

$time = microtime(TRUE);

for ($i = 0; $i < 100; $i++)
{
	blowfish('password' + $i);
}

print (microtime(TRUE) - $time) . " ms\n\n";