Browse Source

refactoring: moved the password hashing into own class PasswordHasher.java

main
Richard Schmidt 11 months ago
parent
commit
85d6d2a0d3
  1. 23
      src/main/java/CreateUser.java
  2. 21
      src/main/java/PasswordHasher.java
  3. 4
      src/main/java/SignUpGUI.java
  4. 21
      user.json

23
src/main/java/CreateUser.java

@ -10,9 +10,6 @@ import java.util.List;
import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
class CreateUser {
@ -29,7 +26,7 @@ class CreateUser {
public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) {
this.id = id;
this.userName = name;
this.password = hashPassword(password);
this.password = PasswordHasher.hashPassword(password);;
this.birthday = birthday;
this.firstName = firstName;
this.surname = surname;
@ -115,24 +112,6 @@ class CreateUser {
}
}
// Function to hash the password using SHA-256 algorithm
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;
}
}
// Function to save to JSON file, replace with database call later
public void saveToJsonFile(String filename) {
List<CreateUser> userList = readUserListFromJsonFile(filename);

21
src/main/java/PasswordHasher.java

@ -0,0 +1,21 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class PasswordHasher {
public static 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;
}
}
}

4
src/main/java/SignUpGUI.java

@ -5,6 +5,10 @@ import java.util.List;
import java.util.UUID;
public class SignUpGUI extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField usernameField, passwordField, confirmPasswordField, birthdayField, firstNameField, surnameField;
private JButton signUpButton;

21
user.json

@ -1,20 +1 @@
[
{
"id": "e365e96f-6c6c-48e5-96e0-7b5e058e7198",
"userName": "existinguser",
"password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"birthday": "1990-01-01",
"firstName": "John",
"surname": "Doe",
"stayLoggedIn": false
},
{
"id": "61ef4208-9077-42c4-983f-f1e234f7e5f4",
"userName": "testUser",
"password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"birthday": "1990-01-01",
"firstName": "John",
"surname": "Doe",
"stayLoggedIn": false
}
]
[]
Loading…
Cancel
Save