-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmd.java
410 lines (383 loc) · 13.1 KB
/
md.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* Class for implementing md4 and md5 hash algorithms.
* There are constructors for prepping the hash algorithm (doing the
* padding, mainly) for a String or a byte[], and an mdcalc() method
* for generating the hash. The results can be accessed as an int array
* by getregs(), or as a String of hex digits with toString().
*
* Written for jotp, by Harry Mantakos [email protected]
*
* Feel free to do whatever you like with this code.
* If you do modify or use this code in another application,
* I'd be interested in hearing from you!
*/
class md4 extends md {
md4(String s) {
super(s);
}
md4(byte in[]) {
super(in);
}
static int F(int x, int y, int z) {
return((x & y) | (~x & z));
}
static int G(int x, int y, int z) {
return((x & y) | (x & z) | (y & z));
}
static int H(int x, int y, int z) {
return(x ^ y ^ z);
}
void round1(int blk) {
A = rotintlft((A + F(B, C, D) + d[0 + 16 * blk]), 3);
D = rotintlft((D + F(A, B, C) + d[1 + 16 * blk]), 7);
C = rotintlft((C + F(D, A, B) + d[2 + 16 * blk]), 11);
B = rotintlft((B + F(C, D, A) + d[3 + 16 * blk]), 19);
A = rotintlft((A + F(B, C, D) + d[4 + 16 * blk]), 3);
D = rotintlft((D + F(A, B, C) + d[5 + 16 * blk]), 7);
C = rotintlft((C + F(D, A, B) + d[6 + 16 * blk]), 11);
B = rotintlft((B + F(C, D, A) + d[7 + 16 * blk]), 19);
A = rotintlft((A + F(B, C, D) + d[8 + 16 * blk]), 3);
D = rotintlft((D + F(A, B, C) + d[9 + 16 * blk]), 7);
C = rotintlft((C + F(D, A, B) + d[10 + 16 * blk]), 11);
B = rotintlft((B + F(C, D, A) + d[11 + 16 * blk]), 19);
A = rotintlft((A + F(B, C, D) + d[12 + 16 * blk]), 3);
D = rotintlft((D + F(A, B, C) + d[13 + 16 * blk]), 7);
C = rotintlft((C + F(D, A, B) + d[14 + 16 * blk]), 11);
B = rotintlft((B + F(C, D, A) + d[15 + 16 * blk]), 19);
}
void round2(int blk) {
A = rotintlft((A + G(B, C, D) + d[0 + 16 * blk] + 0x5a827999), 3);
D = rotintlft((D + G(A, B, C) + d[4 + 16 * blk] + 0x5a827999), 5);
C = rotintlft((C + G(D, A, B) + d[8 + 16 * blk] + 0x5a827999), 9);
B = rotintlft((B + G(C, D, A) + d[12 + 16 * blk] + 0x5a827999), 13);
A = rotintlft((A + G(B, C, D) + d[1 + 16 * blk] + 0x5a827999), 3);
D = rotintlft((D + G(A, B, C) + d[5 + 16 * blk] + 0x5a827999), 5);
C = rotintlft((C + G(D, A, B) + d[9 + 16 * blk] + 0x5a827999), 9);
B = rotintlft((B + G(C, D, A) + d[13 + 16 * blk] + 0x5a827999), 13);
A = rotintlft((A + G(B, C, D) + d[2 + 16 * blk] + 0x5a827999), 3);
D = rotintlft((D + G(A, B, C) + d[6 + 16 * blk] + 0x5a827999), 5);
C = rotintlft((C + G(D, A, B) + d[10 + 16 * blk] + 0x5a827999), 9);
B = rotintlft((B + G(C, D, A) + d[14 + 16 * blk] + 0x5a827999), 13);
A = rotintlft((A + G(B, C, D) + d[3 + 16 * blk] + 0x5a827999), 3);
D = rotintlft((D + G(A, B, C) + d[7 + 16 * blk] + 0x5a827999), 5);
C = rotintlft((C + G(D, A, B) + d[11 + 16 * blk] + 0x5a827999), 9);
B = rotintlft((B + G(C, D, A) + d[15 + 16 * blk] + 0x5a827999), 13);
}
void round3(int blk) {
A = rotintlft((A + H(B, C, D) + d[0 + 16 * blk] + 0x6ed9eba1), 3);
D = rotintlft((D + H(A, B, C) + d[8 + 16 * blk] + 0x6ed9eba1), 9);
C = rotintlft((C + H(D, A, B) + d[4 + 16 * blk] + 0x6ed9eba1), 11);
B = rotintlft((B + H(C, D, A) + d[12 + 16 * blk] + 0x6ed9eba1), 15);
A = rotintlft((A + H(B, C, D) + d[2 + 16 * blk] + 0x6ed9eba1), 3);
D = rotintlft((D + H(A, B, C) + d[10 + 16 * blk] + 0x6ed9eba1), 9);
C = rotintlft((C + H(D, A, B) + d[6 + 16 * blk] + 0x6ed9eba1), 11);
B = rotintlft((B + H(C, D, A) + d[14 + 16 * blk] + 0x6ed9eba1), 15);
A = rotintlft((A + H(B, C, D) + d[1 + 16 * blk] + 0x6ed9eba1), 3);
D = rotintlft((D + H(A, B, C) + d[9 + 16 * blk] + 0x6ed9eba1), 9);
C = rotintlft((C + H(D, A, B) + d[5 + 16 * blk] + 0x6ed9eba1), 11);
B = rotintlft((B + H(C, D, A) + d[13 + 16 * blk] + 0x6ed9eba1), 15);
A = rotintlft((A + H(B, C, D) + d[3 + 16 * blk] + 0x6ed9eba1), 3);
D = rotintlft((D + H(A, B, C) + d[11 + 16 * blk] + 0x6ed9eba1), 9);
C = rotintlft((C + H(D, A, B) + d[7 + 16 * blk] + 0x6ed9eba1), 11);
B = rotintlft((B + H(C, D, A) + d[15 + 16 * blk] + 0x6ed9eba1), 15);
}
void round4(int blk) {
System.out.println(" must be md5, in round4!");
}
}
class md5 extends md {
md5(String s) {
super(s);
}
md5(byte in[]) {
super(in);
}
static int F(int x, int y, int z) {
return((x & y) | (~x & z));
}
static int G(int x, int y, int z) {
return((x & z) | (y & ~z));
}
static int H(int x, int y, int z) {
return(x ^ y ^ z);
}
static int I(int x, int y, int z) {
return(y ^ (x | ~z));
}
void round1(int blk) {
A = rotintlft(A + F(B, C, D) + d[0 + 16 * blk] +
0xd76aa478, 7) + B;
D = rotintlft(D + F(A, B, C) + d[1 + 16 * blk] +
0xe8c7b756, 12) + A;
C = rotintlft(C + F(D, A, B) + d[2 + 16 * blk] +
0x242070db, 17) + D;
B = rotintlft(B + F(C, D, A) + d[3 + 16 * blk] +
0xc1bdceee, 22) + C;
A = rotintlft(A + F(B, C, D) + d[4 + 16 * blk] +
0xf57c0faf, 7) + B;
D = rotintlft(D + F(A, B, C) + d[5 + 16 * blk] +
0x4787c62a, 12) + A;
C = rotintlft(C + F(D, A, B) + d[6 + 16 * blk] +
0xa8304613, 17) + D;
B = rotintlft(B + F(C, D, A) + d[7 + 16 * blk] +
0xfd469501, 22) + C;
A = rotintlft(A + F(B, C, D) + d[8 + 16 * blk] +
0x698098d8, 7) + B;
D = rotintlft(D + F(A, B, C) + d[9 + 16 * blk] +
0x8b44f7af, 12) + A;
C = rotintlft(C + F(D, A, B) + d[10 + 16 * blk] +
0xffff5bb1, 17) + D;
B = rotintlft(B + F(C, D, A) + d[11 + 16 * blk] +
0x895cd7be, 22) + C;
A = rotintlft(A + F(B, C, D) + d[12 + 16 * blk] +
0x6b901122, 7) + B;
D = rotintlft(D + F(A, B, C) + d[13 + 16 * blk] +
0xfd987193, 12) + A;
C = rotintlft(C + F(D, A, B) + d[14 + 16 * blk] +
0xa679438e, 17) + D;
B = rotintlft(B + F(C, D, A) + d[15 + 16 * blk] +
0x49b40821, 22) + C;
}
void round2(int blk) {
A = rotintlft(A + G(B, C, D) + d[1 + 16 * blk] +
0xf61e2562, 5) + B;
D = rotintlft(D + G(A, B, C) + d[6 + 16 * blk] +
0xc040b340, 9) + A;
C = rotintlft(C + G(D, A, B) + d[11 + 16 * blk] +
0x265e5a51, 14) + D;
B = rotintlft(B + G(C, D, A) + d[0 + 16 * blk] +
0xe9b6c7aa, 20) + C;
A = rotintlft(A + G(B, C, D) + d[5 + 16 * blk] +
0xd62f105d, 5) + B;
D = rotintlft(D + G(A, B, C) + d[10 + 16 * blk] +
0x02441453, 9) + A;
C = rotintlft(C + G(D, A, B) + d[15 + 16 * blk] +
0xd8a1e681, 14) + D;
B = rotintlft(B + G(C, D, A) + d[4 + 16 * blk] +
0xe7d3fbc8, 20) + C;
A = rotintlft(A + G(B, C, D) + d[9 + 16 * blk] +
0x21e1cde6, 5) + B;
D = rotintlft(D + G(A, B, C) + d[14 + 16 * blk] +
0xc33707d6, 9) + A;
C = rotintlft(C + G(D, A, B) + d[3 + 16 * blk] +
0xf4d50d87, 14) + D;
B = rotintlft(B + G(C, D, A) + d[8 + 16 * blk] +
0x455a14ed, 20) + C;
A = rotintlft(A + G(B, C, D) + d[13 + 16 * blk] +
0xa9e3e905, 5) + B;
D = rotintlft(D + G(A, B, C) + d[2 + 16 * blk] +
0xfcefa3f8, 9) + A;
C = rotintlft(C + G(D, A, B) + d[7 + 16 * blk] +
0x676f02d9, 14) + D;
B = rotintlft(B + G(C, D, A) + d[12 + 16 * blk] +
0x8d2a4c8a, 20) + C;
}
void round3(int blk) {
A = rotintlft(A + H(B, C, D) + d[5 + 16 * blk] +
0xfffa3942, 4) + B;
D = rotintlft(D + H(A, B, C) + d[8 + 16 * blk] +
0x8771f681, 11) + A;
C = rotintlft(C + H(D, A, B) + d[11 + 16 * blk] +
0x6d9d6122, 16) + D;
B = rotintlft(B + H(C, D, A) + d[14 + 16 * blk] +
0xfde5380c, 23) + C;
A = rotintlft(A + H(B, C, D) + d[1 + 16 * blk] +
0xa4beea44, 4) + B;
D = rotintlft(D + H(A, B, C) + d[4 + 16 * blk] +
0x4bdecfa9, 11) + A;
C = rotintlft(C + H(D, A, B) + d[7 + 16 * blk] +
0xf6bb4b60, 16) + D;
B = rotintlft(B + H(C, D, A) + d[10 + 16 * blk] +
0xbebfbc70, 23) + C;
A = rotintlft(A + H(B, C, D) + d[13 + 16 * blk] +
0x289b7ec6, 4) + B;
D = rotintlft(D + H(A, B, C) + d[0 + 16 * blk] +
0xeaa127fa, 11) + A;
C = rotintlft(C + H(D, A, B) + d[3 + 16 * blk] +
0xd4ef3085, 16) + D;
B = rotintlft(B + H(C, D, A) + d[6 + 16 * blk] +
0x04881d05, 23) + C;
A = rotintlft(A + H(B, C, D) + d[9 + 16 * blk] +
0xd9d4d039, 4) + B;
D = rotintlft(D + H(A, B, C) + d[12 + 16 * blk] +
0xe6db99e5, 11) + A;
C = rotintlft(C + H(D, A, B) + d[15 + 16 * blk] +
0x1fa27cf8, 16) + D;
B = rotintlft(B + H(C, D, A) + d[2 + 16 * blk] +
0xc4ac5665, 23) + C;
}
void round4(int blk) {
A = rotintlft(A + I(B, C, D) + d[0 + 16 * blk] +
0xf4292244, 6) + B;
D = rotintlft(D + I(A, B, C) + d[7 + 16 * blk] +
0x432aff97, 10) + A;
C = rotintlft(C + I(D, A, B) + d[14 + 16 * blk] +
0xab9423a7, 15) + D;
B = rotintlft(B + I(C, D, A) + d[5 + 16 * blk] +
0xfc93a039, 21) + C;
A = rotintlft(A + I(B, C, D) + d[12 + 16 * blk] +
0x655b59c3, 6) + B;
D = rotintlft(D + I(A, B, C) + d[3 + 16 * blk] +
0x8f0ccc92, 10) + A;
C = rotintlft(C + I(D, A, B) + d[10 + 16 * blk] +
0xffeff47d, 15) + D;
B = rotintlft(B + I(C, D, A) + d[1 + 16 * blk] +
0x85845dd1, 21) + C;
A = rotintlft(A + I(B, C, D) + d[8 + 16 * blk] +
0x6fa87e4f, 6) + B;
D = rotintlft(D + I(A, B, C) + d[15 + 16 * blk] +
0xfe2ce6e0, 10) + A;
C = rotintlft(C + I(D, A, B) + d[6 + 16 * blk] +
0xa3014314, 15) + D;
B = rotintlft(B + I(C, D, A) + d[13 + 16 * blk] +
0x4e0811a1, 21) + C;
A = rotintlft(A + I(B, C, D) + d[4 + 16 * blk] +
0xf7537e82, 6) + B;
D = rotintlft(D + I(A, B, C) + d[11 + 16 * blk] +
0xbd3af235, 10) + A;
C = rotintlft(C + I(D, A, B) + d[2 + 16 * blk] +
0x2ad7d2bb, 15) + D;
B = rotintlft(B + I(C, D, A) + d[9 + 16 * blk] +
0xeb86d391, 21) + C;
}
}
class md {
int A,B,C,D;
int d[];
int numwords;
/* For verification of a modicum of sanity, run a few
* test strings through
*/
public static void main(String argv[]) {
boolean doinmd4;
String mdtype;
/* Test cases, mostly taken from rfc 1320 */
String str[] = { "" , "a", "abc", "message digest",
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
"01234567890123456789012345678901234567890123456789012345"};
if (argv.length == 0) {
mdtype = "md4";
doinmd4 = true;
} else if (argv.length > 1) {
System.err.println("Usage: md [4|5|md4|md5]");
return;
} else if ((argv[0].equals("4")) || (argv[0].equals("md4"))) {
mdtype = "md4";
doinmd4 = true;
} else if ((argv[0].equals("5")) || (argv[0].equals("md5"))) {
mdtype = "md5";
doinmd4 = false;
} else {
System.err.println("Usage: md [4|5|md4|md5]");
return;
}
for(int i = 0; i < str.length; i++) {
if (doinmd4) {
md4 mdc = new md4(str[i]);
mdc.calc();
System.out.println(mdtype + "(\"" + str[i] + "\") = " + mdc);
} else {
md5 mdc = new md5(str[i]);
mdc.calc();
System.out.println(mdtype + "(\"" + str[i] + "\") = " + mdc);
}
}
}
md (String s) {
byte in[] = new byte[s.length()];
int i;
for(i=0; i < s.length(); i++) {
in[i] = (byte) (s.charAt(i) & 0xff);
}
mdinit(in);
}
md (byte in[]) {
mdinit(in);
}
void mdinit (byte in[]) {
int newlen, endblklen, pad, i;
long datalenbits;
datalenbits = in.length * 8;
endblklen = in.length % 64;
if (endblklen < 56) {
pad = 64 - endblklen;
} else {
pad = (64 - endblklen) + 64;
}
newlen = in.length + pad;
byte b[] = new byte[newlen];
for(i=0; i < in.length; i++) {
b[i] = in[i];
}
b[in.length] = (byte) 0x80;
for (i = b.length + 1; i < (newlen - 8); i++) {
b[i] = 0;
}
for (i = 0; i < 8; i++) {
b[newlen - 8 + i] = (byte) (datalenbits & 0xff);
datalenbits >>= 8;
}
/* init registers */
A = 0x67452301;
B = 0xefcdab89;
C = 0x98badcfe;
D = 0x10325476;
this.numwords = newlen/4;
this.d = new int[this.numwords];
for (i = 0; i < newlen; i += 4) {
this.d[i/4] = (b[i] & 0xff) + ((b[i+1] & 0xff) << 8) +
((b[i+2] & 0xff) << 16) + ((b[i+3] & 0xff) << 24);
}
}
public String toString() {
String s;
return(tohex(A) + tohex(B) + tohex(C) + tohex(D));
}
int[] getregs() {
int regs[] = {this.A, this.B, this.C, this.D};
return regs;
}
void calc() {
int AA, BB, CC, DD, i;
for(i=0; i < numwords/16; i++) {
AA = A; BB = B; CC = C; DD = D;
round1(i);
round2(i);
round3(i);
if (this instanceof md5) {
round4(i);
}
A += AA; B+= BB; C+= CC; D+= DD;
}
}
/* Dummy round*() methods. these are overriden in the md4 and md5
* subclasses
*/
void round1(int blk) {
System.err.println("Danger! Danger! Someone called md.round1()!");
}
void round2(int blk) {
System.err.println("Danger! Danger! Someone called md.round2()!");
}
void round3(int blk) {
System.err.println("Danger! Danger! Someone called md.round3()!");
}
void round4(int blk) {
System.err.println("Danger! Danger! Someone called md.round4()!");
}
static int rotintlft(int val, int numbits) {
return((val << numbits) | (val >>> (32 - numbits)));
}
static String tohex(int i) {
int b;
String tmpstr;
tmpstr = "";
for(b = 0; b < 4; b++) {
tmpstr += Integer.toString((i >> 4) & 0xf, 16)
+ Integer.toString(i & 0xf, 16);
i >>= 8;
}
return tmpstr;
}
}