-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPasswordMain.java
More file actions
226 lines (197 loc) · 8.15 KB
/
PasswordMain.java
File metadata and controls
226 lines (197 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import java.text.*;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class PasswordMain extends BorderPane
{
private Label passwordLabel, passwordALabel, instruction1Label, instruction2Label,instruction3Label,instruction4Label;
private Label instruction5Label, instruction6Label;
//private PasswordField passwordText, passwordAText ;
private TextField passwordText, passwordAText ;
private TextField passwordText0, passwordAText0 ;
private ArrayList<String> illegalPasswords;
private Button checkPwdButton, exitButton, checkPwdsInFileButton;
DecimalFormat format = new DecimalFormat("#0.000");
private Alert alert = new Alert(AlertType.INFORMATION);
//PasswordCheckerUtility pwdChecker;
public PasswordMain()
{
//pwdChecker = new PasswordCheckerUtility();
illegalPasswords = new ArrayList<String>();
VBox subpanel = new VBox();
instruction1Label = new Label("Use the following rules when creating your passwords:");
instruction2Label = new Label("\t1. Length must be greater than 6; a strong password will contain at least 10 characters");
instruction3Label = new Label("\t2. Must contain at least one upper case alpha character");
instruction4Label = new Label("\t3. Must contain at least one lower case alpha character");
instruction5Label = new Label("\t4. Must contain at least one numeric charcter");
instruction6Label = new Label("\t5. May not have more than 2 of the same character in sequence");
VBox.setMargin(instruction1Label, new Insets(2,10,2,10));
VBox.setMargin(instruction2Label, new Insets(2,10,2,10));
VBox.setMargin(instruction3Label, new Insets(2,10,2,10));
VBox.setMargin(instruction4Label, new Insets(2,10,2,10));
VBox.setMargin(instruction5Label, new Insets(2,10,2,10));
VBox.setMargin(instruction6Label, new Insets(2,10,2,10));
subpanel.setAlignment(Pos.CENTER_LEFT);
subpanel.getChildren().addAll(instruction1Label, instruction2Label, instruction3Label,
instruction4Label, instruction5Label, instruction6Label);
HBox subpanel1a = new HBox();
passwordLabel = new Label ("Password");
//passwordText = new PasswordField();
passwordText = new TextField();
passwordText0 = new TextField();
HBox.setMargin(passwordLabel, new Insets(10,10,10,10));
HBox.setMargin(passwordText, new Insets(10,10,10,10));
subpanel1a.setAlignment(Pos.CENTER);
subpanel1a.getChildren().addAll(passwordLabel, passwordText);
HBox subpanel1b = new HBox();
passwordALabel = new Label ("Re-type\nPassword");
//passwordAText = new PasswordField();
passwordAText = new TextField();
passwordAText0 = new TextField();
HBox.setMargin(passwordALabel, new Insets(10,10,10,10));
HBox.setMargin(passwordAText, new Insets(10,10,10,10));
subpanel1b.setAlignment(Pos.CENTER);
subpanel1b.getChildren().addAll(passwordALabel, passwordAText);
VBox subpanel1 = new VBox();
VBox.setMargin(subpanel1a, new Insets(10,10,10,10));
VBox.setMargin(subpanel1b, new Insets(10,10,10,10));
subpanel1.setAlignment(Pos.CENTER);
subpanel1.getChildren().addAll(subpanel1a, subpanel1b);
checkPwdsInFileButton = new Button("Check Passwords in _File");
checkPwdsInFileButton.setMnemonicParsing(true);
checkPwdsInFileButton.setTooltip(new Tooltip("Select to read passwords from a file"));
checkPwdsInFileButton.setOnAction(
event -> {
try {
readFile();
} catch (Exception e) {
e.printStackTrace();
}
});
checkPwdButton = new Button ("Check _Password");
checkPwdButton.setMnemonicParsing(true);
checkPwdButton.setTooltip(new Tooltip("Select to check a password."));
checkPwdButton.setOnAction(
event -> {
addPassword();
});
exitButton = new Button("E_xit");
//_ in label specifies that the next character is the mnemonic, ie, type Alt-e as a shortcut
//this activates the mnemonic on exitButton when the Alt key is pressed
exitButton.setMnemonicParsing(true);
exitButton.setTooltip(new Tooltip("Select to close the application"));
//use a lambda expression for the EventHandler class for exitButton
exitButton.setOnAction(
event -> {
Platform.exit();
System.exit(0);
}
);
HBox buttonPanel = new HBox();
HBox.setMargin(checkPwdButton, new Insets(10,10,10,10));
HBox.setMargin(checkPwdsInFileButton, new Insets(10,10,10,10));
HBox.setMargin(exitButton, new Insets(10,10,10,10));
buttonPanel.setAlignment(Pos.CENTER);
buttonPanel.getChildren().addAll(checkPwdButton, checkPwdsInFileButton, exitButton);
//panelContainer = new
setTop(subpanel);
setCenter(subpanel1);
setBottom(buttonPanel);
}
public void addPassword() {
//Get information
String passwordString = passwordText.getText();
String passwordAString = passwordAText.getText();
try
{
if (passwordString.compareTo(passwordAString)!=0)
throw new UnmatchedException();
if (PasswordCheckerUtility.isValidPassword(passwordString)) {
if (PasswordCheckerUtility.isWeakPassword(passwordString)) {
alert.setContentText("Password is OK but weak");
alert.showAndWait();
//JOptionPane.showMessageDialog(null, "Password is OK but weak", "Password Status", JOptionPane.PLAIN_MESSAGE);
}
else {
alert.setContentText("Password is valid");
alert.showAndWait();
//JOptionPane.showMessageDialog(null, "Password is valid", "Password Status", JOptionPane.PLAIN_MESSAGE);
}
}
}
catch (UnmatchedException ex)
{
alert.setContentText(ex.getMessage());
alert.showAndWait();
//JOptionPane.showMessageDialog(null, ex.getMessage(), "Password Status", JOptionPane.PLAIN_MESSAGE);
}
catch (Exception ex)
{
alert.setContentText(ex.getMessage());
alert.showAndWait();
//JOptionPane.showMessageDialog(null, ex.getMessage(), "Password Error", JOptionPane.PLAIN_MESSAGE);
}
}
//Listens for the Display Apps button to be pressed
private class displayIllegalPasswords implements ActionListener
{
public void actionPerformed (ActionEvent theEvent)
{
String result = "";
for(String temp:illegalPasswords)
result += temp + "\n";
alert.setContentText("Illegal Passwords\n"+result);
alert.showAndWait();
//JOptionPane.showMessageDialog(null, result, "Illegal Passwords", JOptionPane.PLAIN_MESSAGE);
}
}
public void readFile() {
FileChooser chooser = new FileChooser();
Scanner input;
File selectedFile = chooser.showOpenDialog(null);
if(selectedFile != null)
{
ArrayList<String> passwords = new ArrayList<String>();
String results = "";
try{
input = new Scanner(selectedFile);
while(input.hasNext())
{
passwords.add(input.next());
}
illegalPasswords = PasswordCheckerUtility.isInvalidPasswords(passwords);
//should be getInvalidPasswords(...);
for(String passwordString : illegalPasswords)
results += passwordString +"\n";
alert.setContentText("Illegal Passwords\n"+results);
alert.showAndWait();
//JOptionPane.showMessageDialog(null, results, "Illegal Passwords", JOptionPane.PLAIN_MESSAGE);
}// end of try
catch(FileNotFoundException ex)
{
alert.setContentText("File Error\n" + ex.toString());
alert.showAndWait();
//JOptionPane.showMessageDialog(null, ex.toString(), "File Error", JOptionPane.PLAIN_MESSAGE);
ex.printStackTrace();
}
}
}
}