Skip to content

[FEATURE] Add Password Hashing Benchmarks #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
135 changes: 135 additions & 0 deletions password.bench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
// Save this file as password.bench.php in the same directory as bench.php

return [
'bcrypt' => function ($multiplier = 1, $count = 50) {
$password = 'MySecurePassword123!@#';
$count = (int)($count * $multiplier);
$costs = [4, 5, 7, 10, 12]; // Different cost values to test

foreach ($costs as $cost) {
$options = [
'cost' => $cost
];

$startTime = microtime(true);
for ($i = 0; $i < $count; $i++) {
password_hash($password, PASSWORD_BCRYPT, $options);
}
$endTime = microtime(true);
$duration = $endTime - $startTime;

extraStat("bcrypt(cost=$cost)", number_format($duration, 4) . ' s');
}

return 0;
},

'argon2' => function ($multiplier = 1, $count = 50) {
if (!defined('PASSWORD_ARGON2I')) {
return INF; // Skip if Argon2 is not available
}

$password = 'MySecurePassword123!@#';
$count = (int)($count * $multiplier);

// Test different memory and time costs
$configs = [
['memory_cost' => 512, 'time_cost' => 1],
['memory_cost' => 1024, 'time_cost' => 2],
['memory_cost' => 2048, 'time_cost' => 3],
];

foreach ($configs as $config) {
$options = [
'memory_cost' => $config['memory_cost'],
'time_cost' => $config['time_cost'],
'threads' => 2
];

$startTime = microtime(true);
for ($i = 0; $i < $count; $i++) {
password_hash($password, PASSWORD_ARGON2I, $options);
}
$endTime = microtime(true);
$duration = $endTime - $startTime;

extraStat(
"argon2(mem={$config['memory_cost']}KB,time={$config['time_cost']})",
number_format($duration, 4) . ' s'
);
}

return 0;
},

'argon2id' => function ($multiplier = 1, $count = 50) {
if (!defined('PASSWORD_ARGON2ID')) {
return INF; // Skip if Argon2id is not available
}

$password = 'MySecurePassword123!@#';
$count = (int)($count * $multiplier);

// Test different memory and time costs
$configs = [
['memory_cost' => 512, 'time_cost' => 1],
['memory_cost' => 1024, 'time_cost' => 2],
['memory_cost' => 2048, 'time_cost' => 3],
];

foreach ($configs as $config) {
$options = [
'memory_cost' => $config['memory_cost'],
'time_cost' => $config['time_cost'],
'threads' => 2
];

$startTime = microtime(true);
for ($i = 0; $i < $count; $i++) {
password_hash($password, PASSWORD_ARGON2ID, $options);
}
$endTime = microtime(true);
$duration = $endTime - $startTime;

extraStat(
"argon2id(mem={$config['memory_cost']}KB,time={$config['time_cost']})",
number_format($duration, 4) . ' s'
);
}

return 0;
},

'verify' => function ($multiplier = 1, $count = 100) {
$password = 'MySecurePassword123!@#';
$count = (int)($count * $multiplier);

// Create hashes using different algorithms
$hashes = [
'bcrypt' => password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]),
];

if (defined('PASSWORD_ARGON2I')) {
$hashes['argon2'] = password_hash($password, PASSWORD_ARGON2I);
}

if (defined('PASSWORD_ARGON2ID')) {
$hashes['argon2id'] = password_hash($password, PASSWORD_ARGON2ID);
}

// Benchmark verification
foreach ($hashes as $algo => $hash) {
$startTime = microtime(true);
for ($i = 0; $i < $count; $i++) {
password_verify($password, $hash);
}
$endTime = microtime(true);
$duration = $endTime - $startTime;

extraStat("verify($algo)", number_format($duration, 4) . ' s');
}

return 0;
}
];
Loading