如何在java中编辑SHA256

时间:2021-07-23 05:00:22

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:

我很难在标题中解释,但我想创建一个程序,它接受某些文本,比如hello0,在sha256中哈希,看看它是否有两个前导零。如果是这样,打印哈希。如果没有,请将其设为hello1,然后设置为hello2,依此类推,直到找到两个前导零。以下是我在如何从文本中创建sha256哈希的几种方法:

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?

当您运行代码时,您会收到十六进制格式:0a0a30b1031fa60b8fa9478a070b03333df75017fd61c1b1c7e16bd929831ef5。这有一个前导零,但我想要两个。我不知道下一步该做什么,但我相信我已经正确设置了一切。我如何创建一个while或if语句,每次加1?这是最好的方式吗?

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.

问题是,当我创建一个while循环时,sha256哈希不会更新,所以它总是打印0a0a30b1031fa60b8fa9478a070b03333df75017fd61c1b1c7e16bd92983 1ef5。我想知道我做错了什么,想看看别人怎么做。

thank you for your help.

谢谢您的帮助。

1 个解决方案

#1


1  

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.

顺便说一句,这是学习Java控制流语句的重要资源。

#1


1  

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.

顺便说一句,这是学习Java控制流语句的重要资源。