Skip to content

Instantly share code, notes, and snippets.

@phillipsharring
Last active August 29, 2015 14:08
Show Gist options
  • Save phillipsharring/c0dd025b31d40e0d145b to your computer and use it in GitHub Desktop.
Save phillipsharring/c0dd025b31d40e0d145b to your computer and use it in GitHub Desktop.
Bcrypt Passwords with PHP 5.5
password_hash($password, $algorithm, $options = []);
$password = 'H@X04!';
$hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => $cost]);
// 100 milliseconds
// change this to your target time
$timeTarget = 0.1;
$cost = 8;
do {
$cost++;
$start = microtime(true);
password_hash('test', PASSWORD_BCRYPT, ['cost' => $cost]);
$end = microtime(true);
} while (($end - $start) < $timeTarget);
echo 'Appropriate Cost Found: ' . $cost . PHP_EOL;
// true, hooray!
password_verify($password, $hash);
// false
password_verify('not the password', $hash);
// false
password_verify($password, 'not the hash');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment