Я пытаюсь понять, как использовать новые функции libsodium в php7.2, но по какой-то причине мне кажется, что sodium_crypto_pwhash_str_verify() не работает должным образом.
Я решил использовать приведенный пример в их документации: https://paragonie.com/blog/2017/06/libsodium-quick-reference-quick-comparison-similar-functions-and-which-one-use#crypto-pwhash-sample-php
Пример кода из самого libsodium:
Note: I've contacted the author about the sample code and it has now been fixed! So the example below is outdated. (2018-05-30)
(единственная корректировка, которую я сделал, это $passwort = "test" и echos)
<?php
$password = "test"; // by me
/* This uses Argon2i with two numeric constants that correspond to
* the number of passes (OPSLIMIT) and the amount of memory to use
* (MEMLIMIT). A higher OPSLIMIT helps against CPU attacks, while a
* higher MEMLIMIT helps against GPU attacks.
*/
$storeInDatabase = sodium_crypto_pwhash_str(
$password,
SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
);
/* Once that's stored, you can just test against the hash like so: */
if (sodium_crypto_pwhash_str_verify($password, $storeInDatabase)) {
/* Logged in! */
echo "works!"; // by me
} else {
/* Incorrect password. */
echo "failed!"; // by me
}
Поскольку я хеширую и проверяю те же $password и $storeInDatabase, это всегда должно выводить "works!", но это не так.
Что я здесь делаю не так? К сожалению, на php.net пока нет полезной документации по функциям sodium.
Для справки:
http://php.net/manual/en/function.sodium-crypto-pwhash-str.php
http://php.net/manual/en/function.sodium-crypto-pwhash-str-verify.php






У вас $password и $storeInDatabase неправильно
Так должно быть:
if (sodium_crypto_pwhash_str_verify($storeInDatabase,$password)) {
Из документов:
bool sodium_crypto_pwhash_str_verify ( string $hash , string $password )
Вы правы - так оно и работает. Хотя технически это не я привожу неверные аргументы, а пример кода libsodium! Думаю, я отправлю им сообщение об этом :)