Skip to content

Instantly share code, notes, and snippets.

@richjenks
Created November 6, 2014 12:10
Show Gist options
  • Save richjenks/b5818d12045e3148b4b8 to your computer and use it in GitHub Desktop.
Save richjenks/b5818d12045e3148b4b8 to your computer and use it in GitHub Desktop.
<?php
/**
* hash_cost_benchmark
*
* Benchmarks your server and selects an appropriate cost for hash_pasword
*
* @param float $target Maximum acceptable duration in seconds
* @param int $cost Initial cost
*
* @return int Highest cost that can be used to meet the target
*/
public static function hash_cost_benchmark($target = 0.5, $cost = 8) {
if (function_exists('password_hash')) {
do {
$cost++;
$start = microtime(true);
password_hash('test', PASSWORD_BCRYPT, ['cost' => $cost]);
$end = microtime(true);
} while (($end - $start) < $target);
return $cost;
} else {
return 'Requires PHP >5.5';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment