Created
November 6, 2014 12:10
-
-
Save richjenks/b5818d12045e3148b4b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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