首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php sodium_crypto_pwhash_str_verify()失败

php sodium_crypto_pwhash_str_verify()失败
EN

Stack Overflow用户
提问于 2018-05-29 12:05:57
回答 1查看 675关注 0票数 2

我正在努力弄清楚如何使用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)

代码语言:javascript
复制
<?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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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 )

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50583992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档