Browse Source

refactoring: removed the hashing function in LoginGUI and replaced it with the PasswordHasher class

main
Richard Schmidt 11 months ago
parent
commit
5e0110a8f1
  1. 2
      src/main/java/CreateUser.java
  2. 21
      src/main/java/LoginGUI.java
  3. 12
      user.json

2
src/main/java/CreateUser.java

@ -26,7 +26,7 @@ class CreateUser {
public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) { public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) {
this.id = id; this.id = id;
this.userName = name; this.userName = name;
this.password = PasswordHasher.hashPassword(password);;
this.password = PasswordHasher.hashPassword(password);
this.birthday = birthday; this.birthday = birthday;
this.firstName = firstName; this.firstName = firstName;
this.surname = surname; this.surname = surname;

21
src/main/java/LoginGUI.java

@ -123,7 +123,7 @@ public class LoginGUI extends JFrame implements ActionListener {
if (userList != null) { if (userList != null) {
for (CreateUser user : userList) { for (CreateUser user : userList) {
if (user.getUserName().equals(username)) { if (user.getUserName().equals(username)) {
String hashedPassword = hashPassword(password);
String hashedPassword = PasswordHasher.hashPassword(password);
if (user.getPassword().equals(hashedPassword)) { if (user.getPassword().equals(hashedPassword)) {
return true; return true;
} }
@ -133,25 +133,6 @@ public class LoginGUI extends JFrame implements ActionListener {
return false; return false;
} }
private String hashPassword(String password) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(password.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
private class EnterKeyListener implements KeyListener { private class EnterKeyListener implements KeyListener {
@Override @Override
public void keyTyped(KeyEvent e) {} public void keyTyped(KeyEvent e) {}

12
user.json

@ -1 +1,11 @@
[]
[
{
"id": "c99b1061-13b7-4fa8-b1ac-814e07db3ef4",
"userName": "Test",
"password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"birthday": "",
"firstName": "",
"surname": "",
"stayLoggedIn": false
}
]
Loading…
Cancel
Save