55502f40dc8b7c769880b10874abc9d0

Can you improve the speed of my brute forcer?

Its not quite finished yet, the threads part at the end will ideally stop all threads crunching the password when one thread finds a match.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercise1;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author mdw937
 */
public class BruteForceAlgorithm {
    private char[] numbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    private char[] lower = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
        'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z'};
    private char[] upper = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
        'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
        'W', 'X', 'Y', 'Z'};
    private char[] all = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
        'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
        'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
        'W', 'X', 'Y', 'Z'};
    private String input;
    private int maxlen = 3;

    public BruteForceAlgorithm(String a) {
        input = a;
    }

    static void threadMessage(String message) {
        String threadName = Thread.currentThread().getName();
        System.out.format("%s: %s%n", threadName, message);
    }

    // A class to compute all the possible combinations of a given symbol set.
    // We can make many threads of these within the brute force object.
    // We interupt the active threads when the password is found.
    class recursive implements Runnable {
        // The symbol set we're working with
        char[] syms;
        Thread thisThread;

        public recursive(char[] symbols) {
            thisThread = Thread.currentThread();
            syms = symbols;
        }

        // Declared as private for compiler inline performance
        private void bruteNext(String old) {
            if (Thread.interrupted()){
                //We've been interrupted: no more crunching.
                return;
            } else {
                if (check(old)) {
                    // Password found!
                    System.out.println("Found!");
                    return;
                } else {
                    // Search for next combination
                    for (int i = 0; i < syms.length; i++) {
                        if (old.length() <= maxlen) {
                            bruteNext(old.concat(new Character(syms[i]).toString()));
                            //System.out.println(old);
                        }
                    }
                }
            }
        }

        public void run() {
            for (int i = 0; i < syms.length; i++) {
                // Call this method recursivley
                bruteNext(new Character(syms[i]).toString());
            }
            threadMessage("    No Match (by brute force)");
        }
    }

    // Declared as private for compiler inline performance
    private boolean check(String temp) {
        try {
            String guess = ConvertToSHA1.SHA1(temp);
            // Check if password is correct!
            if (input.matches(guess)) {
                threadMessage("    Match (by brute force) " + temp);
                //Thread.currentThread().interrupt();
                Thread.currentThread().stop();
                // Password Found!
                return true;
            }
            else {
                // No Password found!
                return false;
            }
        // Catach exceptions
        } catch (NoSuchAlgorithmException ex) {
            System.out.println("NoSuchAlgorithmException thrown whilst trying to convert password to SHA1");
            return false;
        } catch (UnsupportedEncodingException ex) {
            System.out.println("UnsupportedEncodingException thrown whilst trying to convert password to SHA1");
            return false;
        }   
    }

    public void run() {
        // Thread 0
        Thread a = new Thread(new recursive(all));
        a.start();
        // Thread 1
        Thread b = new Thread(new recursive(lower));
        b.start();
        // Thread 2
        Thread c = new Thread(new recursive(upper));
        c.start();
        // Thread 3
        Thread d = new Thread(new recursive(numbers));
        d.start();
    }
}

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

Sly

December 15, 2009, December 15, 2009 20:23, permalink

No rating. Login to rate!

add a synchronized method like bool is_psw_found(). This will return a global bool shared by all instances of that class. Then, at the start of your recurse thing, call that method and, if true, return.

E8b44bec0232508e67b40c2422b253e7

MaryTenderLOS

December 17, 2011, December 17, 2011 00:06, permalink

No rating. Login to rate!

Although these fun baby toys are designed for babies, parent’s supervision is still the best. Not only does it have pre-recorded stock lines, it makes you sound like Optimus Pride when you put it on. Offer fun alternatives to television.

17aef5e81d685e2c1fe2137c2b57b153

QuomBeeskheiceMem

December 17, 2011, December 17, 2011 08:13, permalink

No rating. Login to rate!

hello my beautiful world
hello everyone on this place!
i am Kate

Your refactoring





Format Copy from initial code

or Cancel