How to compare hashed password from 2 servlets using jBCrypt

355 Views Asked by At

I'm having problems on how and where to compare the password that I hashed on my registration servlet and the one that i hashed on my Login. servlet.

Hoping you guys could help me. Thanks. login servlet:

String password = request.getParameter("pword");
String haspw = BCrypt.hashpw(password, salt);

/* if username and password match*/
UserIO io = new UserIO();

authenticate = io.LogAccount(username, haspw);
if (authenticate == true) {
    uri = "Homepage.jsp";
    session.setAttribute("active", username);
} else {
    uri = "/WEB-INF/jsp/error.jsp";
}
RequestDispatcher rd = request.getRequestDispatcher(uri);
rd.forward(request, response);
out.close();

UserIO

if (registered_name.isEmpty() ||registered_pass.isEmpty()) { //registered_name.equals(username) && registered_pass.equals(password)
    //uri = "Homepage.jsp";
    hasInfo = false;
} else if (registered_name.equals(username) && registered_pass.equals(password)) {
hasInfo = true;
}

Register Servlet

String pword = request.getParameter("pword");
    String hashed = BCrypt.hashpw(pword, salt);
1

There are 1 best solutions below

2
On

Use method checkpw:

BCrypt.checkpw(pword, salt);

This return a boolean in comparation.