Skip to content

Commit

Permalink
Use Bcrypt for password encoding/checking
Browse files Browse the repository at this point in the history
  • Loading branch information
2martens authored and KlausRicharz committed Jan 28, 2022
1 parent 3497519 commit 0796aaa
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 277 deletions.
24 changes: 3 additions & 21 deletions src/main/java/org/tb/mobile/LoginMobileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,10 @@ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServlet
boolean isValid = false;
String username = request.getParameter("username");
String password = request.getParameter("password");
Employee loginEmployee = employeeDAO.getLoginEmployee(username);
Employee employee = employeeDAO.getLoginEmployee(username);

boolean passwordMatches = loginEmployee != null && SecureHashUtils.passwordMatches(
password,
loginEmployee.getPassword()
);
if (!passwordMatches) {
boolean legacyPasswordMatches = loginEmployee != null && SecureHashUtils.legacyPasswordMatches(
password, loginEmployee.getPassword()
);
if (legacyPasswordMatches) {
// employee still has old password form
// store password again with new hashing algorithm
Employee em = employeeDAO.getEmployeeById(loginEmployee.getId());
em.changePassword(password);
loginEmployee.changePassword(password);
employeeDAO.save(em, loginEmployee);
passwordMatches = true;
}
}
if (loginEmployee != null && passwordMatches) {
long employeeId = loginEmployee.getId();
if (employee != null && SecureHashUtils.passwordMatches(password, employee.getPassword())) {
long employeeId = employee.getId();
isValid = true;
Date date = new Date();
Long employeecontractId = employeecontractDAO.getEmployeeContractByEmployeeIdAndDate(employeeId, date).getId();
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/tb/util/SecureHashUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.tb.exception.LogicException;

@Slf4j
public class SecureHashUtils {

private static final int COMPLEXITY = 10;

private SecureHashUtils() {}

public static String encodePassword(String password) {
PasswordEncoder encoder = new BCryptPasswordEncoder(COMPLEXITY);
return encoder.encode(password);
Expand All @@ -24,10 +23,6 @@ public static boolean passwordMatches(String enteredPassword, String hashedPassw
return encoder.matches(enteredPassword, hashedPassword);
}

public static boolean legacyPasswordMatches(String enteredPassword, String md5HashedPassword) {
return makeMD5(enteredPassword).equals(md5HashedPassword);
}

/**
* Makes a md5-hash for a given string.
*
Expand All @@ -43,8 +38,8 @@ public static String makeMD5(String text) {
md = MessageDigest.getInstance("MD5"); // getting a 'MD5-Instance'
encryptMsg = md.digest(text.getBytes()); // solving the MD5-Hash
} catch (NoSuchAlgorithmException e) {
log.error("MD5 not supported!", e);
throw new LogicException("MD5 not supported", e);
System.out.println("No Such Algorithm Exception!");
return "";
}

String swap = ""; // swap-string for the result
Expand Down
Loading

0 comments on commit 0796aaa

Please sign in to comment.