Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package HashCracker;
  2.  
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.concurrent.CountDownLatch;
  6.  
  7. public class Cracker {
  8.     // Array of chars used to produce strings
  9.     public static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz0123456789.,-!".toCharArray();   
  10.    
  11.     public Cracker(){
  12.        
  13.     }
  14.     /*
  15.      Given a byte[] array, produces a hex String,
  16.      such as "234a6f". with 2 chars for each byte in the array.
  17.      (provided code)
  18.     */
  19.     public static String hexToString(byte[] bytes) {
  20.         StringBuffer buff = new StringBuffer();
  21.         for (int i=0; i<bytes.length; i++) {
  22.             int val = bytes[i];
  23.             val = val & 0xff;  // remove higher bits, sign
  24.             if (val<16) buff.append('0'); // leading 0
  25.             buff.append(Integer.toString(val, 16));
  26.         }
  27.         return buff.toString();
  28.     }
  29.    
  30.     /*
  31.      Given a string of hex byte values such as "24a26f", creates
  32.      a byte[] array of those values, one byte value -128..127
  33.      for each 2 chars.
  34.      (provided code)
  35.     */
  36.     public static byte[] hexToArray(String hex) {
  37.         byte[] result = new byte[hex.length()/2];
  38.         for (int i=0; i<hex.length(); i+=2) {
  39.             result[i/2] = (byte) Integer.parseInt(hex.substring(i, i+2), 16);
  40.         }
  41.         return result;
  42.     }
  43.    
  44.     // possible test values:
  45.     // a 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8
  46.     // fm adeb6f2a18fe33af368d91b09587b68e3abcb9a7
  47.     // a! 34800e15707fae815d7c90d49de44aca97e2d759
  48.     // xyz 66b27417d37e024c46526c2f6d358a754fc552f3
  49.  
  50.     public String generateHash(String string) throws NoSuchAlgorithmException {
  51.         MessageDigest md = MessageDigest.getInstance("SHA");
  52.        
  53.         string = hexToString(string.getBytes());
  54.         byte[] byteArray = md.digest(hexToArray(string));
  55.         string = hexToString(byteArray);
  56.         return string;
  57.     }
  58.    
  59.     public static void main(String[] args) throws NoSuchAlgorithmException, InterruptedException {
  60.  
  61.         String result = "";
  62.         Cracker cracker = new Cracker();
  63.        
  64.         if(args.length == 1) {
  65.             result = cracker.generateHash(args[0]);
  66.             System.out.println(result);
  67.         }
  68.         else if(args.length == 3) {
  69.             String hashValue = args[0];
  70.             Integer maxChars = Integer.parseInt(args[1]);
  71.             Integer numWorkers = Integer.parseInt(args[2]);
  72.             Integer workNumber;
  73.             CountDownLatch doneSignal = new CountDownLatch(numWorkers);
  74.            
  75.             for(int i = 0; i < numWorkers; i++) {
  76.                 workNumber = i;
  77.                 new Worker(cracker, hashValue, maxChars, numWorkers, workNumber, CHARS, doneSignal).start();
  78.                 doneSignal.await();
  79.             }
  80.             System.out.println("All Done");
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement