gpt4 book ai didi

java - jBCrypt 无法验证密码

转载 作者:行者123 更新时间:2023-12-01 15:43:29 25 4
gpt4 key购买 nike

我正在为我的应用程序加密密码,因为密码将存储在共享首选项中

我发现了 bcrypt 并阅读了很多关于它的好东西,但我无法让它工作

我正在使用jBCrypt 。我按照说明进行了测试

String hashed = BCrypt.hashpw("dog", BCrypt.gensalt(12));
String candidate = BCrypt.hashpw("dog", BCrypt.gensalt(12));
if (BCrypt.checkpw(candidate, hashed)){
Toast.makeText(Loader.this, "equals", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(Loader.this, "don't match?", Toast.LENGTH_LONG).show();
}

但是每次我运行应用程序时显示的 toast 都不匹配?因此,当我在共享首选项中记录哈希密码,然后将其与用户输入进行比较时,它说每次都会说错,因为显然它每次都会给我一个不同的哈希值,这是怎么回事,我该如何使用它?

最佳答案

根据documentationBCrypt.checkpw()明文密码作为其第一个参数。所以应该是:

String hashed = BCrypt.hashpw("dog", BCrypt.gensalt(12));
String candidate = "dog";

if (BCrypt.checkpw(candidate, hashed)) {
Toast.makeText(Loader.this, "equals", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Loader.this, "doesn't match?", Toast.LENGTH_LONG).show();
}

关于java - jBCrypt 无法验证密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7600395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com