diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index c799654..c078749 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -10,6 +10,9 @@ 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 { @@ -17,15 +20,19 @@ class CreateUser { private String userName; private String password; private String birthday; + private String firstName; + private String surname; private boolean stayLoggedIn; // Constructor - public CreateUser(String id, String name, String password, String birthday) { + public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) { this.id = id; this.userName = name; - this.password = password; + this.password = hashPassword(password); this.birthday = birthday; + this.firstName = firstName; + this.surname = surname; } // Getters and Setters @@ -61,6 +68,22 @@ class CreateUser { this.birthday = birthday; } + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String surname() { + return firstName; + } + + public void surname(String firstName) { + this.firstName = firstName; + } + public boolean isStayLoggedIn() { return stayLoggedIn; } @@ -70,7 +93,7 @@ class CreateUser { } // Function to create user with validation - public static CreateUser createUser(String id, String userName, String password, String birthday) { + public static CreateUser createUser(String id, String userName, String password, String birthday, String firstName, String surname) { if (userName == null || userName.isEmpty()) { throw new IllegalArgumentException("Username cannot be empty"); } @@ -79,7 +102,25 @@ class CreateUser { } if (password.length() < 6) { throw new IllegalArgumentException("Password must be at least 6 characters long"); - } return new CreateUser(id, userName, password, birthday); + } return new CreateUser(id, userName, password, birthday, firstName, surname); + } + + // 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 @@ -135,7 +176,7 @@ class CreateUser { try { // Example usage UUID randomUUID = UUID.randomUUID(); - CreateUser user = createUser(randomUUID.toString(), "Test User", "TestPasswort123", "01.01.2000"); + CreateUser user = createUser(randomUUID.toString(), "Hash Test", "123456", "01.01.2000", "Hans", "Wurst"); // Example of accessing properties System.out.println("UserID: " + user.getId()); diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 675ffa5..6a442b7 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -5,15 +5,19 @@ import java.util.List; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + public class LoginGUI extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton; private JCheckBox stayLoggedInCheckbox; + private JButton signUpButton; public LoginGUI() { setTitle("Login"); - setSize(300, 180); + setSize(300, 220); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -42,6 +46,11 @@ public class LoginGUI extends JFrame implements ActionListener { loginButton.addActionListener(this); add(loginButton); + signUpButton = new JButton("Sign Up"); + signUpButton.setBounds(100, 140, 100, 25); // Adjusted position + signUpButton.addActionListener(this); + add(signUpButton); + getRootPane().setDefaultButton(loginButton); passwordField.addKeyListener(new EnterKeyListener()); @@ -51,6 +60,10 @@ public class LoginGUI extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); String username = usernameField.getText(); + // Set stayLoggedIn to false if the checkbox is unchecked + if (!stayLoggedInCheckbox.isSelected()) { + stayLoggedIn = false; + } updateStayLoggedIn(username, stayLoggedIn); } }); @@ -61,6 +74,10 @@ public class LoginGUI extends JFrame implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { login(); + }else if (e.getSource() == signUpButton) { + SignUpGUI signUpGUI = new SignUpGUI(); + signUpGUI.setVisible(true); + } } @@ -88,15 +105,38 @@ public class LoginGUI extends JFrame implements ActionListener { private boolean authenticateUser(String username, String password) { List userList = CreateUser.readUserListFromJsonFile("user.json"); if (userList != null) { - for (CreateUser user : userList) { - if (user.getUserName().equals(username) && user.getPassword().equals(password)) { - return true; //Success + for (CreateUser user : userList) { + if (user.getUserName().equals(username)) { + // Hash the user input password + String hashedPassword = hashPassword(password); + // Compare the hashed passwords + if (user.getPassword().equals(hashedPassword)) { + return true; // Success } } } + } return false; // Fail } + 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 { @Override diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index b19cf8e..2f31890 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -5,12 +5,12 @@ import java.util.List; import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { - private JTextField usernameField, passwordField, confirmPasswordField, birthdayField; + private JTextField usernameField, passwordField, confirmPasswordField, birthdayField, firstNameField, surnameField; private JButton signUpButton; - + public SignUpGUI() { setTitle("Sign Up"); - setSize(300, 250); + setSize(400, 300); // Adjusted size for accommodating more fields setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -19,7 +19,7 @@ public class SignUpGUI extends JFrame implements ActionListener { add(usernameLabel); usernameField = new JTextField(); - usernameField.setBounds(100, 20, 160, 25); + usernameField.setBounds(140, 20, 160, 25); add(usernameField); JLabel passwordLabel = new JLabel("Password:"); @@ -27,7 +27,7 @@ public class SignUpGUI extends JFrame implements ActionListener { add(passwordLabel); passwordField = new JPasswordField(); - passwordField.setBounds(100, 50, 160, 25); + passwordField.setBounds(140, 50, 160, 25); add(passwordField); JLabel confirmPasswordLabel = new JLabel("Confirm Password:"); @@ -35,22 +35,37 @@ public class SignUpGUI extends JFrame implements ActionListener { add(confirmPasswordLabel); confirmPasswordField = new JPasswordField(); - confirmPasswordField.setBounds(140, 80, 120, 25); + confirmPasswordField.setBounds(140, 80, 160, 25); add(confirmPasswordField); + JLabel firstNameLabel = new JLabel("First Name:"); // New field for first name + firstNameLabel.setBounds(20, 110, 80, 25); + add(firstNameLabel); + + firstNameField = new JTextField(); + firstNameField.setBounds(140, 110, 160, 25); + add(firstNameField); + + JLabel surnameLabel = new JLabel("Surname:"); // New field for surname + surnameLabel.setBounds(20, 140, 80, 25); + add(surnameLabel); + + surnameField = new JTextField(); + surnameField.setBounds(140, 140, 160, 25); + add(surnameField); + JLabel birthdayLabel = new JLabel("Birthday:"); - birthdayLabel.setBounds(20, 110, 80, 25); + birthdayLabel.setBounds(20, 170, 80, 25); add(birthdayLabel); birthdayField = new JTextField(); - birthdayField.setBounds(100, 110, 160, 25); + birthdayField.setBounds(140, 170, 160, 25); add(birthdayField); signUpButton = new JButton("Sign Up"); - signUpButton.setBounds(100, 150, 100, 25); + signUpButton.setBounds(140, 210, 100, 25); // Adjusted position signUpButton.addActionListener(this); add(signUpButton); - } @Override @@ -60,6 +75,8 @@ public class SignUpGUI extends JFrame implements ActionListener { String password = passwordField.getText(); String confirmPassword = confirmPasswordField.getText(); String birthday = birthdayField.getText(); + String firstName = firstNameField.getText(); + String surname = surnameField.getText(); if (!password.equals(confirmPassword)) { JOptionPane.showMessageDialog(this, "Passwords do not match!", "Sign Up Error", JOptionPane.ERROR_MESSAGE); @@ -72,7 +89,7 @@ public class SignUpGUI extends JFrame implements ActionListener { } try { UUID randomUUID = UUID.randomUUID(); - CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday); + CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday, firstName, surname); user.saveToJsonFile("user.json"); JOptionPane.showMessageDialog(this, "User signed up successfully!"); dispose(); diff --git a/user.json b/user.json index 5437c96..29df4e8 100644 --- a/user.json +++ b/user.json @@ -1,23 +1,20 @@ [ { - "id": "a2864d79-1079-4cbb-8d77-f5f84995580d", - "userName": "Another Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000", + "id": "d7ae19fe-4684-4d69-a73d-4cca612962a3", + "userName": "Test", + "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", + "birthday": "", + "firstName": "", + "surname": "", "stayLoggedIn": false }, { - "id": "3690702d-9c7e-48fb-8a01-ef89b3b76268", - "userName": "TestUser2", - "password": "123456", - "birthday": "01.01.2000", + "id": "2ec2c0c5-677c-4262-8958-fef98d11cc63", + "userName": "Test2", + "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92", + "birthday": "", + "firstName": "", + "surname": "", "stayLoggedIn": false - }, - { - "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", - "userName": "Test User", - "password": "Test", - "birthday": "01.01.2000", - "stayLoggedIn": true } ] \ No newline at end of file