我正在努力弄清楚如何使用php7.2中的新libsodium特性,但出于某种原因,我似乎无法让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自身的示例代码:
注意:我已经联系了作者有关示例代码,现在它已经修复了!所以下面的例子已经过时了。(2018-05-30)
(我所做的唯一调整就是$passwort = "test"和echo__s)
<?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
发布于 2018-05-29 12:11:33
你把$password和$storeInDatabase搞错了
它应该是:
if (sodium_crypto_pwhash_str_verify($storeInDatabase,$password)) {
从医生那里:
bool sodium_crypto_pwhash_str_verify ( string $hash , string $password )
https://stackoverflow.com/questions/50583992
复制相似问题