Skip to content

Instantly share code, notes, and snippets.

@Indigo744
Last active June 18, 2020 13:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Indigo744/e92356282eb808b94d08d9cc6e37884c to your computer and use it in GitHub Desktop.
Save Indigo744/e92356282eb808b94d08d9cc6e37884c to your computer and use it in GitHub Desktop.
PHP Argon2 benchmark
<?php
/**
* Password Argon2 Hash Benchmark
* Argon2 is available since PHP 7.2
*
* Just upload this script to your server and run it, either through CLI or by calling it in your browser.
*
* See Argon2 specs https://password-hashing.net/argon2-specs.pdf chapter 9 Recommended Parameters
*/
// Upper time limit to check
$upperTimeLimit = 2500;
$threads = [1, 2, 4];
$time_cost_min = 1;
$time_cost_max = 8;
$memory_cost_min = 1 << 10; // 1 MB
$memory_cost_max = 1 << 18; // 256 MB
$password = 'this_is_just_a_long_string_to_test_on_U8WNZqmz8ZVBNiNTQR8r';
if (php_sapi_name() !== 'cli' ) echo "<pre>";
echo "\nPassword ARGON2 Benchmark";
echo "\nWill run until the upper limit of {$upperTimeLimit}ms is reached for each thread value";
echo "\n\nTimes are expressed in milliseconds.";
$start = microtime(true);
$hash = password_hash($password, PASSWORD_ARGON2I);
$time = round((microtime(true) - $start) * 1000);
echo "\n\n\nTime with default settings: {$time}ms";
echo "\nHash = $hash";
foreach($threads as $thread) {
echo "\n\n\n=Testing with $thread threads";
echo "\n m_cost (MB) ";
for ($m_cost = $memory_cost_min; $m_cost <= $memory_cost_max; $m_cost *= 2) {
$m_cost_mb = $m_cost / 1024;
echo '|' . str_pad($m_cost_mb, 5, ' ', STR_PAD_BOTH);
}
echo "\n =====================================================";
for ($time_cost = $time_cost_min; $time_cost <= $time_cost_max; $time_cost++) {
echo "\n t_cost=$time_cost ";
for ($m_cost = $memory_cost_min; $m_cost <= $memory_cost_max; $m_cost *= 2) {
$start = microtime(true);
password_hash($password, PASSWORD_ARGON2I, [
'memory_cost' => $m_cost,
'time_cost' => $time_cost,
'threads' => $thread,
]);
$time = round((microtime(true) - $start) * 1000);
if ($time < $upperTimeLimit) {
echo '|' . str_pad($time, 5, ' ', STR_PAD_BOTH);
} else {
echo '|' . str_pad(">LIM", 5, ' ', STR_PAD_BOTH);
$m_cost = $memory_cost_max;
$time_cost = $time_cost_max;
}
}
}
}
echo "\n\n";
if (php_sapi_name() !== 'cli' ) echo "</pre>";
@ncovercash
Copy link

Thought I'd let you know - the filename is misnamed, you spelled agron2 and not argon2!

@Indigo744
Copy link
Author

@smileytechguy thanks, fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment