Jump to content

Creating hash codes from predefined strings in Java

- - - - -

  • Please log in to reply
10 replies to this topic

#1
Aventinus_

Aventinus_

    Newbie

  • Members
  • Pip
  • 5 posts
Hello I'm developing a program in Java for testing purposes. In the first section I create a hash code from a password using the MessageDigest method. In the second section I want to create hashes from a series of predefined strings (AAAAA to ABAAA). The program will create a hash for AAAAA and compare it with the used password. Then AAAAB, AAAAC ... AAABA etc. My problem is how to implement this in Java. I know that in C/C++ I would use pointers to do it. But how am I supposed to do this in Java? Is there a method for this kind of stuff? A way would be to use nested fors but that wouldn't be the best way to implement it. Any ideas or directions?

Thanks in advance.

#2
Aventinus_

Aventinus_

    Newbie

  • Members
  • Pip
  • 5 posts
Hello again guys, I solved the problem, here's the code I used:

String validChars="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

	    int validLength=validChars.length();

	    int C1,C2,C3,C4,C5;

	    String newHashcode;

	    

	    for (C1=0;C1<1;C1++)

			for (C2=0;C2<2;C2++)

				for (C3=0;C3<validLength;C3++){

					for (C4=0;C4<validLength;C4++){

						for (C5=0;C5<validLength;C5++){

							String temp=""+validChars.charAt(C1)+validChars.charAt(C2)+validChars.charAt(C3)+validChars.charAt(C4)+validChars.charAt(C5);

							newHashcode = HashC.HashCreator(temp);

							if (HashC.Compare(passHashcode, newHashcode)) {

								System.out.println("=======================");

								System.out.println("Password cracked!");

								System.out.println("Password: " + temp);

								System.out.println("Hashcode: " + newHashcode);

								System.out.println("=======================");

								break;

							}

						}

	    

					}

				}

Cheers! :thumbup1:

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
So all those loops are to create String from AAAAA to 99999 ?

Hm, there has to be a simpler way....

#4
Aventinus_

Aventinus_

    Newbie

  • Members
  • Pip
  • 5 posts

wim DC said:

So all those loops are to create String from AAAAA to 99999 ?

Hm, there has to be a simpler way....

Well, that was my thought exactly and it also raises the question: What if I wanted to check 16 or 32 chars? So I guess that there must be a more clever way. If you stumble upon something, let me know. :-)

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
There you go, kinda long line, should maybe split it up, but it works.
    public static void main(String[] args) {
        String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        int length = validChars.length();
        int max = (int) Math.pow(validChars.length(), 5.0);
        for (int i = 0; i < max ; i++) {
            String result = "" + validChars.charAt((int) (i/(Math.pow(length, 4.0))%length)) + validChars.charAt((int) (i/(Math.pow(length, 3.0))%length)) + validChars.charAt((int) (i/(Math.pow(length, 2.0))%length)) + validChars.charAt((int) (i/(Math.pow(length, 1.0))%length)) + validChars.charAt(i%length);
            System.out.println(result);
        }
    }
Takes a while to run, maybe change the max to use pow( ..., 4.0) instead of 5.0 at first, then it goes to A9999.

Edit:
Made it pwettier
    public static void main(String[] args) {
        String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        int length = validChars.length();
        int max = (int) Math.pow(validChars.length(), 5.0);
        int maxChars = 5;


        for (int i = 0; i < max ; i++) {
            StringBuilder sb = new StringBuilder(maxChars);
            for(int j=maxChars-1 ; j>=0 ; j--){
                sb.append(validChars.charAt((int) (i/(Math.pow(length, j))%length)));
            }
            System.out.println(sb.toString());
        }
    }


#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Here's a quicker version of what DC posted:
		String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

		StringBuffer result = new StringBuffer("");

		int length = validChars.length();

		int max = (int) Math.pow(validChars.length(), 4.0);

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

			result.append("")

					.append(validChars.charAt((int) (i

							/ (Math.pow(length, 4.0)) % length)))

					.append(validChars.charAt((int) (i

							/ (Math.pow(length, 3.0)) % length)))

					.append(validChars.charAt((int) (i

							/ (Math.pow(length, 2.0)) % length)))

					.append(validChars.charAt((int) (i

							/ (Math.pow(length, 1.0)) % length)))

					.append(validChars.charAt(i % length)).append("\n");



		}


		System.out.println(result);

	}

I get the dreaded OutOfMemory error when I go beyond 4.

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Aye that be faster, but know that you keep a String of
1679616 characters in memory in the end. That's quite a bit of RAM you're occupying.
1 char = 16 bit, so 3280,5 KiloByte.
Not too bad, for AAAAA to A9999 , if you go to "99999" however you end up with a 115MB String.

#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

wim DC said:

Aye that be faster, but know that you keep a String of
1679616 characters in memory in the end. That's quite a bit of RAM you're occupying.
1 char = 16 bit, so 3280,5 KiloByte.
Not too bad, for AAAAA to A9999 , if you go to "99999" however you end up with a 115MB String.

Yes that's correct. Does your second version run any faster? The reason it's slow is that the constant System.out.println()'s take up precious time.

#9
Aventinus_

Aventinus_

    Newbie

  • Members
  • Pip
  • 5 posts
Well, I thank you both for your time but I have a question. Both your solutions require less lines of code but what about processing power? From that point of view, which solution is the best? Especially when considering adding more characters (16 for example).

#10
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
They're both the same as far as processing power.
The reason DC's version runs slower is because more usage of I/O.
The reason mine gets the OutOfMemory error is because I'm storing the entire Output in a String(in RAM), then printing at the end of the program.

I'm wondering, in my case, that perhaps you can print and dump the contents of the string buffer several times throughout the program to decrease RAM usage and keep some speed.

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I think, looking purely in terms of processing power your initial code would / should be fastest.
As in ours, there's stuff the processor must compute like the Math.pow(...) , and the modulus operator "%", where you just have a bunch of loops.

The only thing you could enhance is, like lethalwire did, keep it all in a String before printing because System.out.println(..) is slow as f:w00t:

The downside of ur loops is that if you want a lenghtier string, you must change more code than ours. Meaning our code is easier to maintain.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users