Skip to content

Instantly share code, notes, and snippets.

@tyano
Last active May 29, 2019 21:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyano/8913662 to your computer and use it in GitHub Desktop.
Save tyano/8913662 to your computer and use it in GitHub Desktop.
MD5ハッシュ突き合わせ(Java版) - Clojure版と同じように修正し、Core i5/2.4GHzで550ms程度までパフォーマンスアップ。
package example.mdsample.java;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.lang.StringUtils;
public class App {
public static void main(String[] args) throws Exception {
for(int i = 0; i < 10; i++) {
App app = new App();
long start = System.currentTimeMillis();
System.out.println("result = " + app.findPassword("hoge", "4b364677946ccf79f841114e73ccaf4f"));
long end = System.currentTimeMillis();
System.out.println("Ellapsed Time: " + (end - start) + "ms");
}
}
private final MessageDigest digester;
public App() throws NoSuchAlgorithmException {
super();
this.digester = MessageDigest.getInstance("MD5");
}
private String hexDigest(String s) throws NoSuchAlgorithmException, UnsupportedEncodingException {
digester.reset();
digester.update(s.getBytes("UTF-8"));
byte[] digest = digester.digest();
return DatatypeConverter.printHexBinary(digest);
}
public static class Result {
public String password;
public String hash;
public Result(String password, String hash) {
this.password = password;
this.hash = hash;
}
@Override
public String toString() {
return "Result{" + "password=" + password + ", hash=" + hash + '}';
}
}
private Result findPassword(String salt, String pw) throws NoSuchAlgorithmException, UnsupportedEncodingException {
for (int i = 0; i < 1000000; i++) {
String format = StringUtils.leftPad(String.valueOf(i), 6, '0');
String hex = hexDigest(salt + "$" + format);
if (pw.toUpperCase().equals(hex)) {
return new Result(format, hex);
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment