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

时间:2016-05-20 17:02:27

标签: java

我是初学者,为了好奇而制作了这个程序,看看“解密”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);
}
}

请求帮助!

1 个答案:

答案 0 :(得分:2)

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

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

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

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

  • 以上链接的结论是: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 ]和字母[ A F ] (大写)

  • 要查找种子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 "";
    }