diff --git a/Answers/40230212013/Graphical Login Page/.vscode/settings.json b/Answers/40230212013/Graphical Login Page/.vscode/settings.json new file mode 100644 index 0000000..e112a70 --- /dev/null +++ b/Answers/40230212013/Graphical Login Page/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/Answers/40230212013/Graphical Login Page/README.md b/Answers/40230212013/Graphical Login Page/README.md new file mode 100644 index 0000000..7c03a53 --- /dev/null +++ b/Answers/40230212013/Graphical Login Page/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/Answers/40230212013/Graphical Login Page/bin/App.class b/Answers/40230212013/Graphical Login Page/bin/App.class new file mode 100644 index 0000000..4712ef4 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/bin/App.class differ diff --git a/Answers/40230212013/Graphical Login Page/bin/EmailValidator.class b/Answers/40230212013/Graphical Login Page/bin/EmailValidator.class new file mode 100644 index 0000000..42ec6a3 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/bin/EmailValidator.class differ diff --git a/Answers/40230212013/Graphical Login Page/bin/LoginGUI$1.class b/Answers/40230212013/Graphical Login Page/bin/LoginGUI$1.class new file mode 100644 index 0000000..8b837f5 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/bin/LoginGUI$1.class differ diff --git a/Answers/40230212013/Graphical Login Page/bin/LoginGUI$2.class b/Answers/40230212013/Graphical Login Page/bin/LoginGUI$2.class new file mode 100644 index 0000000..f20c781 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/bin/LoginGUI$2.class differ diff --git a/Answers/40230212013/Graphical Login Page/bin/LoginGUI.class b/Answers/40230212013/Graphical Login Page/bin/LoginGUI.class new file mode 100644 index 0000000..d212183 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/bin/LoginGUI.class differ diff --git a/Answers/40230212013/Graphical Login Page/bin/PasswordUtils.class b/Answers/40230212013/Graphical Login Page/bin/PasswordUtils.class new file mode 100644 index 0000000..537cb25 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/bin/PasswordUtils.class differ diff --git a/Answers/40230212013/Graphical Login Page/bin/User.class b/Answers/40230212013/Graphical Login Page/bin/User.class new file mode 100644 index 0000000..c29b22d Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/bin/User.class differ diff --git a/Answers/40230212013/Graphical Login Page/bin/UserStore.class b/Answers/40230212013/Graphical Login Page/bin/UserStore.class new file mode 100644 index 0000000..42c9c88 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/bin/UserStore.class differ diff --git a/Answers/40230212013/Graphical Login Page/src/App.class b/Answers/40230212013/Graphical Login Page/src/App.class new file mode 100644 index 0000000..2780705 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/src/App.class differ diff --git a/Answers/40230212013/Graphical Login Page/src/App.java b/Answers/40230212013/Graphical Login Page/src/App.java new file mode 100644 index 0000000..476fe42 --- /dev/null +++ b/Answers/40230212013/Graphical Login Page/src/App.java @@ -0,0 +1,6 @@ +public class App { + public static void main(String[] args) { + LoginGUI loginGUI = new LoginGUI(); + loginGUI.setVisible(true); + } +} diff --git a/Answers/40230212013/Graphical Login Page/src/EmailValidator.java b/Answers/40230212013/Graphical Login Page/src/EmailValidator.java new file mode 100644 index 0000000..1bdf807 --- /dev/null +++ b/Answers/40230212013/Graphical Login Page/src/EmailValidator.java @@ -0,0 +1,11 @@ +import java.util.regex.Pattern; + +public class EmailValidator { + private static final Pattern EMAIL_PATTERN = Pattern.compile( + "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$" + ); + + public static boolean isValid(String email) { + return EMAIL_PATTERN.matcher(email).matches(); + } +} \ No newline at end of file diff --git a/Answers/40230212013/Graphical Login Page/src/LoginGUI$1.class b/Answers/40230212013/Graphical Login Page/src/LoginGUI$1.class new file mode 100644 index 0000000..842ede3 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/src/LoginGUI$1.class differ diff --git a/Answers/40230212013/Graphical Login Page/src/LoginGUI$2.class b/Answers/40230212013/Graphical Login Page/src/LoginGUI$2.class new file mode 100644 index 0000000..1b47739 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/src/LoginGUI$2.class differ diff --git a/Answers/40230212013/Graphical Login Page/src/LoginGUI.class b/Answers/40230212013/Graphical Login Page/src/LoginGUI.class new file mode 100644 index 0000000..d0f2015 Binary files /dev/null and b/Answers/40230212013/Graphical Login Page/src/LoginGUI.class differ diff --git a/Answers/40230212013/Graphical Login Page/src/LoginGUI.java b/Answers/40230212013/Graphical Login Page/src/LoginGUI.java new file mode 100644 index 0000000..3e4b889 --- /dev/null +++ b/Answers/40230212013/Graphical Login Page/src/LoginGUI.java @@ -0,0 +1,85 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +public class LoginGUI extends JFrame{ + private JTextField usernameField; + private JPasswordField passwordField; + private JTextField emailField; + private JLabel message; + private UserStore userStore; + + public LoginGUI() + { + + setTitle("Login and Registration"); + setSize(400, 300); userStore = new UserStore(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLocationRelativeTo(null); + JPanel panel = new JPanel(new GridLayout(7, 1)); + usernameField = new JTextField(); + passwordField = new JPasswordField(); + emailField = new JTextField(); + message = new JLabel(); + JButton login = new JButton("Login"); + JButton register = new JButton("Register"); + + panel.add(new JLabel("Username:")); + panel.add(usernameField); + panel.add(new JLabel("Password:")); + panel.add(passwordField); + panel.add(new JLabel("Email:")); + panel.add(emailField); + panel.add(message); + panel.add(login); + panel.add(register); + add(panel); + + login.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) + { + String username = usernameField.getText(); + String password = new String(passwordField.getPassword()); + if(UserStore.login(username, password)) + { + message.setText("Login successful"); + } + else + { + message.setText("Invalid username or password."); + } + } + }); + register.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) + { + String username = usernameField.getText(); + String password = new String(passwordField.getPassword()); + String email = emailField.getText(); + if(!EmailValidator.isValid(email)) + { + message.setText("Invalid Email format!"); + return; + } + int passwordStrength = PasswordUtils.isStrong(password); + if (passwordStrength < 3) { + message.setText("Weak password. Strength level: " + passwordStrength); + return; + } + + User user = new User(username, password, email); + if(UserStore.register(user)) + { + message.setText("Register successful!"); + } + else + { + message.setText("User exists!"); + } + + } + }); + } +} diff --git a/Answers/40230212013/Graphical Login Page/src/PasswordUtils.java b/Answers/40230212013/Graphical Login Page/src/PasswordUtils.java new file mode 100644 index 0000000..19be82f --- /dev/null +++ b/Answers/40230212013/Graphical Login Page/src/PasswordUtils.java @@ -0,0 +1,37 @@ +import java.util.regex.Pattern; + +public class PasswordUtils { + private static final Pattern specialChar = Pattern.compile("[@\\-_.]"); + private static final Pattern Lowercase = Pattern.compile("[a-z]"); + private static final Pattern Uppercase = Pattern.compile("[A-Z]"); + private static final Pattern Digit = Pattern.compile("\\d"); + public static int isStrong(String password) + { + int strength = 0; + if(password.length()<= 8) + { + strength++; + } + boolean lower = Lowercase.matcher(password).find(); + boolean upper = Uppercase.matcher(password).find(); + boolean digit = Digit.matcher(password).find(); + boolean special = specialChar.matcher(password).find(); + if(lower || upper || digit) + { + strength++; + } + if(lower && upper) + { + strength++; + } + if(lower && upper && digit) + { + strength++; + } + if(special) + { + strength++; + } + return strength; + } +} diff --git a/Answers/40230212013/Graphical Login Page/src/User.java b/Answers/40230212013/Graphical Login Page/src/User.java new file mode 100644 index 0000000..2a8e4ed --- /dev/null +++ b/Answers/40230212013/Graphical Login Page/src/User.java @@ -0,0 +1,41 @@ +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class User { + private String username; + private String hashPassword; + private String email; + public User(String username, String password, String email) + { + this.username = username; + this.hashPassword = hashPassword(password); + this.email = email; + } + public String getUsername() { + return username; + } + public String getHashPassword() { + return hashPassword; + } + public String getEmail() { + return email; + } + public static String hashPassword(String password) + { + try + { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] hash = digest.digest(password.getBytes()); + StringBuilder str = new StringBuilder(); + for(byte b : hash) + { + str.append(String.format("%02x", b)); + } + return str.toString(); + } + catch(NoSuchAlgorithmException e) + { + throw new RuntimeException(e); + } +} +} diff --git a/Answers/40230212013/Graphical Login Page/src/UserStore.java b/Answers/40230212013/Graphical Login Page/src/UserStore.java new file mode 100644 index 0000000..ad89c47 --- /dev/null +++ b/Answers/40230212013/Graphical Login Page/src/UserStore.java @@ -0,0 +1,66 @@ +import java.util.HashMap; +import java.util.Map; +import java.io.*; +public class UserStore { + private static final String file = "users.txt"; + private static Map users = new HashMap<>(); + public UserStore() + { + loadUsers(); + } + public static boolean register(User user) + { + if(users.containsKey(user.getUsername())) + { + return false; + } + users.put(user.getUsername(), user); + saveUsers(); + return true; + } + public static boolean login(String username, String password) + { + User user = users.get(username); + if (user == null) + { + return false; + } + String hashPassword = User.hashPassword(password); + return hashPassword.equals(user.getHashPassword()); + } + private void loadUsers() + { + try(BufferedReader bufr = new BufferedReader(new FileReader(file))) + { + String line; + while ((line = bufr.readLine()) != null) + { + String[] parts = line.split(","); + if(parts.length == 3) + { + User user = new User(parts[0], parts[1], parts[2]); + users.put(user.getUsername(), user); + } + } + } + catch(IOException e) + { + e.printStackTrace(); + } + } + private static void saveUsers() + { + try(BufferedWriter bufw = new BufferedWriter(new FileWriter(file))) + { + for(User user : users.values()) + { + bufw.write(user.getUsername() + "," + user.getHashPassword() + "," + user.getEmail()); + bufw.newLine(); + } + } + catch(IOException e) + { + e.printStackTrace(); + } +} +} diff --git a/Answers/40230212013/Graphical Login Page/users.txt b/Answers/40230212013/Graphical Login Page/users.txt new file mode 100644 index 0000000..7c657b2 --- /dev/null +++ b/Answers/40230212013/Graphical Login Page/users.txt @@ -0,0 +1 @@ +navid,e9ff6b954d2c80dde0ac488c7962ae3604ff633770b130d8aea86b5c9482e5fe,navid@gmail.com