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.
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:
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.
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.
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!
Here's the tutorial about methods. You need to read up about them.
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
http://docs.oracle.com/javase/tutori...arguments.html
If you don't understand my answer, don't ignore it, ask a question.
Djinn (November 19th, 2013)
Finally got it with a little bit of help from other sources.
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!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 } }
Is it working now?
If you don't understand my answer, don't ignore it, ask a question.
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.