1 package com.jsql.util.bruter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.security.MessageDigest;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class DigestMD4 extends MessageDigest implements Cloneable {
38
39
40
41
42
43
44
45 private static final int BLOCK_LENGTH = 64;
46
47
48
49
50 private int[] context = new int[4];
51
52
53
54
55 private long count;
56
57
58
59
60 private byte[] buffer = new byte[DigestMD4.BLOCK_LENGTH];
61
62
63
64
65 private final int[] X = new int[16];
66
67
68
69
70
71 public DigestMD4() {
72 super("MD4");
73 this.engineReset();
74 }
75
76
77
78
79 private DigestMD4(DigestMD4 md) {
80 this();
81 this.context = md.context.clone();
82 this.buffer = md.buffer.clone();
83 this.count = md.count;
84 }
85
86
87
88
89
90
91
92
93 @Override
94 public Object clone() throws CloneNotSupportedException {
95 Object clone = super.clone();
96 return new DigestMD4(this);
97 }
98
99
100
101
102
103
104
105
106
107 @Override
108 public void engineReset() {
109
110
111 this.context[0] = 0x67452301;
112 this.context[1] = 0xEFCDAB89;
113 this.context[2] = 0x98BADCFE;
114 this.context[3] = 0x10325476;
115 this.count = 0L;
116
117 for (int i = 0; i < DigestMD4.BLOCK_LENGTH; i++) {
118 this.buffer[i] = 0;
119 }
120 }
121
122
123
124
125 @Override
126 public void engineUpdate(byte b) {
127
128 int i = (int)(this.count % DigestMD4.BLOCK_LENGTH);
129 this.count++;
130 this.buffer[i] = b;
131
132 if (i == DigestMD4.BLOCK_LENGTH - 1) {
133 this.transform(this.buffer, 0);
134 }
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149 @Override
150 public void engineUpdate(byte[] input, int offset, int len) {
151
152 if (offset < 0 || len < 0 || (long) offset + len > input.length) {
153 throw new ArrayIndexOutOfBoundsException();
154 }
155
156
157 int bufferNdx = (int)(this.count % DigestMD4.BLOCK_LENGTH);
158 this.count += len;
159 int partLen = DigestMD4.BLOCK_LENGTH - bufferNdx;
160 int i = 0;
161
162 if (len >= partLen) {
163
164 System.arraycopy(input, offset, this.buffer, bufferNdx, partLen);
165
166 this.transform(this.buffer, 0);
167
168 for (i = partLen; i + DigestMD4.BLOCK_LENGTH - 1 < len; i+= DigestMD4.BLOCK_LENGTH) {
169 this.transform(input, offset + i);
170 }
171
172 bufferNdx = 0;
173 }
174
175
176 if (i < len) {
177 System.arraycopy(input, offset + i, this.buffer, bufferNdx, len - i);
178 }
179 }
180
181
182
183
184
185
186
187
188 @Override
189 public byte[] engineDigest() {
190
191 int bufferNdx = (int)(this.count % DigestMD4.BLOCK_LENGTH);
192 int padLen = bufferNdx < 56 ? 56 - bufferNdx : 120 - bufferNdx;
193
194
195 byte[] tail = new byte[padLen + 8];
196 tail[0] = (byte) 0x80;
197
198
199
200
201 for (int i = 0; i < 8; i++) {
202 tail[padLen + i] = (byte)((this.count * 8) >>> (8 * i));
203 }
204
205 this.engineUpdate(tail, 0, tail.length);
206
207 byte[] result = new byte[16];
208
209
210 for (int i = 0; i < 4; i++) {
211 for (int j = 0; j < 4; j++) {
212 result[i * 4 + j] = (byte)(this.context[i] >>> (8 * j));
213 }
214 }
215
216
217 this.engineReset();
218
219 return result;
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 private void transform(byte[] block, int offset) {
236
237
238 for (int i = 0; i < 16; i++) {
239 this.X[i] =
240 (block[offset++] & 0xFF)
241 | (block[offset++] & 0xFF) << 8
242 | (block[offset++] & 0xFF) << 16
243 | (block[offset++] & 0xFF) << 24;
244 }
245
246 int A = this.context[0];
247 int B = this.context[1];
248 int C = this.context[2];
249 int D = this.context[3];
250
251 A = this.FF(A, B, C, D, this.X[ 0], 3);
252 D = this.FF(D, A, B, C, this.X[ 1], 7);
253 C = this.FF(C, D, A, B, this.X[ 2], 11);
254 B = this.FF(B, C, D, A, this.X[ 3], 19);
255 A = this.FF(A, B, C, D, this.X[ 4], 3);
256 D = this.FF(D, A, B, C, this.X[ 5], 7);
257 C = this.FF(C, D, A, B, this.X[ 6], 11);
258 B = this.FF(B, C, D, A, this.X[ 7], 19);
259 A = this.FF(A, B, C, D, this.X[ 8], 3);
260 D = this.FF(D, A, B, C, this.X[ 9], 7);
261 C = this.FF(C, D, A, B, this.X[10], 11);
262 B = this.FF(B, C, D, A, this.X[11], 19);
263 A = this.FF(A, B, C, D, this.X[12], 3);
264 D = this.FF(D, A, B, C, this.X[13], 7);
265 C = this.FF(C, D, A, B, this.X[14], 11);
266 B = this.FF(B, C, D, A, this.X[15], 19);
267
268 A = this.GG(A, B, C, D, this.X[ 0], 3);
269 D = this.GG(D, A, B, C, this.X[ 4], 5);
270 C = this.GG(C, D, A, B, this.X[ 8], 9);
271 B = this.GG(B, C, D, A, this.X[12], 13);
272 A = this.GG(A, B, C, D, this.X[ 1], 3);
273 D = this.GG(D, A, B, C, this.X[ 5], 5);
274 C = this.GG(C, D, A, B, this.X[ 9], 9);
275 B = this.GG(B, C, D, A, this.X[13], 13);
276 A = this.GG(A, B, C, D, this.X[ 2], 3);
277 D = this.GG(D, A, B, C, this.X[ 6], 5);
278 C = this.GG(C, D, A, B, this.X[10], 9);
279 B = this.GG(B, C, D, A, this.X[14], 13);
280 A = this.GG(A, B, C, D, this.X[ 3], 3);
281 D = this.GG(D, A, B, C, this.X[ 7], 5);
282 C = this.GG(C, D, A, B, this.X[11], 9);
283 B = this.GG(B, C, D, A, this.X[15], 13);
284
285 A = this.HH(A, B, C, D, this.X[ 0], 3);
286 D = this.HH(D, A, B, C, this.X[ 8], 9);
287 C = this.HH(C, D, A, B, this.X[ 4], 11);
288 B = this.HH(B, C, D, A, this.X[12], 15);
289 A = this.HH(A, B, C, D, this.X[ 2], 3);
290 D = this.HH(D, A, B, C, this.X[10], 9);
291 C = this.HH(C, D, A, B, this.X[ 6], 11);
292 B = this.HH(B, C, D, A, this.X[14], 15);
293 A = this.HH(A, B, C, D, this.X[ 1], 3);
294 D = this.HH(D, A, B, C, this.X[ 9], 9);
295 C = this.HH(C, D, A, B, this.X[ 5], 11);
296 B = this.HH(B, C, D, A, this.X[13], 15);
297 A = this.HH(A, B, C, D, this.X[ 3], 3);
298 D = this.HH(D, A, B, C, this.X[11], 9);
299 C = this.HH(C, D, A, B, this.X[ 7], 11);
300 B = this.HH(B, C, D, A, this.X[15], 15);
301
302 this.context[0] += A;
303 this.context[1] += B;
304 this.context[2] += C;
305 this.context[3] += D;
306 }
307
308
309
310 private int FF(int a, int b, int c, int d, int x, int s) {
311 int t = a + ((b & c) | (~b & d)) + x;
312 return t << s | t >>> (32 - s);
313 }
314
315 private int GG(int a, int b, int c, int d, int x, int s) {
316 int t = a + ((b & (c | d)) | (c & d)) + x + 0x5A827999;
317 return t << s | t >>> (32 - s);
318 }
319
320 private int HH(int a, int b, int c, int d, int x, int s) {
321 int t = a + (b ^ c ^ d) + x + 0x6ED9EBA1;
322 return t << s | t >>> (32 - s);
323 }
324 }