In this program, I have been asked to read a an existing textfile (1324passlist) containing a list of passwords, then create a dictionary file with both the password and MD5 hash of the password on the same line. Have also allowed user to input a password hash, search the created dictionary file and print the corresponding password, if it exists. I'm not a programmer and it's not pretty, but it runs according to the assignment guidelines. I would like advice on where I could tidy up and write the code more efficiently.
public class main
{
public static void searchTextFile(String hash) throws IOException
{
File fileName = new File("dictionary.txt");
Scanner scan = new Scanner(fileName);
String line = scan.nextLine();
StringTokenizer st = new StringTokenizer(line, " ");
while(st.hasMoreTokens())
{
String word1 = st.nextToken();
String word2 = st.nextToken();
if(word2.equals(hash))
{
System.out.println(word1);
break;
}
if(!st.hasMoreTokens())
{
System.out.println("Hash not found.");
}
}
}
public static String getMd5(String pInput)
{
try
{
MessageDigest lDigest = MessageDigest.getInstance("MD5");
lDigest.update(pInput.getBytes());
BigInteger lHashInt = new BigInteger(1, lDigest.digest());
return String.format("%1$032X", lHashInt);
}
catch(NoSuchAlgorithmException lException)
{
throw new RuntimeException(lException);
}
}
public static void findPassword(String fileName, String hash)
{
try
{
Scanner scanner = new Scanner(new File(fileName));
List<String> pwAndHash = new ArrayList<String>();
while(scanner.hasNext())
{
pwAndHash.add(scanner.next());
}
try
{
String hash1 = hash.toUpperCase();
int hashIndex = pwAndHash.indexOf(hash1);
int passwordIndex = hashIndex - 1;
System.out.println(pwAndHash.get(passwordIndex));
}
catch(ArrayIndexOutOfBoundsException a)
{
System.out.println("The hash was not found.");
}
}
catch(FileNotFoundException e)
{
}
}
public static String hashPassword(String password)
{
String encrypted = "";
try
{
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] passwordBytes = password.getBytes();
digest.reset();
digest.update(passwordBytes);
byte[] message = digest.digest();
StringBuilder hexString = new StringBuilder();
for ( int i=0; i < message.length; i++)
{
hexString.append(Integer.toHexString(0xFF & message[ i ]));
}
encrypted = hexString.toString();
}
catch(Exception e)
{
}
return encrypted;
}
public static void main(String[]args)
{
File file = new File("1324passlist.txt");
Scanner userInput = new Scanner(System.in);
try
{
Scanner input = new Scanner(file);
while (input.hasNextLine())
{
try
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("dictionary.txt", true)));
String password = input.nextLine();
out.print(password);
out.flush();
String hashedPassword = getMd5(password);
out.print(" " + hashedPassword + " ");
out.close();
}
catch(IOException d)
{
}
}
}
catch (FileNotFoundException e)
{
}
System.out.println("Enter a hash to search for its corresponding password.");
String hashToSearch = userInput.next();
findPassword("dictionary.txt", hashToSearch);
try
{
searchTextFile(hashToSearch);
}
catch(IOException e)
{
}
}
}