如何让这个程序更快更简单?

我是一个初学者,我为好奇心制作了这个程序,看看“解密”sha-256哈希需要多少时间,而且它变得非常复杂。 这个东西可以找到8个字符的长文本,其中只包含数字0..9和从a到f的几个字符。

package hash; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Hash { public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException { String[] array = new String[16]; array[0]="0"; array[1]="1"; array[1]="1"; array[2]="2"; array[3]="3"; array[4]="4"; array[5]="5"; array[6]="6"; array[7]="7"; array[8]="8"; array[9]="9"; array[10]="a"; array[11]="b"; array[12]="c"; array[13]="d"; array[14]="e"; array[15]="f"; int one, two, three, four, five, six, seven, eight; one=two=three=four=five=six=seven=eight=0; String text = ""; //this is gonna be the original text String hash = "ae5ce162888ee3ebe974976cac5ab94a3f55049f8515884883d579fb3fa378d2"; //this is the hash byte[] digest = null; MessageDigest md = MessageDigest.getInstance("SHA-256"); for (int i=1; i<999999999; i++) { if (i%(16*16*16*16*16*16*16)==0) { two=three=four=five=six=seven=eight=0; one++; text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight]; } else { if (i%(16*16*16*16*16*16)==0) { three=four=five=six=seven=eight=0; two++; text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight]; } else { if (i%(16*16*16*16*16)==0) { four=five=six=seven=eight=0; three++; text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight]; } else { if (i%(16*16*16*16)==0) { five=six=seven=eight=0; four++; text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight]; } else { if (i%(16*16*16)==0) { six=seven=eight=0; five++; text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight]; } else { if (i%(16*16)==0) { seven=eight=0; six++; text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight]; } else { if (i%16==0) { eight=0; seven++; text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight]; } else { eight++; text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight]; } } } } } } } md.update(text.getBytes("UTF-8")); digest = md.digest(); if (String.format("%064x", new java.math.BigInteger(1, digest)).equals(hash)) { i=999999999; } } System.out.println(text); } } 

谢谢你的帮助!

在发布代码之前要说很多话

  • 你正在提出没有意义的问题。 制作散列函数,以便您不仅可以计算所有散列值并进行比较。

  • 速度将取决于很多事情。 如果种子是0000将不会花很长时间,如果它是3Gs98y/!=p 9IYÇ]T$78fffIUas4ª!5MJg7BN Jfi86n ,K¨}]+I可能需要数年时间。 此外,计算机速度,使用的编程语言等。

  • 他们意味着要花很多时间。 有关详细信息,请查看此链接 。 请注意,他们正在测量数十万年,而不是数秒。

  • 这是上面链接的结论: It doesn't get much better with the fastest hardware on the planet computing thousands of hashes in parallel. No human technology will be able to crunch this number into something acceptable. So forget brute-forcing SHA-256 here It doesn't get much better with the fastest hardware on the planet computing thousands of hashes in parallel. No human technology will be able to crunch this number into something acceptable. So forget brute-forcing SHA-256 here

  • 这样您就可以更好地理解:当种子最多使用16个字符时,最多可以使用128个不同的字符。 可能的轮回是128^n (其中n:种子的长度)。

  • 它只使用数字[ 0,9 ]和字母[ AF ](大写)

  • 要找出种子0129ABCF需要227 seconds

  • 这段代码在很多方面仍然可以改进

     public static String calculateHash(String hash) throws NoSuchAlgorithmException, UnsupportedEncodingException{ MessageDigest md = MessageDigest.getInstance("SHA-256"); // 16 characters used in the seed, 8 length. Possibilities : 8 * 8 * 8... (16 times) = 8 ^ 16 long convinations = (long) Math.pow(8, 16); String hex, formatted; for (long i = 0; i < (long) convinations; i++) { hex = String.format("%08X", i); // This takes CPU time, only uncomment for debug purposses //System.out.println(hex); // Calculate hash of "hex" md.update(hex.getBytes("UTF-8")); byte[] digest = md.digest(); // Compare results formatted = String.format("%064x", new BigInteger(1, digest)); if (formatted.equals(hash)) { return hex; } } return ""; }