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 2 of 2 FirstFirst 12
Results 26 to 33 of 33

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

  1. #26
    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
    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
    I'll give it a go. Taking a break for a few hours and will let you know what I get.

  2. #27
    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
    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
    I moved it like you said, but it still must be recognizing it as a method in a method. will use // to describe what I am trying to do.

    import java.security.MessageDigest;
     
    public class SHAHashingExample  {
        public static void main(String[] args)throws Exception {
        	String a = "0000000000000000"; // defines Original value
     
            for (int b=Integer.parseInt(a);b<=17;b++); { 
    		String strHexNumber = Integer.toHexString(b); // loop to change integer to a hex value 
    		}
    		String prependZeros(int b); { // number >= 0  **Compiling Error at parantheses**
    		String s= "000000000000000000"+b; // 16 zeros prepended
    		return s.substring(s.length()-16); // keep the rightmost 16 chars  
    			System.out.println("Original : " + strHexNumber); // Outputs 17 hex values from 0 to 11
    			}	
    		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()); //Output of Hash
     
        }
    }

    Trying to compile, I still get:

    SHAHashingExample.java:11: error: ';' expected
    	String prependZeros(int b); { // number >= 0
                               ^
    SHAHashingExample.java:11: error: ';' expected
    	String prependZeros(int b); { // number >= 0
                                     ^
    2 errors

    Do I need a different variable besides b now so it will compile? I'm not sure what I am missing since I feel like I have tried everything else. But will keep working.

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

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

    You need to move the definition for the prependZeros() method OUTSIDE of the {} that define the main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    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
    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
    import java.security.MessageDigest;
     
    public class SHAHashingExample  {
        public static void main(String[] args)throws Exception {
        	String a = "0000000000000000"; // defines Original value
     
            for (int b=Integer.parseInt(a);b<=17;b++); { 
    		String strHexNumber = Integer.toHexString(b); // loop to change integer to a hex value 
    		}
     		String prependZeros(int b); { // number >= 0  **Compiling Error at parantheses**
    		String s= "000000000000000000"+b; // 16 zeros prepended
    		return s.substring(s.length()-16); // keep the rightmost 16 chars 
    		}
    		System.out.println("Original : " + strHexNumber); // Outputs 17 hex values from 0 to 17
     
    		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()); //Output of Hash
     
        }
    }

    I've moved the code to this now, still get same compiling error. When you say "method", do you mean the for loop statement or do you mean I have to create a whole new section with a new "public static void main(String[] args) {" that begins at every coding... But I don't think that will help. Hahaha I feel like I am so close too!

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

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

    If you don't understand my answer, don't ignore it, ask a question.

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

    Djinn (November 19th, 2013)

  7. #31
    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

    Finally got it with a little bit of help from other sources.
    import java.security.MessageDigest;
    public class SHAHashingExample 
    {
    	public static void main(String[] args)throws Exception
    	{
    		for(int j=0; j<17; j++) // loop to create consecutive "Original" output
    		{
    			String a = "0000000000000000"; // Define Starting number
    			a=appendHash(j,a); // replaces the last digit(s) of a, with the value in j 
    			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); // outputs original 16 digit hex code         
    			System.out.println("Hash : " + hexString.toString()); //outputs Hash of Original
    		}
    	}
     
     
     
     
    	public static String appendHash(int b, String s) // New Method defined to keep 16 digit hex
    	{
    		s+=Integer.toHexString(b); // convert b to a hex, and only then appends it to s 
     
    		return s.substring(s.length()-16); // keep the rightmost 16 chars 
     
    	}
    }
    I wasn't quite sure what you meant by new method until it was explained to me to place it under it's own "public static etc". What you said makes WAY more sense now! Made a few other changes and put // next to them as I understood what line does what. Thanks again!

  8. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

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

    Is it working now?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #33
    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
    Is it working now?
    Yes, but I guess I was curious what changes I could make to reference this as "long" expressions instead of int. Int is only 32 bit and could only do the first 2147483647 (16^8-1) without error. My original intention was to do the entire string of 18 quintillion+ possible hashes from 0000000000000000 to ffffffffffffffff, but forgot it's max limit. My computer is STILL processing the program so I can test the speed and how soon it will actually be done with just 2 billion. Even with long expression, the max I can get is 9223372036854775808 and I'd just run it twice with different limits. That's one big text file... HAHA!

    But thanks again guys unless you want to assist with anything of the above I mentioned.

Page 2 of 2 FirstFirst 12

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