-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTester.java
More file actions
164 lines (119 loc) · 5.56 KB
/
Tester.java
File metadata and controls
164 lines (119 loc) · 5.56 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
// CSI2110/CSI2510 Assignment#4 2018
// Lucia Moura, Robert Laganiere
// This Tester class is to be used, and not be changed
// Its main program tests part 1 and part 2 reading the required files.
// For debugging you may temporarily comment out part 2
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Tester {
static PasswordCracker cracker; // methods from Password Cracker used in other methods
// reads common words file and returns a list with these words
static ArrayList<String> readCommonPasswords(String fileName) {
BufferedReader buffer = null;
FileReader reader = null;
ArrayList<String> words=new ArrayList<String>(10000);
try {
reader = new FileReader(fileName);
buffer = new BufferedReader(reader);
String plainPassword;
while ((plainPassword = buffer.readLine()) != null) {
words.add(plainPassword);
}
}
catch (Exception e) {
System.out.println("Something Wrong Reading "+fileName);
}
finally {
try {
if (buffer != null)
buffer.close();
if (reader != null)
reader.close();
} catch (IOException ex) {
}
}
return words;
}
// Input is a file with user account info (username with their encrypted passwords)
// and a database that has already been populated with common passwords and their variants
// This method prints for each account the original password (or<not found!>) if not
// in the database
static void findPasswords(String fileName, DatabaseInterface database) {
BufferedReader buffer = null;
FileReader reader = null;
try {
reader = new FileReader(fileName);
buffer = new BufferedReader(reader);
String currentLine;
String username, cryptic, secret;
while ((currentLine = buffer.readLine()) != null) {
// here we expect in each line: username encrypted_password
String[] arr = currentLine.split(" ");
if (arr.length!=2) System.out.println("Something Wrong in the Input!");
else {
username=arr[0]; // read username
cryptic=arr[1].toUpperCase(); // read password hexa for (Sha1) and capitalize to match our capitalized SHA1
System.out.print("user: "+String.format("%15s",username)+" passwd: ");
secret=cracker.crackPassword(cryptic, database); // **** call to your method crackPassword in
// **** class PasswordCracker
if (secret.equals("")){
System.out.println("<not found!>"); // *** crackPassword returns "" if not found
}
else System.out.println(secret);
}
}
}
catch (Exception e) {
e.printStackTrace();
e.printStackTrace();
System.out.println("Something Wrong in findPasswords");
}
}
public static void main(String[] args) {
System.out.print("Reading easy passwords from file commonPwd10K.txt\n");
ArrayList<String> commonPasswords = readCommonPasswords("commonPwd10K.txt");
System.out.print("Enter Accounts Filename (default is leakedAccounts.txt) > ");
Scanner scan = new Scanner(System.in);
String nextLine = scan.nextLine();
String leakedAccountsFile;
if (nextLine.equals("")) leakedAccountsFile=new String("leakedAccounts.txt");
else leakedAccountsFile=nextLine;
//common pass = commonPwd and and LeakedAccountsFile(Strin) = leaked accounts
cracker =new PasswordCracker();
System.out.println("\n***** Part 1: Testing StandardDatabase *****");
DatabaseStandard db1=new DatabaseStandard(); //**** testing your class DatabaseStandard
long time,afterTime;
time=System.nanoTime();
cracker.createDatabase(commonPasswords,db1); // **** call to your method createDatabase in
// **** class PasswordCracker
afterTime=System.nanoTime();
System.out.println("Standard Database Creation Time is "+(afterTime-time)/1000000.0
+" miliseconds\n " ); // grab stats about how long it took to created DatabaseStandard
System.out.println(db1.size() + "size of DB");
db1.printStatistics(); // grab stats from the class DatabaseStandard you programmed
System.out.print("\nPress any key to crack usernames in file:"+leakedAccountsFile+" > ");
scan.nextLine();
findPasswords(leakedAccountsFile, db1); // call our method above to crack and print the passwords
System.out.print("\nWant to test DatabaseMine? (y/n)>"); // want to test of part 2 ?
Scanner scan3 = new Scanner(System.in);
String ans = scan3.nextLine();
if (ans.equals("y") || ans.equals("Y")) {
System.out.println("\n***** Part 2: Testing DatabaseMine *****");
DatabaseMine db2=new DatabaseMine(917333); //**** testing your class DatabaseMine
time=System.nanoTime();
cracker.createDatabase(commonPasswords,db2);// **** call to your method createDatabase in
// **** class PasswordCracker
afterTime=System.nanoTime();
System.out.println("DatabaseMine Creation Time is "+(afterTime-time)/1000000.0
+" miliseconds\n"); // grab stats about how long it took to created DatabaseMine
db2.printStatistics(); // grab stats about the class DatabaseMine you programmed
System.out.print("\nPress any key to crack usernames in file:"+leakedAccountsFile+" > ");
nextLine = scan.nextLine();
findPasswords(leakedAccountsFile, db2); // call our method above to crack and print the passwords
}
System.out.println("End of Program.");
}
}