teaching machines

CS 145 Lecture 19 – Passwords, stack vs. heap

April 9, 2012 by . Filed under cs145, lectures, spring 2012.

Agenda

Code

Account.java

package preexam2;

public class Account {
  public static boolean authenticate(String password) {
    return password.equals("horsieso");
  }
}

Cracker.java

package preexam2;

import java.util.Arrays;

public class Cracker {
  public static void test(int[] foo) {
    foo[0] = 17;
  }
  
  public static long getPasswordCount(int nsymbols,
                                      int nchars) {
    nsymbols = 0;
    return (long) Math.pow(nsymbols, nchars);
  }

  public static void main(String[] args) {
    int a = 26;
    System.out.println(getPasswordCount(a, 5));
    System.out.println(getPasswordCount(52, 5));
    System.out.println(getPasswordCount(62, 5));
    System.out.println(getPasswordCount(26, 8));
    
    int[] bigHeavyStuff = new int[1];
    System.out.println(Arrays.toString(bigHeavyStuff));
    test(bigHeavyStuff);
    System.out.println(Arrays.toString(bigHeavyStuff));

    String symbols = "abcdefghijklmnopqrstuvwxyz";
    char[] guess = new char[8];

    for (int i = 0; i < symbols.length(); ++i) {
      guess[0] = symbols.charAt(i);

      for (int j = 0; j < symbols.length(); ++j) {
        guess[1] = symbols.charAt(j);

        for (int k = 0; k < symbols.length(); ++k) {
          guess[2] = symbols.charAt(k);

          for (int l = 0; l < symbols.length(); ++l) {
            guess[3] = symbols.charAt(l);

            for (int m = 0; m < symbols.length(); ++m) {
              guess[4] = symbols.charAt(m);

              for (int n = 0; n < symbols.length(); ++n) {
                guess[5] = symbols.charAt(n);

                for (int o = 0; o < symbols.length(); ++o) {
                  guess[6] = symbols.charAt(o);

                  for (int p = 0; p < symbols.length(); ++p) {
                    guess[7] = symbols.charAt(p);

                    String aGuess = new String(guess);
                    // System.out.println(aGuess);
                    boolean isCracked = Account.authenticate(aGuess);
                    if (isCracked) {
                      System.out.println(aGuess);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Haiku

Heap: refs cheap, news keep.
Stack: vars tack on, whacked on back.
Don’t ignore your store.