Skip to content

Instantly share code, notes, and snippets.

@d1rk
Forked from xeoncross/blowfish.php
Created May 14, 2012 09:21
Show Gist options
  • Save d1rk/2692915 to your computer and use it in GitHub Desktop.
Save d1rk/2692915 to your computer and use it in GitHub Desktop.
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";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment