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      private static final Logger LOGGER = LogManager.getRootLogger();
14  
15      private String hash;
16      private String generatedHash;
17      private String password;
18      private String type;
19  
20      public void tryBruteForce() {
21          this.starttime = System.nanoTime();
22          for (int size = this.minLength; size <= this.maxLength; size++) {
23              if (this.found || this.done) {
24                  break;
25              }
26              try {
27                  this.generateAllPossibleCombinations(StringUtils.EMPTY, size);
28              } catch (NoSuchAlgorithmException e) {
29                  LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
30              }
31          }
32          this.done = true;
33      }
34  
35      private void generateAllPossibleCombinations(String baseString, int length) throws NoSuchAlgorithmException {
36          if (!this.found || !this.done) {
37              if (baseString.length() == length) {
38                  this.generatedHash = switch (this.type.toLowerCase()) {
39                      case "adler32" -> HashUtil.toAdler32(baseString);
40                      case "crc16" -> Crc16Helper.generateCRC16(baseString);
41                      case "crc32" -> HashUtil.toCrc32(baseString);
42                      case "crc64" -> Crc64Helper.generateCRC64(baseString.getBytes(StandardCharsets.UTF_8));
43                      case "mysql" -> HashUtil.toMySql(baseString);
44                      case "md4" -> HashUtil.toMd4(baseString);
45                      default -> HashUtil.toHash(this.type, baseString);
46                  };
47                  this.password = baseString;
48                  if (this.hash.equals(this.generatedHash)) {
49                      this.found = true;
50                      this.done = true;
51                  }
52                  this.count++;
53                  
54              } else if (baseString.length() < length) {
55                  for (String element: this.characters) {
56                      this.generateAllPossibleCombinations(baseString + element, length);
57                  }
58              }
59          }
60      }
61      
62      
63      // Getter and setter
64  
65      public String getPassword() {
66          return this.password;
67      }
68  
69      public void setHash(String hash) {
70          this.hash = hash;
71      }
72  
73      public void setType(String digestType) {
74          this.type = digestType;
75      }
76  
77      public String getGeneratedHash() {
78          return this.generatedHash;
79      }
80  }