Recursive Loop Function

Lets say I have the following

class someClass{

publicvoid getSomeCombo (int length, String treeHold, String alphabet){

if (length == 1){

for(int i = 0; i < alphabet.length; i++){

if ((treeHold + alphabet.substring(i, i+1)).equals("aaa")){

<need the help here>

}

}

}

else

{

for(int i = 0; i < alphabet.length; i++){

getSomeCombo (length - 1, treeHold + alphabet.substring(i, i+1), alphabet);

}

}

How would I break once I hit "aaa" with the original method running with arguments 3, "", "abcdefghijklmnopqrstuvwxyz"?

I can't afford to exit the program, using a conditional to check if its done every time wastes too much computing power, using a break will only work one loop.

Message was edited by:

Kane635

[1548 byte] By [Kane635a] at [2007-10-3 2:26:55]
# 1

What about a simple "return;" (without quotes of course)?

Some more tips:

* you can use String.charAt(int index):char for getting a single character (instead of String.substring(...))

* it is not clear what this method could be useful for. In general it is better to provide the idea of what you are intending to achieve with your problem-code.

* Good that you provided a code example. For readability, use some identation next time.

falke2203a at 2007-7-14 19:25:59 > top of Java-index,Java Essentials,New To Java...
# 2

This is the actual code:

import java.security.*;

import java.net.*;

class wordlistGenerator {

public String alphabetSet = "";

public String hashToGuess = "";

public String inputToHash = "";

public int alphabetLength = 0;

public void updateAlphabet(String input) {

alphabetSet = input;

alphabetLength = input.length();

}

public void updateHashToGuess(String input) {

hashToGuess = input;

}

public String getInputToHash() {

return inputToHash;

}

public void bruteForceInputToHash (int length, String treeHold) {

if (length == 1) {

for (int i = 0; i < alphabetLength; i++) {

if (hashToGuess.equals(getHexMD5(treeHold + alphabetSet.substring(i, i+1)))) {

inputToHash = treeHold + alphabetSet.substring(i, i+1);

}

}

}

else

{

for(int i = 0; i < alphabetLength; i++) {

bruteForceInputToHash(length-1, (treeHold + alphabetSet.substring(i, (i+1))));

}

}

}

public static String getHexMD5 (String input) {

byte[] defaultBytes = input.getBytes();

String testString = "";

try {

MessageDigest currentMD5 = MessageDigest.getInstance("MD5");

currentMD5.reset();

currentMD5.update(defaultBytes);

byte messageDigest[] = currentMD5.digest();

StringBuffer hexString = new StringBuffer();

for (int i=0; i < messageDigest.length; i++) {

String hex = Integer.toHexString(0xFF & messageDigest[i]);

if (hex.length() == 1) {

hexString.append("0" + hex);

} else {

hexString.append(hex);

}

}

testString = hexString.toString();

}

catch (NoSuchAlgorithmException nsae) {

}

return testString;

}

}

A simple return will not work. This will only end ONE method. This is a hierarchy. Example; if I do the following...

wordlistGenerator wlUnique = new wordlistGenerator();

wlUnique.updateAlphabet("abcdefghijklmnopqrstuvwxyz");

wlUnique.updateHashToGuess("212767045cc37041292a57e6335ab2f1");

wlUnique.bruteForceInputToHash(5, "");

System.out.println(wlUnique.getInputToHash);

It will print out "Asian" to the screen. However... let me try my best.

Assume we use a 3 base alphabet "abc", and length 2, and I am trying to find the input to the hash for "ca".

The computer will run as follows:

bruteForceInputToHash(4, "")

bruteForceInputToHash(4, "a")

bruteForceInputToHash(4, "aa")

(Compare the md5 hash of the above to the known hash)

bruteForceInputToHash(4, "ab")

(Compare the md5 hash of the above to the known hash)

bruteForceInputToHash(4, "ac")

(Compare the md5 hash of the above to the known hash)

bruteForceInputToHash(4, "b")

bruteForceInputToHash(4, "ba")

(Compare the md5 hash of the above to the known hash)

bruteForceInputToHash(4, "bb")

(Compare the md5 hash of the above to the known hash)

bruteForceInputToHash(4, "bc")

(Compare the md5 hash of the above to the known hash)

bruteForceInputToHash(4, "c")

bruteForceInputToHash(4, "ca")

(Compare the md5 hash of the above to the known hash)

bruteForceInputToHash(4, "cb")

(Compare the md5 hash of the above to the known hash)

bruteForceInputToHash(4, "cc")

(Compare the md5 hash of the above to the known hash)

If I use a return, ca would simply return and end, and the computer would continue checking the hashes of cb, and cc. In a larger example, if I test for the hash of "aaaaa", the program will immediately find the hash, but continue testing all the way until "zzzzz".

Edit: Is it possible to clear the stack that contains methods to be run, of all types of a specific method? Or how else would I quite all of these recursive method loops?

Message was edited by:

Kane635

Kane635a at 2007-7-14 19:25:59 > top of Java-index,Java Essentials,New To Java...
# 3
Bump.
Kane635a at 2007-7-14 19:25:59 > top of Java-index,Java Essentials,New To Java...