diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 31b1749..01085c9 100644 --- a/src/main/java/CreateUser.java +++ b/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 userList = readUserListFromJsonFile(filename); diff --git a/src/main/java/PasswordHasher.java b/src/main/java/PasswordHasher.java new file mode 100644 index 0000000..14a1137 --- /dev/null +++ b/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; + } + } +} \ No newline at end of file diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index faa360d..b9f7b1c 100644 --- a/src/main/java/SignUpGUI.java +++ b/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; diff --git a/user.json b/user.json index cfc325c..0637a08 100644 --- a/user.json +++ b/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 - } -] \ No newline at end of file +[] \ No newline at end of file