Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.rafaskb.hasher;
- import java.nio.ByteBuffer;
- import java.nio.CharBuffer;
- import java.nio.charset.Charset;
- import java.security.DigestException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.HashSet;
- import java.util.Set;
- public class Main {
- public static void main(String[] args) {
- try {
- new Main().crack();
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- }
- private void crack() throws NoSuchAlgorithmException {
- String charsTextField = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
- String hashAlgorithm = "MD5"; // MD5 or SHA1
- String hashString = "5d41402abc4b2a76b9719d911017c592"; // hello
- // remove duplicates from chars
- String chars = null;
- {
- String tmp = charsTextField;
- Set<Character> characters = new HashSet<Character>();
- StringBuilder s = new StringBuilder();
- for(int i = 0; i < tmp.length(); ++i) {
- char c = tmp.charAt(i);
- if(characters.add(c)) {
- s.append(c);
- }
- }
- chars = s.toString();
- }
- CharBuffer charBuffer = CharBuffer.allocate(hashString.length());
- ByteBuffer byteBuffer = ByteBuffer.allocate(hashString.length());
- StringBuilder sb = new StringBuilder();
- CharSequence hash;
- charBuffer.append(chars.charAt(0));
- MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm);
- while(true) {
- // check string
- byteBuffer.clear();
- byteBuffer.put(toBytes(charBuffer.array(), byteBuffer.array()));
- hash = hashString(messageDigest, byteBuffer.array(), 0, byteBuffer.limit(), sb);
- if(hashString.equals(hash)) {
- final String solution = new String(byteBuffer.array(), Charset.forName("UTF-8"));
- System.out.println("Success!");
- System.out.println("The answer is \"" + solution + "\"!");
- break;
- }
- // inc string
- for(int i = 0; i < charBuffer.limit(); i++) {
- if(charBuffer.get(charBuffer.limit() - 1 - i) != chars.charAt(charBuffer.limit() - 1)) {
- charBuffer.put(charBuffer.limit() - 1 - i,
- chars.charAt(chars.indexOf(charBuffer.get(charBuffer.limit() - 1 - i)) + 1));
- for(int j = charBuffer.limit() - i; j < charBuffer.limit(); j++) {
- charBuffer.put(j, chars.charAt(0));
- }
- break;
- }
- if(i == charBuffer.limit() - 1) {
- for(int j = 0; j < charBuffer.limit(); j++) {
- charBuffer.put(j, chars.charAt(0));
- }
- charBuffer.append(chars.charAt(0));
- break;
- }
- }
- }
- }
- public CharSequence hashString(MessageDigest m, byte[] buf, int offset, int len, StringBuilder sb) {
- try {
- sb.delete(0, sb.length());
- sb.setLength(0);
- m.reset();
- m.digest(buf, offset, len);
- for(int i = 0; i < buf.length; ++i)
- sb.append(Integer.toHexString((buf[i] & 0xFF) | 0x100).substring(1, 3));
- return sb;
- } catch (DigestException e) {
- e.printStackTrace();
- }
- return null;
- }
- private byte[] toBytes(char[] data, byte[] result) {
- for(int i = 0; i < result.length; i++) {
- result[i] = i < data.length ? (byte) data[i] : 0;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement