Search code examples
javahashsha256

How to edit SHA256 in java


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 0a0a30b1031fa60b8fa9478a070b03333df75017fd61c1b1c7e16bd92983‌​1ef5. I'm wondering if I'm doing something wrong and wanted to see how someone else would do it.

thank you for your help.


Solution

  • public class Sha {
    
        private static String byteArrayToHexString(byte[] array) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 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");
    
            for (int i = 0; i < 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 the way, this is the great resource to learn about Java control flow statements.