Created
June 9, 2010 14:58
-
-
Save sebjacobs/431592 to your computer and use it in GitHub Desktop.
This file contains 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.FileReader; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.security.*; | |
import java.io.PrintWriter; | |
public class BreakDigest{ | |
public static void main(String [] args) throws Exception{ | |
if(args.length>0){ | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
BufferedReader br; | |
File digestFile = new File("digest.txt"); | |
String currentLine=""; | |
if(!digestFile.exists()){ //if digestfile doesn't exist, create it from dict.txt | |
br = new BufferedReader(new FileReader("dict.txt")); | |
PrintWriter digestWriter =new PrintWriter(new FileWriter("digest.txt")); | |
byte [] lineArray; | |
byte [] lineDigest = new byte[32]; | |
String digestedLine=""; | |
while((currentLine=br.readLine())!=null){ | |
lineArray=currentLine.getBytes(); | |
lineDigest=md.digest(lineArray); | |
StringBuffer lineBuffer = new StringBuffer(); | |
String hexchar; | |
for (int i=0; i < lineDigest.length; i++) { //convert digest to hex | |
hexchar = Integer.toHexString((int)(lineDigest[i] & 0xFF)); | |
if (hexchar.length() == 1) hexchar = "0" + hexchar; | |
lineBuffer.append(hexchar); | |
} | |
digestedLine=lineBuffer.toString(); | |
digestWriter.println(currentLine+" "+digestedLine); //output digest paired with plaintext to digest.txt | |
} | |
digestWriter.close(); | |
currentLine=""; | |
} | |
br = new BufferedReader(new FileReader(digestFile)); | |
String [] lineSplit; | |
boolean matchFound=false; | |
while((currentLine=br.readLine())!=null){ | |
lineSplit =currentLine.split(" "); | |
if(md.isEqual(args[0].getBytes(),lineSplit[1].getBytes())){ //check inputted digest against digest.text | |
System.out.println(lineSplit[0]); | |
matchFound=true; | |
break; | |
} | |
} | |
if(!matchFound){ | |
System.out.println("no matches found"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment