From 059e0d95d0c28bc5060e87e146eaf7411f51bf90 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Thu, 27 Apr 2023 03:47:30 +0300 Subject: skeleton commit; based on an anom project --- tests/compare/hashcost.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 tests/compare/hashcost.php (limited to 'tests/compare/hashcost.php') diff --git a/tests/compare/hashcost.php b/tests/compare/hashcost.php new file mode 100755 index 0000000..7a1401f --- /dev/null +++ b/tests/compare/hashcost.php @@ -0,0 +1,54 @@ +'; +echo "\nPassword Hash Cost Calculator\n\n"; +echo "Testing BCRYPT hashing the password '$password'\n\n"; +echo "We're going to run until the time to generate the hash takes longer than {$mSec}ms\n"; + +$cost = 3; +do { + $cost++; + echo "\nTesting cost value of $cost: "; + $time = benchmark($password, $cost); + echo "... took $time"; +} while ($time < ($mSec/1000)); + +echo "\n\nIdeal cost is $cost\n"; +echo "\nRunning 100 times to check the average:\n"; + +$start = microtime(true); +$times = []; +for ($i=1;$i<=100;$i++) { + echo "\r$i/100"; + $times[] = benchmark($password, $cost); +} + +echo "\n\ndone benchmarking in ".(microtime(true)-$start)."\n"; + +echo "\nSlowest time: ".max($times); +echo "\nFastest time: ".min($times); +echo "\nAverage time: ".(array_sum($times)/count($times)); + +echo "\n\nFinished\n"; +echo ""; + +function benchmark($password, $cost=4) +{ + $start = microtime(true); + password_hash($password, PASSWORD_BCRYPT, ['cost'=>$cost]); + return microtime(true) - $start; +} -- cgit v1.2.3