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      protected boolean paused = false;
37  
38      public long getRemainder() {
39          return this.getNumberOfPossibilities() - this.count;
40      }
41  
42      public long getNumberOfPossibilities() {
43          
44          long possibilities = 0;
45          
46          for (int i = this.minLength; i <= this.maxLength; i++) {
47              possibilities += (long) Math.pow(this.characters.size(), i);
48          }
49          
50          return possibilities;
51      }
52  
53      public void addLowerCaseLetters() {
54          for (var c = 'a'; c <= 'z'; c++) {
55              this.characters.add(String.valueOf(c));
56          }
57      }
58  
59      public void addDigits() {
60          for (var c = 0; c <= 9; c++) {
61              this.characters.add(String.valueOf(c));
62          }
63      }
64  
65      public void addUpperCaseLetters() {
66          for (var c = 'A'; c <= 'Z'; c++) {
67              this.characters.add(String.valueOf(c));
68          }
69      }
70  
71      public void addSpecialCharacters() {
72          for (char c: specialCharacters) {
73              this.characters.add(String.valueOf(c));
74          }
75      }
76  
77      public void excludeChars(String s) {
78          
79          char[] arrayChars = s.toCharArray();
80          
81          for (char arrayChar: arrayChars) {
82              this.characters.remove(Character.toString(arrayChar));
83          }
84      }
85  
86      public int getPerSecond() {
87          
88          int i;
89          
90          try {
91              i = (int) (this.count / this.calculateTimeDifference());
92          } catch (Exception e) {
93              
94              LOGGER.log(LogLevelUtil.IGNORE, e);
95              
96              // Ignore division by zero
97              return 0;
98          }
99          
100         return i;
101     }
102 
103     public String calculateTimeElapsed() {
104         
105         long timeTaken = this.calculateTimeDifference();
106         
107         int seconds = (int) timeTaken;
108         
109         var minutes = 0;
110         var hours = 0;
111         var days = 0;
112         
113         minutes = seconds / 60;
114         seconds = seconds % 60;
115         hours = minutes / 60;
116         minutes = minutes % 60;
117         days = hours / 24;
118         hours = hours % 24;
119         
120         return String.format(
121             "Time elapsed: %sdays %sh %smin %ss",
122             days,
123             hours,
124             minutes,
125             seconds
126         );
127     }
128 
129     private long calculateTimeDifference() {
130         return (long) ((this.endtime - this.starttime) * (1 * Math.pow(10, -9)));
131     }
132     
133     
134     // Getter and setter
135 
136     public synchronized void setEndtime(long endtime) {
137         this.endtime = endtime;
138     }
139 
140     public void setMaxLength(int maxLength) {
141         this.maxLength = maxLength;
142     }
143 
144     public void setMinLength(int minLength) {
145         this.minLength = minLength;
146     }
147 
148     public boolean isFound() {
149         return this.found;
150     }
151 
152     public void setPaused(boolean paused) {
153         this.paused = paused;
154     }
155 
156     public boolean isPaused() {
157         return this.paused;
158     }
159 
160     public void setFound(boolean found) {
161         this.found = found;
162     }
163 
164     public int getCounter() {
165         return this.count;
166     }
167 
168     public int getMaxLength() {
169         return this.maxLength;
170     }
171 
172     public int getMinLength() {
173         return this.minLength;
174     }
175 
176     public void setIsDone(Boolean done) {
177         this.done = done;
178     }
179 
180     public boolean isDone() {
181         return this.done;
182     }
183 }