View Javadoc
1   package com.jsql.util.bruter;
2   
3   import com.jsql.util.LogLevelUtil;
4   import org.apache.logging.log4j.LogManager;
5   import org.apache.logging.log4j.Logger;
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  public class Bruter {
11      
12      /**
13       * Log4j logger sent to view.
14       */
15      private static final Logger LOGGER = LogManager.getRootLogger();
16  
17      protected final List<String> characters = new ArrayList<>();
18       
19      protected boolean found = false;
20       
21      protected int maxLength;
22      protected int minLength;
23       
24      protected int count;
25       
26      protected long starttime;
27      protected long endtime;
28       
29      private static final char[] specialCharacters = {
30          '~', '`', '!', '@', '#', '$', '%', '^',
31          '&', '*', '(', ')', '_', '-', '+', '=', '{', '}', '[', ']', '|', '\\',
32          ';', ':', '\'', '"', '<', '.', ',', '>', '/', '?', ' '
33      };
34       
35      protected boolean done = false;
36  
37      public long getRemainder() {
38          return this.getNumberOfPossibilities() - this.count;
39      }
40  
41      public long getNumberOfPossibilities() {
42          long possibilities = 0;
43          for (int i = this.minLength; i <= this.maxLength; i++) {
44              possibilities += (long) Math.pow(this.characters.size(), i);
45          }
46          return possibilities;
47      }
48  
49      public void addLowerCaseLetters() {
50          for (var c = 'a'; c <= 'z'; c++) {
51              this.characters.add(String.valueOf(c));
52          }
53      }
54  
55      public void addDigits() {
56          for (var c = 0; c <= 9; c++) {
57              this.characters.add(String.valueOf(c));
58          }
59      }
60  
61      public void addUpperCaseLetters() {
62          for (var c = 'A'; c <= 'Z'; c++) {
63              this.characters.add(String.valueOf(c));
64          }
65      }
66  
67      public void addSpecialCharacters() {
68          for (char c: Bruter.specialCharacters) {
69              this.characters.add(String.valueOf(c));
70          }
71      }
72  
73      public void excludeChars(String s) {
74          char[] arrayChars = s.toCharArray();
75          for (char arrayChar: arrayChars) {
76              this.characters.remove(Character.toString(arrayChar));
77          }
78      }
79  
80      public int getPerSecond() {
81          int i;
82          try {
83              i = (int) (this.count / this.calculateTimeDifference());
84          } catch (Exception e) {
85              LOGGER.log(LogLevelUtil.IGNORE, e);
86              return 0;  // Ignore division by zero
87          }
88          return i;
89      }
90  
91      public String calculateTimeElapsed() {
92          long timeTaken = this.calculateTimeDifference();
93          
94          int seconds = (int) timeTaken;
95          
96          var minutes = 0;
97          var hours = 0;
98          var days = 0;
99          
100         minutes = seconds / 60;
101         seconds = seconds % 60;
102         hours = minutes / 60;
103         minutes = minutes % 60;
104         days = hours / 24;
105         hours = hours % 24;
106         
107         return String.format(
108             "Time elapsed: %sdays %sh %smin %ss",
109             days,
110             hours,
111             minutes,
112             seconds
113         );
114     }
115 
116     private long calculateTimeDifference() {
117         return (long) ((this.endtime - this.starttime) * (1 * Math.pow(10, -9)));
118     }
119     
120     
121     // Getter and setter
122 
123     public synchronized void setEndtime(long endtime) {
124         this.endtime = endtime;
125     }
126 
127     public void setMaxLength(int maxLength) {
128         this.maxLength = maxLength;
129     }
130 
131     public void setMinLength(int minLength) {
132         this.minLength = minLength;
133     }
134 
135     public boolean isFound() {
136         return this.found;
137     }
138 
139     public void setFound(boolean found) {
140         this.found = found;
141     }
142 
143     public int getCounter() {
144         return this.count;
145     }
146 
147     public void setIsDone(Boolean done) {
148         this.done = done;
149     }
150 
151     public boolean isDone() {
152         return this.done;
153     }
154 }