0

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

我找到了 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();
}

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

4

1 回答 1

1

根据文档BCrypt.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();
}
于 2011-09-29T16:30:13.580 回答