feat: Guess password using hash table

This commit is contained in:
Nathan Woodburn 2023-10-24 14:29:41 +11:00
parent a42d90871e
commit 335ed0d893
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
3 changed files with 1000050 additions and 9 deletions

View File

@ -1,3 +1,4 @@
import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
@ -19,17 +20,24 @@ public class guesser {
boolean match = false;
int check=0;
while (!match) {
String guess = getRandomPassword(check);
check++;
String hashedGuess = hash.hashstring(guess);
if (hashedGuess.equals(hashed)) {
match = true;
System.out.println("Match!");
System.out.println("Password is: " + guess);
// Check hash table
try{
BufferedReader reader = new BufferedReader(new FileReader("hashtable.txt"));
String line;
int lineNumber = 0;
while ((line = reader.readLine()) != null) {
lineNumber++;
if (line.contains(hashed)) {
System.out.println("Match!");
System.out.println("Password is: " + line.split("\\$")[0]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.nanoTime();
long duration = (endTime - startTime);
// Calculate the time taken in seconds
@ -99,7 +107,7 @@ public class guesser {
}
private static String getRandomPassword(int value) {
public static String getRandomPassword(int value) {
// Define the characters that can be used in the random string
String characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

33
hashTable.java Normal file
View File

@ -0,0 +1,33 @@
import java.io.File;
import java.security.NoSuchAlgorithmException;
public class hashTable {
public static void main(String[] args) throws NoSuchAlgorithmException {
System.out.println("Hashing all 6 digit passwords");
// Save to hashtable.txt
File file = new File("hashtable.txt");
if (file.exists()) {
file.delete();
}
// Create a new file in the current directory
try {
file.createNewFile();
} catch (Exception e) {
System.out.println(e.toString());
}
// Write to the file
for (int i = 0; i < 1000000; i++) {
String randomPassword = guesser.getRandomPassword(i);
String hashed = hash.hashstring(randomPassword);
String line = randomPassword + "$" + hashed + "\n";
try {
java.nio.file.Files.write(java.nio.file.Paths.get("hashtable.txt"), line.getBytes(), java.nio.file.StandardOpenOption.APPEND);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
}

1000000
hashtable.txt Normal file

File diff suppressed because it is too large Load Diff