hash - How to edit SHA256 in java -


it hard me explain in title, create program takes text, hello0, hash in sha256, , see if has 2 leading zeros. if so, print hash. if not, make hello1, hello2 , on until 2 leading zeros found. here few ways found on how create sha256 hash text:

import java.security.messagedigest;  public class sha  { public static void main(string[] args)throws exception {  int yeah = 40; string password = "previousblock14currentblock" + yeah;       messagedigest md = messagedigest.getinstance("sha-256");     md.update(password.getbytes());      byte bytedata[] = md.digest();      //convert byte hex format method 1     stringbuffer sb = new stringbuffer();     (int = 0; < bytedata.length; i++) {      sb.append(integer.tostring((bytedata[i] & 0xff) + 0x100, 16).substring(1));     }      system.out.println("hex format : " + sb.tostring());      stringbuffer hexstring = new stringbuffer();  (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("hex format : " + hexstring.tostring());  } } 

when run code receive hex format : 0a0a30b1031fa60b8fa9478a070b03333df75017fd61c1b1c7e16bd929831ef5. has 1 leading 0 want two. don't know next, believe set correctly. how go creating while or if statement, each time adding yeah 1? best way of doing it?

the issue when create while loop sha256 hash wont update, prints 0a0a30b1031fa60b8fa9478a070b03333df75017fd61c1b1c7e16bd92983‌​1ef5. i'm wondering if i'm doing wrong , wanted see how else it.

thank help.

public class sha {      private static string bytearraytohexstring(byte[] array) {         stringbuffer sb = new stringbuffer();         (int = 0; < array.length; i++) {             sb.append(integer.tostring((array[i] & 0xff) + 0x100, 16).substring(1));         }         return sb.tostring();     }      public static void main(string[] args) throws exception {          final int max_pass_attempts = 40000;          final string pass_prefix = "previousblock14currentblock";          messagedigest md = messagedigest.getinstance("sha-256");          (int = 0; < max_pass_attempts; i++) {             string password = pass_prefix + i;              md.reset();             md.update(password.getbytes());             byte[] hash = md.digest();              //system.out.println(bytearraytohexstring(hash));              if (hash[0] == 0 && hash[1] == 0) {                 system.out.println("password: " + password);                 system.out.println("hash: " + bytearraytohexstring(hash));                 return;             }         }                 system.out.println("no luck after " + max_pass_attempts + " tries.");     } } 

by way, this great resource learn java control flow statements.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -