Код: Выделить всё
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user_name = request.getParameter("name");
String email = request.getParameter("email");
// encrypt password
String pass = request.getParameter("pass");
byte[] salt = new byte[16]; // Use a cryptographically secure random number generator to generate the salt
new SecureRandom().nextBytes(salt);
// Hash the password with bcrypt
String hashedPassword = OpenBSDBCrypt.generate(pass.toCharArray(), salt, 12);
String mobile = request.getParameter("contact");
RequestDispatcher dispatcher = null;
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/LOGIN?useSSL=false", "username", "password");
PreparedStatement pst = con.prepareStatement("insert into users(user_name, salt, hashed_pass, email, mobile) values(?,?,?,?,?) ");
pst.setString(1, user_name);
pst.setBytes(2, salt);
pst.setString(3, hashedPassword);
pst.setString(4, email);
pst.setString(5, mobile);
}
...
Код: Выделить всё
create table users (
id int primary key auto_increment,
user_name varchar(50),
salt varbinary(16),
hashed_pass varchar(60),
email varchar(50),
mobile varchar(20)
);
Код: Выделить всё
pass
Код: Выделить всё
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("username");
String pass = request.getParameter("password");
HttpSession session = request.getSession();
RequestDispatcher dispatcher = null;
Connection con = null;
PreparedStatement pst = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/LOGIN?useSSL=false", "username", "password");
String query = "select * from users where email = ? and pass = ?";
pst = con.prepareStatement(query);
pst.setString(1, email);
pst.setString(2, pass);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
session.setAttribute("name", rs.getString("user_name"));
dispatcher = request.getRequestDispatcher("index.jsp");
} else {
request.setAttribute("status", "failed");
dispatcher = request.getRequestDispatcher("login.jsp");
}
dispatcher.forward(request, response);
rs.close();
}
....
Подробнее здесь: https://stackoverflow.com/questions/782 ... -salt-were