1
我有一個單詞表,其中來自20級的〜68000個單詞,位於0xf.at處,並且想要散列兩個單詞以獲得散列。然後將該散列與現有散列進行比較,直到找到兩個單詞。MD5哈希更快
我已經在java中嘗試過了,但我沒有意識到它是慢的。
import java.io.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException,
IOException {
try (BufferedReader br = new BufferedReader(new FileReader("E:/Trojan/Desktop/wordlist.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
Hash h = new Hash();
String myHash = "cd48323bcf01557f5deadc2ec301affb";
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
String lines[] = everything.split("\\r?\\n");
for (int j = 1; j <= 68848; j++) {
for (int i = 1; i <= 68847; i++) {
//System.out.println(i+":"+lines[i]+" "+j+":"+lines[j]);
if (h.getHash(lines[i]+lines[j], "MD5") == myHash){
System.out.println(lines[i]+lines[j]);
break;
}
}
}
}
}
}
和MD5哈希函數我從計算器的〔實施例了:
public class Hash {
/**
*
* @param txt, text in plain format
* @param hashType MD5 OR SHA1
* @return hash in hashType
*/
public static String getHash(String txt, String hashType) {
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance(hashType);
byte[] array = md.digest(txt.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
}
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
//error action
}
return null;
}
public static String md5(String txt) {
return Hash.getHash(txt, "MD5");
}
public static String sha1(String txt) {
return Hash.getHash(txt, "SHA1");
}
}
我怎樣才能得到這個更快?
爲什麼不使用java.security.MessageDigest?這些可能是優化的。 – mszymborski
第一個問題 - 你正在比較字符串引用和'==',所以它不會匹配。 –