jBCrypt无法验证密码

时间:2011-09-29 16:26:04

标签: java android security passwords bcrypt

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

我找到了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都不匹配?因此,当我在我的共享首选项中记录散列密码然后将其与用户输入进行比较时,它表示每次都会说错误,因为显然它每次都给我一个不同的哈希,我怎么能用它?

1 个答案:

答案 0 :(得分:1)

根据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();
}
相关问题