// Collection of code snippets by Arne VajhÃ¸j 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class HashFinderMT {
    private int nthr;
    private byte[] valid;
    private boolean done;
    public HashFinderMT(int nthr, String valid) {
        this.nthr = nthr;
        this.valid = valid.getBytes();
        done = false;
    }
    public void findHash(byte[] target) throws NoSuchAlgorithmException {
        int len =  1;
        while(!done) {
            System.out.println("Checking all passwords of length " + len);
            findHash(target, len);
            len++;
        }
    }
    public void findHash(byte[] target, int len) throws NoSuchAlgorithmException {
        Thread[] t = new FinderThread[nthr];
        int[] split = new int[nthr + 1];
        for(int i = 0; i <= nthr; i++) {
            split[i] = valid.length * i / nthr;
        }
        for(int i = 0; i < nthr; i++) {
            t[i] = new FinderThread(split[i], split[i + 1] - 1, this, len, target);
        }
        for(int i = 0; i < nthr; i++) {
            t[i].start();
        }
        for(int i = 0; i < nthr; i++) {
            try {
                t[i].join();
            } catch (InterruptedException e) {
            }
        }
    }
    public byte[] getValid() {
        return valid;
    }
    public boolean getDone() {
        return done;
    }
    public void setDone(boolean done) {
        this.done = done;
    }
    public static void main(String[] args) throws NoSuchAlgorithmException {
        // Demo : find zzzz blandt små bogstaver og tal
        HashFinderMT hfmt = new HashFinderMT(4, "abcdefghijklmnopqrstuwvxyz0123456789");
        hfmt.findHash(MessageDigest.getInstance("SHA-256").digest("zzzz".getBytes()));
    }
}

class FinderThread extends Thread {
    private MessageDigest md;
    private int start;
    private int end;
    private HashFinderMT hfmt;
    private byte[] buf;
    byte[] target;
    byte[] valid;
    public FinderThread(int start, int end, HashFinderMT hfmt, int len, byte[] target) throws NoSuchAlgorithmException {
        md = MessageDigest.getInstance("SHA-256");
        this.start = start;
        this.end = end;
        this.hfmt = hfmt;
        buf = new byte[len];
        this.target = target;
        valid = hfmt.getValid();
    }
    public void run() {
        for(int i = start; i <= end; i++) {
            buf[0] = valid[i];
            checkAllHash(buf, 1);
        }
    }
    public  void checkAllHash(byte[] buf, int ix) {
        if(hfmt.getDone()) return;
        if(ix < buf.length) {
            for(int i = 0; i < valid.length; i++) {
                buf[ix] = valid[i];
                checkAllHash(buf, ix + 1);
            }
        } else {
            checkHash(buf);
        }
    }
    public void checkHash(byte[] pw) {
        if(Arrays.equals(md.digest(pw), target)) {
            System.out.println("Found match: " + new String(pw));
            hfmt.setDone(true);
        }
    }
}
