View Javadoc
1   package com.jsql.util.bruter;
2   
3   import com.jsql.util.LogLevelUtil;
4   import org.apache.commons.lang3.StringUtils;
5   import org.apache.logging.log4j.LogManager;
6   import org.apache.logging.log4j.Logger;
7   
8   import java.nio.charset.StandardCharsets;
9   import java.security.NoSuchAlgorithmException;
10  
11  public class HashBruter extends Bruter {
12      
13      /**
14       * Log4j logger sent to view.
15       */
16      private static final Logger LOGGER = LogManager.getRootLogger();
17  
18      private String hash;
19      
20      private String generatedHash;
21      
22      private String password;
23      
24      private String type;
25  
26      public void tryBruteForce() {
27          
28          this.starttime = System.nanoTime();
29          
30          for (int size = this.minLength; size <= this.maxLength; size++) {
31              
32              if (this.found || this.done) {
33                  break;
34              }
35              
36              while (this.paused) {
37                  try {
38                      Thread.sleep(500);
39                  } catch (InterruptedException e) {
40                      
41                      LOGGER.log(LogLevelUtil.IGNORE, e, e);
42                      Thread.currentThread().interrupt();
43                  }
44              }
45              
46              try {
47                  this.generateAllPossibleCombinations(StringUtils.EMPTY, size);
48              } catch (NoSuchAlgorithmException e) {
49                  LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
50              } catch (InterruptedException e) {
51                  
52                  LOGGER.log(LogLevelUtil.IGNORE, e, e);
53                  Thread.currentThread().interrupt();
54              }
55          }
56          
57          this.done = true;
58      }
59  
60      private void generateAllPossibleCombinations(String baseString, int length) throws NoSuchAlgorithmException, InterruptedException {
61          
62          while (this.paused) {
63              Thread.sleep(500);
64          }
65          
66          if (!this.found || !this.done) {
67              if (baseString.length() == length) {
68                  
69                  switch (this.type.toLowerCase()) {
70                      case "adler32": this.generatedHash = HashUtil.toAdler32(baseString); break;
71                      case "crc16":   this.generatedHash = Crc16Helper.generateCRC16(baseString); break;
72                      case "crc32":   this.generatedHash = HashUtil.toCrc32(baseString); break;
73                      case "crc64":   this.generatedHash = Crc64Helper.generateCRC64(baseString.getBytes(StandardCharsets.UTF_8)); break;
74                      case "mysql":   this.generatedHash = HashUtil.toMySql(baseString); break;
75                      case "md4":     this.generatedHash = HashUtil.toMd4(baseString); break;
76                      default:        this.generatedHash = HashUtil.toHash(this.type, baseString); break;
77                  }
78                  
79                  this.password = baseString;
80                  
81                  if (this.hash.equals(this.generatedHash)) {
82                      
83                      this.found = true;
84                      this.done = true;
85                  }
86                  
87                  this.count++;
88                  
89              } else if (baseString.length() < length) {
90                  for (String element: this.characters) {
91                      this.generateAllPossibleCombinations(baseString + element, length);
92                  }
93              }
94          }
95      }
96      
97      
98      // Getter and setter
99  
100     public String getPassword() {
101         return this.password;
102     }
103 
104     public void setHash(String hash) {
105         this.hash = hash;
106     }
107 
108     public void setType(String digestType) {
109         this.type = digestType;
110     }
111 
112     public String getGeneratedHash() {
113         return this.generatedHash;
114     }
115 }