Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 33

Thread: Setting up a loop for a SHA-256 hash for multiple outputs

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Setting up a loop for a SHA-256 hash for multiple outputs

    I started only a few days ago trying to figure this out under "questions". Go there for an explanation of what I am trying to do.

    I've been reading a lot and I learned a little bit more about what command and line does what and why... I am still trying to get a preferred output, but not getting it. Here's what I have so far trying to test to see if I can generate the first 17 hashes from consecutive 16 digit hexidecimal number from 0x0 to 0x10 and I am wondering why it isn't working... explanations with // below of what I try to do with each line:
    import java.security.MessageDigest;
     
    public class SHAHashingExample
    {
        public static void main(String[] args)
        {
        	String a = "0000000000000000";
     
            // here is where I am converting a string to an integer so I can make it a loop
            for (int b = Integer.parseInt(a); b <= 17; b++); {
    	for (int c=0; c <= b; c++); {
     
    		// here is where I am wanting it to print the first original hex value 
    		System.out.println("Original : " + c);
    		}
     
                    //below is the set up for the hash
            	MessageDigest md = MessageDigest.getInstance("SHA-256");
            	md.update(a.getBytes());
     
            	byte byteData[] = md.digest();
     
            	//convert the byte to hex format
            	StringBuffer hexString = new StringBuffer();
        		for (int i=0;i<byteData.length;i++) {
        			String hex=Integer.toHexString(0xff & byteData[i]);
       	     		if(hex.length()==1) hexString.append('0');
       	     		hexString.append(hex);
        		}
                    //here is where it outputs the hash of the original
            	System.out.println("Hash : " + hexString.toString());
            }
            //Here is a line separator for it to print a blank line between the first Hash and the next Original
    	System.out.printlm("")
        }
    }

    But I still get:

    Original: 0000000000000000
    Hash: fcdb4b423f4e5283afa249d762ef6aef150e91fccd810d43e5 e719d14512dec7
    <blank line>

    When I want:

    Original: 0000000000000000
    Hash: fcdb4b423f4e5283afa249d762ef6aef150e91fccd810d43e5 e719d14512dec7
    <blank line>
    Original: 0000000000000001
    Hash: 665e994827f6b03167e80c1513eb356e0d4f013f2e03a3b345 e1e5e3c24dfca6
    -----and so on until...-----
    Original: 0000000000000010
    Hash: fb4714aaedd1c0f5682d402c31131f1f85a57236e4f47cd111 acf596e3241ab4

    It's not looping correctly to give me the output I want and I am not sure where I am going wrong. Is what I want to do impossible with java language or do I just not know enough yet? Or what specifically am I missing or have out of place to make it generate the multiple outputs I want? Any help would be appreciated.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    for (int b = Integer.parseInt(a); b <= 17; b++); {
    Examine your loops very very carefully and see if you can spot what the problem is.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Can you post the code you are compiling and executing? The posted code does not compile without errors.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Quote Originally Posted by Norm View Post
    Can you post the code you are compiling and executing? The posted code does not compile without errors.
    Here is the original one I was working with
    import java.security.MessageDigest;
     
    public class SHAHashingExample 
    {
        public static void main(String[] args)throws Exception
        {
        	String a = "0000000000000000";
     
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(a.getBytes());
     
            byte byteData[] = md.digest();
     
            //convert the byte to hex 
            StringBuffer hexString = new StringBuffer();
        	for (int i=0;i<byteData.length;i++) {
        		String hex=Integer.toHexString(0xff & byteData[i]);
       	     	if(hex.length()==1) hexString.append('0');
       	     	hexString.append(hex);
        	}
            System.out.println("Original : " + a);    	
            System.out.println("Hash : " + hexString.toString());
        }
    }

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Have you spotted the mistake yet?
    Improving the world one idiot at a time!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Here is the original one I was working with
    What was the problem with that one?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Quote Originally Posted by Norm View Post
    What was the problem with that one?
    Nothing. That one is written to only give 1 output result. Per my original post, I want the consecutive output of 17 (0x0000000000000000 through 0x0000000000000010).

    Quote Originally Posted by Junky View Post
    Have you spotted the mistake yet?
    In looking it over, I either need to not close off my brackets or maybe even put in a catch-return statement? Maybe it's in my print statements where I need to make a different variable so it prints something different since all it prints is just the initial value and my loops is essentially sitting there doing nothing and is overlooked with all values for a, b, and c are all equal. Or is it even as dumb as me puting semicolons after my for statements when they aren't supposed to be there?

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Look closer.
    for (int b = Integer.parseInt(a); b <= 17; b++); {
    Above loop is wrong, Below loop is correct. Compare them.
    for (int i=0;i<byteData.length;i++) {
    Improving the world one idiot at a time!

  9. The Following User Says Thank You to Junky For This Useful Post:

    Djinn (November 18th, 2013)

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Fix the compiler errors before posting the new version of the code.

    If you need help with the errors, copy the full text of the error messages and post it.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Quote Originally Posted by Norm View Post
    Fix the compiler errors before posting the new version of the code.

    If you need help with the errors, copy the full text of the error messages and post it.
    Ok, so this:

    import java.security.MessageDigest;
     
    public class SHAHashingExample
    {
        public static void main(String[] args)throws Exception
        {
        	String a = "0000000000000000";
     
            for (int b=Integer.parseInt(a);b<=17;b++) {
     
    		System.out.println("Original : " + b);
    		}
            	MessageDigest md = MessageDigest.getInstance("SHA-256");
            	md.update(a.getBytes());
     
            	byte byteData[] = md.digest();
     
            	//convert the byte to hex format method 2
            	StringBuffer hexString = new StringBuffer();
        		for (int i=0;i<byteData.length;i++) {
        			String hex=Integer.toHexString(0xff & byteData[i]);
       	     		if(hex.length()==1) hexString.append('0');
       	     		hexString.append(hex);
        		}
            	System.out.println("Hash : " + hexString.toString());
     
        }
    }
    Gets me output of:

    Original: 1
    Original: 2
    through
    Original:17
    Hash: <hex code>

    Is it preferred I have the println just print as a hex value or better to have the loop define the hex value (example, System.out.println(String.format("%08X")? Also, the loop works with the original, but not the hash... hmm... I have an idea, but any hints would help... I'll keep noodling on it.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    The code's formatting makes it hard to read and understand. The } ending a block should be in the same column with the start of the statement with the block's beginning }

    is it preferred I have the println just print
    What do you want to see printed?
    Decide on that FIRST,
    THEN write the code to generate that output.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Quote Originally Posted by Norm View Post
    What do you want to see printed?
    Per first post:
    Original: 0000000000000000
    Hash: fcdb4b423f4e5283afa249d762ef6aef150e91fccd810d43e5 e719d14512dec7
    <blank line>
    Original: 0000000000000001
    Hash: 665e994827f6b03167e80c1513eb356e0d4f013f2e03a3b345 e1e5e3c24dfca6
    -----and so on until...-----
    Original: 0000000000000010
    Hash: fb4714aaedd1c0f5682d402c31131f1f85a57236e4f47cd111 acf596e3241ab4

    I'm working on fixing the Original now so it prints how I want, then will try to loop the hash value. You guys really are helping and I am grateful.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Original: 0000000000000001
    Work on how to create the number shown above. Give an int with value 1, how to get that String?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Quote Originally Posted by Norm View Post
    Work on how to create the number shown above. Give an int with value 1, how to get that String?
    ok so far I got the hex values without the leading zeros by doing this:

            for (int b=Integer.parseInt(a);b<=17;b++) {
    		String strHexNumber = Integer.toHexString(b);
    		System.out.println("Original : " + strHexNumber);
    		}

    Another person I got some help from said I can convert an int to a zero-padded String with String.format(). For example:
    System.out.println(String.format("%08X", 201));
    But I just get errors if I try to do that with the hexString. Still trying a few things here and there, but I'm not giving up anytime soon!

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Since you are working with Strings, create the right most part of the String that contains the hex digits, concatenate that at the end of a long enough String of "0000"s, then use substring() to take off from the right most end of the long concatenated String the number of characters you want.
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    Djinn (November 18th, 2013)

  18. #16
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Quote Originally Posted by Norm View Post
    Since you are working with Strings, create the right most part of the String that contains the hex digits, concatenate that at the end of a long enough String of "0000"s, then use substring() to take off from the right most end of the long concatenated String the number of characters you want.
    Working with an if/else string for that.

    Edit:

    nevermind, I see what you mean.
    Using this but may need to rework it:
    String prependZeros(int number) { // number >= 0
    String s= "000000000000000000"+number; // 16 zeros prepended
    return s.substring(s.length()-16); // keep the rightmost 16 chars


    --- Update ---

    Update:

    Ok, here's the updated code so far, but getting an error when compiling:

    import java.security.MessageDigest;
     
    public class SHAHashingExample
    {
        public static void main(String[] args)throws Exception
        {
        	String a = "0000000000000000";
     
            for (int b=Integer.parseInt(a);b<=17;b++) {
    		String strHexNumber = Integer.toHexString(b);
    			String prependZeros(b); { // number >= 0
           			String s= "00000000000000000"+b; // 15 zeros prepended
           			return s.substring(s.length()-16); // keep the rightmost 16 chars
       			}
    		System.out.println("Original : " + strHexNumber);
    		}
     
            	MessageDigest md = MessageDigest.getInstance("SHA-256");
            	md.update(a.getBytes());
     
            	byte byteData[] = md.digest();
     
            	//convert the byte to hex format
            	StringBuffer hexString = new StringBuffer();
        		for (int i=0;i<byteData.length;i++) {
        			String hex=Integer.toHexString(0xff & byteData[i]);
       	     		if(hex.length()==1) hexString.append('0');
       	     		hexString.append(hex);
        		        }
            	System.out.println("Hash : " + hexString.toString());
     
        }
    }

    I get errors and can't compile because of:
    String prependZeros(b).

    Been working on this for hours. Will report progress tomorrow if I get it to work. Good night for tonight

  19. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Copy and paste here the full text of any error messages you want help with.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #18
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Quote Originally Posted by Norm View Post
    Copy and paste here the full text of any error messages you want help with.
    This is the error I get when I try to compile the above.

    SHAHashingExample.java:11: error: ';' expected
    	String prependZeros(b); { // number >= 0
                               ^
    SHAHashingExample.java:11: error: not a statement
    	String prependZeros(b); { // number >= 0
                                ^
    SHAHashingExample.java:11: error: ';' expected
    	String prependZeros(b); { // number >= 0
                                 ^
    3 errors

  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    What is that line supposed to do?

    You can NOT define a method inside of another method.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Djinn (November 19th, 2013)

  23. #20
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    The 3 lines I posted earlier was an example coding to get the leading zeros to stay with original 16 digit hex number. I was going to to use an if/else but instead saw this as an example and though it would be better.

    Once I figure out the Original: formatting to add the leading zeros, I then have to figure out how to make the hash become affected by the new numbers generated by the loop

  24. #21
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Do you have a question?
    Improving the world one idiot at a time!

  25. #22
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Quote Originally Posted by Junky View Post
    Do you have a question?
    Always.

    Is my prependzero string idea (from this post) a valid one to get the result I want with the leading zeros for the Original output? And if so, since Norm says I can't use "b" as a variable or I'm guessing create the string like I did, do I just need to use a new variable or do I need to phrase it differently? I'm more experimenting at this point to see what will even compile, and if it does, see what I get for output and then edit from there.

  26. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Did you move the definiton for the prependZeros() outside of the method it was in?
    What happened then?

    Post your new code and any errors you are getting.

    Norm says I can't use "b"
    where did I say that?

    The prepend idea looks good. I've used it many times.
    If you don't understand my answer, don't ignore it, ask a question.

  27. #24
    Junior Member
    Join Date
    Nov 2013
    Posts
    19
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    Quote Originally Posted by Norm View Post
    Did you move the definiton for the prependZeros() outside of the method it was in?
    What happened then?
    I'll admit I feel I am still too new to it all and I can only guess your meaning. I assume you mean I need to close off my initial for statement with a "}", then I can take the result and run it through the new method? Even when I did that, I got the same error from this post.

    Quote Originally Posted by Norm View Post
    where did I say that?
    See above. I took your meaning for method in a method differently.

    Also if it hasn't been stated, the fact that you guys are having me find my own answer and see my own mistakes rather than just correcting my code and just giving it to me is something I really appreciate. Makes me feel like I am learning more.

  28. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Setting up a loop for a SHA-256 hash for multiple outputs

    methods must all be defined at the class level. The code for a method must be inside a {} pair.
    Move the prependZeros() method after the } that ends the method where it was coded in post#16
    If you don't understand my answer, don't ignore it, ask a question.

  29. The Following User Says Thank You to Norm For This Useful Post:

    Djinn (November 19th, 2013)

Page 1 of 2 12 LastLast

Similar Threads

  1. Setting up a SHA-256 hash for multiple passwords
    By Djinn in forum Java Theory & Questions
    Replies: 4
    Last Post: November 17th, 2013, 10:04 AM
  2. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  3. Setting variable correctly in sentinel-controlled loop
    By exchaoordo in forum Loops & Control Statements
    Replies: 1
    Last Post: June 1st, 2012, 03:40 PM
  4. Password Hash - Brute Force Test - SHA-1
    By djl1990 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 14th, 2012, 03:36 PM
  5. Need help with setting multiple font styles in jTextPane
    By jch02140 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 23rd, 2010, 06:16 AM

Tags for this Thread