【问题标题】:jBCrypt fails to verify passwordjBCrypt 无法验证密码
【发布时间】:2011-11-27 20:40:22
【问题描述】:

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

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

【问题讨论】:

    标签: java android security passwords bcrypt


    【解决方案1】:

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

    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();
    }
    

    【讨论】:

    • 我觉得自己像个白痴 :) 抱歉,谢谢你的回答哈
    猜你喜欢
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多