Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
18 changes: 18 additions & 0 deletions Answers/40230212013/Graphical Login Page/README.md
Original file line number Diff line number Diff line change
@@ -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).
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions Answers/40230212013/Graphical Login Page/src/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class App {
public static void main(String[] args) {
LoginGUI loginGUI = new LoginGUI();
loginGUI.setVisible(true);
}
}
11 changes: 11 additions & 0 deletions Answers/40230212013/Graphical Login Page/src/EmailValidator.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
85 changes: 85 additions & 0 deletions Answers/40230212013/Graphical Login Page/src/LoginGUI.java
Original file line number Diff line number Diff line change
@@ -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!");
}

}
});
}
}
37 changes: 37 additions & 0 deletions Answers/40230212013/Graphical Login Page/src/PasswordUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
41 changes: 41 additions & 0 deletions Answers/40230212013/Graphical Login Page/src/User.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
66 changes: 66 additions & 0 deletions Answers/40230212013/Graphical Login Page/src/UserStore.java
Original file line number Diff line number Diff line change
@@ -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<String, User> 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();
}
}
}
1 change: 1 addition & 0 deletions Answers/40230212013/Graphical Login Page/users.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
navid,e9ff6b954d2c80dde0ac488c7962ae3604ff633770b130d8aea86b5c9482e5fe,navid@gmail.com