我无法使用BCrypt的checkpw(plaintextpw,previoushash)方法获取明文密码和之前的哈希值.
在寄存器servlet中,我获取输入的密码,使用BCrypt的hashpw(密码,genSalt)方法对其进行散列并将其存储在db中.
在登录servlet中,我从db中获取该哈希值,并使用BCrypt的checkpw来查看它是否与输入的密码匹配.
它从不匹配.这在我的常规Java应用程序中工作正常,而不是在webapp中.没有其他人有这个问题所以我认为我一定是做错了:
//RegisterServlet
String pw_hash = BCrypt.hashpw(request.getParameter("password"), BCrypt.gensalt());
String loginInsertString = "insert into login (loname,lopassword,locustomerid)" +
" VALUES ('" + username + "','" + pw_hash + "','" + loginInsert + "');";
//LoginServlet
ResultSet rs = stmt.executeQuery("select lopassword from login where loname = '" +
loginName + "';");
while( rs.next()){
dbhash = rs.getString(1);
}
out.println(dbhash+"<br>");
if (BCrypt.checkpw(request.getParameter("password"), dbhash)) {
out.println("It matches");
}else{
out.println("It does not match");
}
Run Code Online (Sandbox Code Playgroud)
BCrypt API非常简单 - 这里
我不存储盐,因为BCrypt你认为不需要 - 所以我做错了什么?