Created
July 18, 2017 10:54
-
-
Save h3adshotzz/cf37e94e7621cee1a46bf5327f785a4b to your computer and use it in GitHub Desktop.
SHA1 dictionary calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.ArrayList; | |
import java.util.Formatter; | |
public class Main { | |
private static long startTime = 0; | |
private static long endTime = 0; | |
private static double totalTime = 0; | |
public static void main(String[] args) { | |
System.out.println("Hash File: "); | |
String hashFileLocation = input(); | |
System.out.println("Dictionary File: "); | |
String dictionaryFileLocation = input(); | |
System.out.println("Running SHAForce1..."); | |
System.out.println("=========================="); | |
File hashFile = new File(hashFileLocation); | |
String[] hashFileArray = loadFile(hashFile); | |
File dictionaryFile = new File(dictionaryFileLocation); | |
String[] dictionaryFileArray = loadFile(dictionaryFile); | |
String[][] matches = new String[2][hashFileArray.length]; | |
int counter = 0; | |
for (int i = 0; i < hashFileArray.length; i++) { | |
matches[0][i] = hashFileArray[i]; | |
} | |
startTime = System.currentTimeMillis(); | |
for (int i = 0; i < hashFileArray.length; i++) { | |
for (int k = 0; k < dictionaryFileArray.length; k++) { | |
if (getSHA1(dictionaryFileArray[k]).equals(hashFileArray[i])) { | |
System.out.println("[" + counter + "] " + getSHA1(dictionaryFileArray[k])); | |
matches[1][i] = dictionaryFileArray[k]; | |
} else { | |
System.out.println("[" + counter + "] " + getSHA1(dictionaryFileArray[k])); | |
} | |
counter++; | |
} | |
counter++; | |
} | |
endTime = System.currentTimeMillis(); | |
totalTime = endTime - startTime; | |
summary(matches, hashFileArray.length, dictionaryFileArray.length, counter); | |
} | |
private static void summary(String[][] matches, int hashAmount, int uniquehashesCalculated, int totalHashesCalculated) { | |
System.out.println("==== SUMMARY ===="); | |
int matchesCalculated = 0; | |
for (int i = 0; i < hashAmount; i++) { | |
if (matches[1][i] != null) { | |
System.out.println("[" + i + "] MATCH FOUND FOR HASH : " + matches[0][i] + " as : " + matches[1][i]); | |
matchesCalculated++; | |
} | |
} | |
System.out.println("SHAForce1, " + uniquehashesCalculated + " Unique Hashes calculated (" + totalHashesCalculated + " in total) in " + totalTime + "ms, " + matchesCalculated + " of which were cracked."); | |
} | |
private static String getSHA1(String data) { | |
String sha1 = null; | |
try { | |
MessageDigest crypt = MessageDigest.getInstance("SHA-1"); | |
crypt.reset(); | |
crypt.update(data.getBytes()); | |
sha1 = byteToHex(crypt.digest()); | |
} catch(NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
return sha1; | |
} | |
private static String byteToHex(final byte[] hash) { | |
Formatter formatter = new Formatter(); | |
for (byte b : hash) | |
{ | |
formatter.format("%02x", b); | |
} | |
String result = formatter.toString(); | |
formatter.close(); | |
return result; | |
} | |
private static String[] loadFile(File filePath) { | |
ArrayList<String> fileSize = new ArrayList<>(); | |
if (filePath.exists()) { | |
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { | |
String sCurrentLine; | |
while ((sCurrentLine = br.readLine()) != null) { | |
fileSize.add(sCurrentLine); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
System.out.println("File does not exist!"); | |
} | |
String[] data = new String[fileSize.size()]; | |
for (int i = 0; i < data.length; i++) { | |
data[i] = fileSize.get(i); | |
} | |
return data; | |
} | |
private static int getFileArraySize(File hashFile) { | |
ArrayList<String> fileSize = new ArrayList<>(); | |
if (hashFile.exists()) { | |
try (BufferedReader br = new BufferedReader(new FileReader(hashFile))) { | |
String sCurrentLine; | |
while ((sCurrentLine = br.readLine()) != null) { | |
fileSize.add(sCurrentLine); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
System.out.println("File not found"); | |
} | |
return fileSize.size(); | |
} | |
private static String input() { | |
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); | |
String input = null; | |
try { | |
input = bf.readLine(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return input; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment