// 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 HashFinder {
    private static MessageDigest md;
    private static byte[] target;
    public static void main(String[] args) throws NoSuchAlgorithmException {
        md = MessageDigest.getInstance("SHA-256");
        // Demo : find zzzz blandt små bogstaver og tal 
        target = md.digest("zzzz".getBytes());
        checkAllHash("abcdefghijklmnopqrstuwvxyz0123456789");
    }
    public static void checkAllHash(String valid) {
        int i =  1;
        while(true) {
            System.out.println("Checking all passwords of length " + i);
            checkAllHash(valid.getBytes(), new byte[i], 0);
            i++;
        }
    }
    public static void checkAllHash(byte[] valid, byte[] buf, int ix) {
        if(ix < buf.length) {
            for(int i = 0; i < valid.length; i++) {
                buf[ix] = valid[i];
                checkAllHash(valid, buf, ix + 1);
            }
        } else {
            checkHash(buf);
        }
    }
    private static void checkHash(byte[] pw) {
        if(Arrays.equals(md.digest(pw), target)) {
            System.out.println("Found match: " + new String(pw));
            System.exit(0);
        }
    }
}
