Advertisement
RafaSKB

Untitled

Jan 21st, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package me.rafaskb.hasher;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.CharBuffer;
  5. import java.nio.charset.Charset;
  6. import java.security.DigestException;
  7. import java.security.MessageDigest;
  8. import java.security.NoSuchAlgorithmException;
  9. import java.util.HashSet;
  10. import java.util.Set;
  11.  
  12. public class Main {
  13.    
  14.     public static void main(String[] args) {
  15.         try {
  16.             new Main().crack();
  17.         } catch (NoSuchAlgorithmException e) {
  18.             e.printStackTrace();
  19.         }
  20.     }
  21.    
  22.     private void crack() throws NoSuchAlgorithmException {
  23.         String charsTextField = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
  24.         String hashAlgorithm = "MD5"; // MD5 or SHA1
  25.         String hashString = "5d41402abc4b2a76b9719d911017c592"; // hello
  26.        
  27.         // remove duplicates from chars
  28.         String chars = null;
  29.         {
  30.             String tmp = charsTextField;
  31.             Set<Character> characters = new HashSet<Character>();
  32.             StringBuilder s = new StringBuilder();
  33.             for(int i = 0; i < tmp.length(); ++i) {
  34.                 char c = tmp.charAt(i);
  35.                 if(characters.add(c)) {
  36.                     s.append(c);
  37.                 }
  38.             }
  39.             chars = s.toString();
  40.         }
  41.        
  42.         CharBuffer charBuffer = CharBuffer.allocate(hashString.length());
  43.         ByteBuffer byteBuffer = ByteBuffer.allocate(hashString.length());
  44.         StringBuilder sb = new StringBuilder();
  45.        
  46.         CharSequence hash;
  47.        
  48.         charBuffer.append(chars.charAt(0));
  49.        
  50.         MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm);
  51.        
  52.         while(true) {
  53.             // check string
  54.             byteBuffer.clear();
  55.             byteBuffer.put(toBytes(charBuffer.array(), byteBuffer.array()));
  56.             hash = hashString(messageDigest, byteBuffer.array(), 0, byteBuffer.limit(), sb);
  57.            
  58.             if(hashString.equals(hash)) {
  59.                 final String solution = new String(byteBuffer.array(), Charset.forName("UTF-8"));
  60.                 System.out.println("Success!");
  61.                 System.out.println("The answer is \"" + solution + "\"!");
  62.                 break;
  63.             }
  64.            
  65.             // inc string
  66.             for(int i = 0; i < charBuffer.limit(); i++) {
  67.                 if(charBuffer.get(charBuffer.limit() - 1 - i) != chars.charAt(charBuffer.limit() - 1)) {
  68.                     charBuffer.put(charBuffer.limit() - 1 - i,
  69.                         chars.charAt(chars.indexOf(charBuffer.get(charBuffer.limit() - 1 - i)) + 1));
  70.                    
  71.                     for(int j = charBuffer.limit() - i; j < charBuffer.limit(); j++) {
  72.                         charBuffer.put(j, chars.charAt(0));
  73.                     }
  74.                     break;
  75.                 }
  76.                
  77.                 if(i == charBuffer.limit() - 1) {
  78.                     for(int j = 0; j < charBuffer.limit(); j++) {
  79.                         charBuffer.put(j, chars.charAt(0));
  80.                     }
  81.                     charBuffer.append(chars.charAt(0));
  82.                     break;
  83.                 }
  84.             }
  85.         }
  86.     }
  87.    
  88.     public CharSequence hashString(MessageDigest m, byte[] buf, int offset, int len, StringBuilder sb) {
  89.         try {
  90.             sb.delete(0, sb.length());
  91.             sb.setLength(0);
  92.             m.reset();
  93.            
  94.             m.digest(buf, offset, len);
  95.            
  96.             for(int i = 0; i < buf.length; ++i)
  97.                 sb.append(Integer.toHexString((buf[i] & 0xFF) | 0x100).substring(1, 3));
  98.            
  99.             return sb;
  100.         } catch (DigestException e) {
  101.             e.printStackTrace();
  102.         }
  103.         return null;
  104.     }
  105.    
  106.     private byte[] toBytes(char[] data, byte[] result) {
  107.         for(int i = 0; i < result.length; i++) {
  108.             result[i] = i < data.length ? (byte) data[i] : 0;
  109.         }
  110.         return result;
  111.     }
  112.    
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement