Dictionary algorithm, computing all case options I am building this algorithm as part of a larger project for a network security class. The gist of this part is that I have been given a dictionary, and I need to find every possible option for a word in both upper and lowercase.

My teacher selected a word out of this dictionary, and messed with the case of a few random letters in it, then created the SHA-256 hash value for it. We have been given the hash value and now have to find the word.

I figured I would create two new dictionaries, use the first one to create all possible options of the upper lowercase combinations, then use that second one to create a third with all the hash values.

Example:




to
tO
To
TO



My first thought was that it looked a lot like counting in binary, and that's what I have based this algorithm off of. The only problem is that it's pretty slow.



   public class DictionaryBuilder {

private Scanner in;
private PrintWriter out;

public DictionaryBuilder() {

}

public File createHashDictionary(File dictionary) throws IOException {
    File hashDict = new File("HashDictionary.txt");
    in = new Scanner(dictionary);
    out = new PrintWriter(hashDict);
    String word;
    String wordList[];

    while (in.hasNext()) {
        wordList = getSmallWordList(in.nextLine());
        for (int i = 0; i &lt; wordList.length; i++) {
            out.println(wordList[i]);
        }

    }
    in.close();
    out.close();
    return hashDict;
}

public String[] getSmallWordList(String _word) {
    System.out.println(_word);
    char[] word = _word.toCharArray();

    int length = (int) (Math.pow(2, _word.length()));

    char[][] binaryList = new char[word.length][length];

    int weirdCount = length / 2;

    for (int i = 0; i &lt; word.length; i++) {
        //System.out.println();
        int count = 0;
        int onOff = 1;

        for (int n = 0; n &lt; length; n++) {
            if (count == weirdCount) {
                onOff++;
                count = 0;
            }
            if ((onOff % 2 == 0)) {
                binaryList[i][n] = Character.toLowerCase(word[i]);
                System.out.print(binaryList[i][n]);
                count++;
            } else {
                binaryList[i][n] = Character.toUpperCase(word[i]);
                //System.out.print(binaryList[i][n]);
                count++;
            }
        }
        weirdCount = weirdCount / 2;
    }

    //System.out.println();
    //System.out.println();
    String wordList[] = new String[length];

    for (int i = 0; i &lt; wordList.length; i++) {
        wordList[i] = "";
        for (int n = 0; n &lt; word.length; n++) {
            wordList[i] = wordList[i] + "" + binaryList[n][i];

        }
        if(i%10==0){
        System.out.println(wordList[i]);}
    }

    return wordList;
}


Main method:

    public class Launch {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {

    File dictionary = new File("Dictionary");

    DictionaryBuilder build = new DictionaryBuilder();
    File x = build.createHashDictionary(dictionary);

   Scanner in = new Scanner(x);
   System.out.println(in.nextLine());


}


It's stuck on "Antidisestablishmentarian." Can I streamline this?
You are absolutely correct - it is binary counting. In the case of antidiseastablismnetarianism you'd need to produce \$2^{28}\$ words, that is quite a few. No surprise you are stuck, but unfortunately there is no real way around.

You may streamline the word generation a bit (most likely it wouldn't affect the bottom line) by doing a more natural binary counting. For example,

    int length = 1 &lt;&lt; word.length;

    for (int counter = 0; counter &lt; length; ++counter) {
        for (int pos = 0; pos &lt; word.length; ++pos) {
            binaryList[pos][counter] = (counter &amp; (1 &lt;&lt; pos))?
                toUpper(word[pos]): toLower(word[pos]);
        }
    }