对于Hash Cracker Homework中的Loop帮助

时间:2012-04-15 23:26:12

标签: java multithreading

在我正在做的家庭作业上,我们正在制作哈希饼干。我正在实施它以便让我的饼干。 java调用worker.java。 Worker.java实现了Runnable。 Worker将获取char列表的开头和结尾,它将要破解的哈希值,以及创建哈希值的密码的最大长度。我知道我想在run()中做一个循环,但我想不出我会怎么做,所以它会达到给定的max pasword长度。我已经发布了我到目前为止的代码。我应该研究的任何方向或区域....我认为有一种方法可以通过某种方式来编写循环,但我不知道或找不到正确的语法。 哦..也。在主要的我划分,所以可以选择x线程数量,我知道现在写入它只适用于给出的40个可能的字符的偶数。在x个线程中,一个将尝试PW从a-g开始并且长达y长,然后下一个线程将从h-n开始,依此类推。

package HashCracker;

import java.util.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Cracker {
    // Array of chars used to produce strings
    public static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz0123456789.,-!".toCharArray();    
    public static final int numOfChar=40;

    /*
     Given a byte[] array, produces a hex String,
     such as "234a6f". with 2 chars for each byte in the array.
     (provided code)
    */
    public static String hexToString(byte[] bytes) {
        StringBuffer buff = new StringBuffer();
        for (int i=0; i<bytes.length; i++) {
            int val = bytes[i];
            val = val & 0xff;  // remove higher bits, sign
            if (val<16) buff.append('0'); // leading 0
            buff.append(Integer.toString(val, 16));
        }
        return buff.toString();
    }

    /*
     Given a string of hex byte values such as "24a26f", creates
     a byte[] array of those values, one byte value -128..127
     for each 2 chars.
     (provided code)
    */
    public static byte[] hexToArray(String hex) {
        byte[] result = new byte[hex.length()/2];
        for (int i=0; i<hex.length(); i+=2) {
            result[i/2] = (byte) Integer.parseInt(hex.substring(i, i+2), 16);
        }
        return result;
    }


    public static void main(String args[]) throws NoSuchAlgorithmException
    {

        if(args.length==1)//Hash Maker
        {
            //create a byte array , meassage digestand put password into it 
            //and get out a hash value printed to the screen using provided methods. 
            byte[] myByteArray=args[0].getBytes();
            MessageDigest hasher=MessageDigest.getInstance("SHA-1");
            hasher.update(myByteArray);
            byte[] digestedByte=hasher.digest();
            String hashValue=Cracker.hexToString(digestedByte); 

            System.out.println(hashValue);
        }
        else//Hash Cracker
        {
            ArrayList<Thread> myRunnables=new ArrayList<Thread>();
            int numOfThreads = Integer.parseInt(args[2]);
            int charPerThread=Cracker.numOfChar/numOfThreads;
            int start=0;
            int end=charPerThread-1;

            for(int i=0; i<numOfThreads; i++)
            {
                //creates, stores and starts threads. 
                Runnable tempWorker=new Worker(start, end, args[1], Integer.parseInt(args[1]));
                Thread temp=new Thread(tempWorker);
                myRunnables.add(temp);
                temp.start();
                start=end+1;
                end=end+charPerThread;
            }


        }


    }





import java.util.*;

public class Worker implements Runnable{

    private int charStart;
    private int charEnd;
    private String Hash2Crack;
    private int maxLength;
    public Worker(int start, int end, String hashValue, int maxPWlength)
    {
        charStart=start;
        charEnd=end;
        Hash2Crack=hashValue;
        maxLength=maxPWlength;


    }



    public void run()
    {
        byte[] myHash2Crack_=Cracker.hexToArray(Hash2Crack);

        for(int i=charStart; i<charEnd+1; i++)
        {

            ////// this is where I am stuck.
        }



    }
}

1 个答案:

答案 0 :(得分:0)

Cracker.numOfChar是一个整数,因此您的索引表示法没有意义。您的意思是使用Cracker.CHARS[i]吗?