测试一组线程

时间:2014-12-10 13:40:33

标签: java arrays multithreading loops hash

我想要一个类来运行一个线程数组。每个线程都有一大堆密码要测试。我要找的目标密码是新字节[] {126,126,126,126,126}

以下是我目前需要修改的代码,以满足上述要求:

 byte[] myPW = {33,33,33,33,33};
    byte[] myPWOriginalHashed = {0}; ;
    try {myPWOriginalHashed = SecurityHash.test(new byte[]{33,126,126,126,126});} 
 catch(Exception ex) {}
    int counter = 1;
    while (true) {
        for (int i = 33; i < 128; i++) {
            for (int j = 33; j < 128; j++) {
                for (int k = 33; k < 128; k++) {
                    for (int l = 33; l < 128; l++) {
                        for (int m = 33; m < 128; m++) {
                            myPW = new byte[]{(byte)i,(byte)j,(byte)k,(byte)l,(byte)m};
                            byte[] myPWHashed = {0};
                            try {myPWHashed = SecurityHash.test(myPW);} 
                            catch (Exception ex){
                                System.out.println(ex.getLocalizedMessage());
                            }
                            // See if we have a match
                            if (Arrays.equals(myPWHashed, myPWOriginalHashed) ) {
                                // We have a match
                                System.out.println(counter + " words processed");
                                throw new Exception("Done");
                            } 

正如您所看到的,每个字节都有很多循环。如何实现一个线程来包含一组要测试的数字?

1 个答案:

答案 0 :(得分:1)

这是一个直接的解决方案。现在这创建了96个线程,这可能不是最佳的。在这种情况下,每个线程测试96 * 96 = 9216个数字。

    byte[] myPW = {33,33,33,33,33};
    byte[] myPWOriginalHashed = {0}; ;
    try {myPWOriginalHashed = SecurityHash.test(new byte[]{33,126,126,126,126});} 
    catch(Exception ex) {}
    int counter = 1;
    while (true) {
        for (int i = 33; i < 128; i++) {
            final int iFinal = i;
            for (int j = 33; j < 128; j++) {
                final int jFinal = j;
                for (int k = 33; k < 128; k++) {
                    final int kFinal = k;

                    Thread myThread = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            for (int m = 33; m < 128; m++) {
                                for (int l = 33; l < 128; l++) {
                                            myPW = new byte[] { (byte) iFinal, (byte) jFinal, (byte) kFinal, (byte) l, (byte) m };
                                            byte[] myPWHashed = { 0 };
                                            try {
                                              myPWHashed = SecurityHash.test(myPW);
                                            } catch (Exception ex) {
                                                System.out.println(ex.getLocalizedMessage());
                                            }
                                            // See if we have a match
                                            if (Arrays.equals(myPWHashed, myPWOriginalHashed)) {
                                                // We have a match
                                                System.out.println(counter + " words processed");
                                                throw new Exception("Done");
                                            }
                                        }

                                    }
                        }
                  });

                  myThread.start();

                 }
              }
          }
        }