Skip to content

Instantly share code, notes, and snippets.

@ozh
Created February 24, 2022 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozh/65a75392b7cb254131cc55afd28de99b to your computer and use it in GitHub Desktop.
Save ozh/65a75392b7cb254131cc55afd28de99b to your computer and use it in GitHub Desktop.
Blowfish hash cost : time estimates
<?php
// Some simple benchmark functions
function starttime() {
$r = explode( ' ', microtime() );
$r = $r[1] + $r[0];
return $r;
}
function endtime($starttime) {
$r = explode( ' ', microtime() );
$r = $r[1] + $r[0];
$r = round($r - $starttime,4);
return $r.' seconds'."\n";
}
// Let's hash with different costs and see the order of computational effort it makes
$password = "This is my p4ssw0rd !";
$start = starttime();
var_dump(password_hash($password, PASSWORD_BCRYPT, ['cost' => 4]));
echo endtime($start); // 0.0013 seconds
$start = starttime();
var_dump(password_hash($password, PASSWORD_BCRYPT)); // with default 10
echo endtime($start); // 0.0777 seconds
$start = starttime();
var_dump(password_hash($password, PASSWORD_BCRYPT, ['cost' => 11]));
echo endtime($start); // 0.1486 seconds
$start = starttime();
var_dump(password_hash($password, PASSWORD_BCRYPT, ['cost' => 15]));
echo endtime($start); // 2.3974 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment