It was hard for me to explain in the title, but I would like to create a program which takes certain text, say hello0
, hash it in sha256, and see if it has two leading zeros. If so, print hash. If not, make it hello1
, then hello2
and so on until two leading zeros are found. Here is a few ways I found on how to create my sha256 hash from 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 the byte to hex format method 1
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 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();
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("Hex format : " + hexString.toString());
}
}
When you run the code you receive Hex format : 0a0a30b1031fa60b8fa9478a070b03333df75017fd61c1b1c7e16bd929831ef5
. This has one leading zero but I want two. I don't know what to do next, but i believe I set everything up correctly. How would I go about creating a while or if statement, each time adding yeah
by 1? Would this be the best way of doing it?
The issue is that when I create a while loop the sha256 hash wont update, so it always prints 0a0a30b1031fa60b8fa9478a070b03333df75017fd61c1b1c7e16bd929831ef5. I'm wondering if I'm doing something wrong and wanted to see how someone else would do it.
thank you for your help.