Created
July 5, 2019 09:45
-
-
Save remicollet/9adb2f5bfe670f1fc02806ec7aec5376 to your computer and use it in GitHub Desktop.
passargon.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('TESTFILE', '/tmp/pass.json'); | |
function generate() { | |
$pass = ['secret', 'coucou', 'LexjfvSDK', 'password']; | |
for ($i=0 ; $i<10 ; $i++) { | |
$pass[] = bin2hex(random_bytes(10+$i)); | |
} | |
$res = []; | |
if (PASSWORD_BCRYPT == 1) { /* PHP 7.3 */ | |
$a = [PASSWORD_BCRYPT, PASSWORD_ARGON2I, PASSWORD_ARGON2ID]; | |
} else { | |
$a = [PASSWORD_BCRYPT, "argon2i", "argon2id"]; | |
} | |
foreach ($a as $algo) { | |
$res[$algo] = []; | |
foreach ($pass as $p) { | |
if (is_null($res[$algo][$p] = password_hash($p, $algo))) { | |
echo "F"; | |
} else { | |
echo "."; | |
} | |
} | |
echo "\n"; | |
} | |
file_put_contents(TESTFILE, json_encode($res, JSON_PRETTY_PRINT)); | |
} | |
function check() { | |
$in = json_decode(file_get_contents(TESTFILE), true); | |
foreach($in as $algo => $p) { | |
foreach($p as $pass => $hash) { | |
if (password_verify($pass, $hash)) { | |
echo "."; | |
} else { | |
echo "F"; | |
} | |
} | |
echo "\n"; | |
} | |
} | |
printf("PHP_VERSION: %s\n", PHP_VERSION); | |
if (defined('PASSWORD_ARGON2_PROVIDER')) { | |
printf("PASSWORD_ARGON2_PROVIDER: %s\n", PASSWORD_ARGON2_PROVIDER); | |
} | |
if ($_SERVER['argc'] < 2) { | |
die("missing parameter: gen or check\n"); | |
} else if ($_SERVER['argv'][1] == 'gen') { | |
generate(); | |
} else if ($_SERVER['argv'][1] != 'check') { | |
die("Bad parameter, need gen or check\n"); | |
} else if (file_exists(TESTFILE)) { | |
check(); | |
} else { | |
die("Missing test file\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment