Skip to content

Instantly share code, notes, and snippets.

@Nilpo
Created January 8, 2016 08:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nilpo/2cfd9847784ce84a9712 to your computer and use it in GitHub Desktop.
Save Nilpo/2cfd9847784ce84a9712 to your computer and use it in GitHub Desktop.
A simple PHP script for testing what cost you should use with your server for password_hash() in PHP 5.5.0+
<?php
/**
* This code will benchmark your server to determine how high of a cost you can
* afford. You want to set the highest cost that you can without slowing down
* you server too much. 8-10 is a good baseline, and more is good if your servers
* are fast enough. The code below aims for ≤ 50 milliseconds stretching time,
* which is a good baseline for systems handling interactive logins.
*/
function_exists('password_hash') or die("Please use PHP 5.5.0 or higher.");
$timeTarget = 0.5; // 50 milliseconds
$cost = 8;
do {
$cost++;
$start = microtime(true);
// as of PHP 5.4, you may use literal notation for arrays
//password_hash("test", PASSWORD_BCRYPT, ["cost" => $cost]);
password_hash("test", PASSWORD_BCRYPT, array("cost" => $cost));
$end = microtime(true);
} while (($end - $start) < $timeTarget);
echo "Appropriate Cost Found: " . $cost . "\n";
// EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment