- microtime: documentation ( source)
- crypt: documentation ( source)
- range: documentation ( source)
- str_pad: documentation ( source)
<?php
function bcryptWithCost($cost) {
$cost = str_pad($cost, 2, '0', STR_PAD_LEFT);
crypt('password', '$2y$' . $cost . '$thisisasaltthisisasalt');
}
$costs = range(12, 13);
foreach ($costs as $cost) {
$timestamp = microtime(true);
bcryptWithCost($cost);
$time = microtime(true) - $timestamp;
echo $cost . ' ' . $time . PHP_EOL;
if (!isset($recommended) && $time >= 0.5) {
$recommended = $cost;
}
if (isset($recommended) && $time < 0.9) {
$recommended = $cost;
}
}
echo "recommended cost param for this environment: $recommended\n";