Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package HashCracker;
- import java.security.NoSuchAlgorithmException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.CountDownLatch;
- /**
- * the worker class has a method that creates the next string in
- * the alphabet. the worker class also compares two hex values
- * to see if they are the same.
- *
- */
- public class Worker extends Thread{
- private String hashValue = "";
- private Integer numChars;
- private char[] CHARS;
- private Integer numWorkers;
- private List<String> inputs;
- private Cracker cracker;
- private final CountDownLatch doneSignal;
- private Integer workNumber;
- /**
- * Creates a worker object. initializes with specific
- * values for a cracker, hashvalue, numchars, numworkers,
- * worknumber, char[] chars, and donesignal.
- * @param cracker
- * @param hashValue
- * @param numChars
- * @param numWorkers
- * @param workNumber
- * @param CHARS
- * @param doneSignal
- */
- public Worker(Cracker cracker, String hashValue, Integer numChars,
- Integer numWorkers, Integer workNumber, char[] CHARS, CountDownLatch doneSignal) {
- this.cracker = cracker;
- this.hashValue = hashValue;
- this.numChars = numChars;
- this.CHARS = CHARS;
- this.numWorkers = numWorkers;
- this.doneSignal = doneSignal;
- this.workNumber = workNumber;
- }
- /**
- * method returns the next string in the alphabet chars.
- * @param string
- * @return String NEXTSTRING
- */
- private String nextString(String string) {
- StringBuilder builder = new StringBuilder(string);
- char a = CHARS[0];
- char z = CHARS[CHARS.length-1];
- int i = builder.length() - 1;
- while (i >= 0) {
- if (builder.charAt(i) != z) {
- break;
- }
- i--;
- }
- if (i == -1) {
- return null;
- }
- builder.setCharAt(i, CHARS[indexOf(CHARS, builder.charAt(i))+1]);
- i++;
- while (i < CHARS.length) {
- builder.setCharAt(i, a);
- i++;
- }
- return builder.toString();
- }
- private int indexOf(char[] array, char element) {
- for (int i = 0, size = array.length; i < size; i++) {
- if (array[i] == element) {
- return i;
- }
- }
- return -1;
- }
- private String genString(Integer length) {
- StringBuilder builder = new StringBuilder();
- for(int i = 0; i < length; i++) {
- builder.append(CHARS[0]);
- }
- return builder.toString();
- }
- public void run() {
- inputs = new ArrayList<String>();
- String tempString = "";
- String genString = "";
- for(int i = 1; i <= numChars; i++) {
- for(int j = workNumber*(numWorkers-1);
- j < workNumber*(CHARS.length/(numWorkers)) + CHARS.length/numWorkers; j++) {
- System.out.println("hi");
- genString = genString(i);
- System.out.println(genString);
- while(genString != null) {
- try {
- tempString = cracker.generateHash(genString);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- if(tempString.equals(hashValue)) {
- System.out.println(genString);
- doneSignal.countDown();
- }
- genString = nextString(tempString);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement