K. Wonder
K. Wonder

Reputation: 21

Java Rainbow Tables- compute method

I am trying to write a program using Rainbow Tables to hash and hack passwords of length four. The algorithm for the hash value is: ℎ𝑎𝑠ℎ𝑉𝑎𝑙𝑢𝑒 = (163 ∗ 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟 𝑎𝑡 𝑝𝑜𝑠𝑡𝑖𝑜𝑛 0) + (162 ∗ 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟 𝑎𝑡 𝑝𝑜𝑠𝑡𝑖𝑜𝑛 1 + (161 ∗ 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟 𝑎𝑡 𝑝𝑜𝑠𝑡𝑖𝑜𝑛 2) + (160 ∗ 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟 𝑎𝑡 𝑝𝑜𝑠𝑡𝑖𝑜𝑛 3).

I need help figuring out what I did wrong on the compute method. There are comments before the code saying what needs to be done if that will help solve the problem.

public void compute() {
    //TODO:  Add code to compute all possible 4-letter passwords - store in rainbow
    // Begin your possible passwords with aaaa and end with zzzz
    // Use hashCode(pwd) % 29 to determine the key (index) of where this element should be added to rainbow's ArrayList of ArrayLists
    // You will need to cast as int, such as key = (int) hashCode(pwd)%29  - key is the index of where to add this element in the rainbow ArrayList of ArrayLists
    if(password.length() != passwordLength){
        throw new InvalidPasswordException();
    }
    while(password.startsWith("aaaa") && password.endsWith("zzzz")){
        key = (int) (hashCode(password)%29);
        rainbow.add(key, password);
    }
}

please let me know if anymore information is needed.

EDIT: here is the entire code so all the other methods can be seen (Not all are complete yet)

import java.util.*;
public class HashMap implements HashMapADT{
private String password;
private Long hash;
private int key;
private final int passwordLength = 4;
ArrayList<ArrayList<PasswordMap>> rainbow;

public HashMap() {
    password = "";
    hash = -1L;
    key = -1;
    initializeHashMap();
}
public HashMap(String str) {
    password = str;
    hash = hashCode(str);
    key = (int)(hash % 29);
    initializeHashMap();
}

private void initializeHashMap() {
    //Initialize an ArrayList of 29 elements - when dividing by 29 only remainders 0 - 28 are possible
    rainbow = new ArrayList<>();
    for(int i = 0; i < 29; i++) {
        rainbow.add(new ArrayList<PasswordMap>());
    }
    compute();      
}

public Long hashCode(String str) throws InvalidPasswordException{
    this.hash = 0L;

    //TODO:  Calculate hashCode using hashing function based on powers of 29

    return 0L;  // temp - delete once hashCode method is implemented.
}

public void compute() {
    //TODO:  Add code to compute all possible 4-letter passwords - store in rainbow
    // Begin your possible passwords with aaaa and end with zzzz
    // Use hashCode(pwd) % 29 to determine the key (index) of where this element should be added to rainbow's ArrayList of ArrayLists
    // You will need to cast as int, such as key = (int) hashCode(pwd)%29  - key is the index of where to add this element in the rainbow ArrayList of ArrayLists
    if(password.length() != passwordLength){
        throw new InvalidPasswordException();
    }
    while(password.startsWith("aaaa") && password.endsWith("zzzz")){
        key = (int) (hashCode(password)%29);
        rainbow.add(key, password);
    }
}
public Long hash(String pwd) {
    //TODO:  Return the hashcode for a given password
    // First, hash the password: int key = (int)(hashCode(pwd) % 29);
    // Use this key to determine which element in the rainbow table you should be traversing to find the hash code
    // Recall rainbow is an ArrayList of ArrayLists!!
    key = (int)(hashCode(pwd)%29);

    return 0L;  // temp - delete once hash method is implemented.
}

public String hack(Long pwdHash) {
    String pwd="";
    //TODO:  Given a hashed password, pwdHash, determine the password
    // When identifying a correct hashed password, you will need to look at a difference RATHER THAN ==
    // That is, 
    //if (Math.abs(pwdHash - rainbow.get(key).get(i).getHash())<.001)  -  you've found your password!!
    // Note:  key is the location of the rainbow list you should be traversing:  key = (int)((pwdHash) % 29);


    return pwd;
}

@Override
public String toString() {
    return password + ": " + hash;
}

}

Upvotes: 1

Views: 1874

Answers (1)

slim
slim

Reputation: 41203

The problem here is your while loop.

while(f()) means "keep doing this as long as f() returns true".

You've said "keep doing this as long as password starts with "aaaa" and password ends with "zzzz".

We don't see you initialise password but if it's a four char string as described, it's impossible for it to both start with "aaaa" and end with "zzzz", so the while loop's block will never execute.

If password was "aaaazzzz" so that the condition was true, then, since you never modify the value of password, the while loop would repeat forever.

You probably want something like:

for(int i=0;; i++) {
    String password = createPassword(i);
    rainbow.add(password, hash(password));
}

... and write a method createPassword() such that createPassword(0) returns "aaaa", createPassword(1) returns "aaab", createPassword(26) returns "aaba" and so on.

Upvotes: 1

Related Questions