1 package org.apache.java.security;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 /***
58 * This class implements the Message Digest 5 algorithm (MD5) as
59 * defined in RFC-1321.
60 *
61 * <p><b>Note:</b> even if standard Java 1.1 APIs already provide a
62 * MD5 implementation, this class is used on those Java runtime
63 * environments (like Kaffe) where the package
64 * <code>java.security</code> is highly improbable to be found.
65 *
66 * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
67 * @version $Id: MD5.java,v 1.1.1.1 2001/08/16 05:08:27 jvanzyl Exp $
68 */
69 public final class MD5
70 extends MessageDigest
71 {
72 private long counter;
73 private int reminder;
74 private byte buffer[];
75 private int state[];
76 private int x[];
77
78 /************************ MD5 Functions ***********************/
79
80 // 16 * 4 bytes
81 static byte padding[] =
82 {
83 (byte) 0x80,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
88 };
89
90 /************************ Self Test ***********************/
91
92 private static String[] messages =
93 {
94 "",
95 "a",
96 "abc",
97 "message digest",
98 "abcdefghijklmnopqrstuvwxyz",
99 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
100 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
101 };
102
103 private static String[] digests =
104 {
105 "d41d8cd98f00b204e9800998ecf8427e",
106 "0cc175b9c0f1b6a831c399e269772661",
107 "900150983cd24fb0d6963f7d28e17f72",
108 "f96b697d7cb7938d525a2f31aaf161d0",
109 "c3fcd3d76192e4007dfb496cca67e13b",
110 "d174ab98d277d9f5a5611c2c9f419d9f",
111 "57edf4a22be3c955ac49da2e2107b67a",
112 };
113
114
115 /***
116 * Creates the algorithm and reset its state.
117 */
118 public MD5()
119 {
120 super();
121 }
122
123 /***
124 * Append another block of specified length to the message
125 * starting at the given offset.
126 *
127 * @param block A byte[].
128 * @param offset An int.
129 * @param length An int.
130 */
131 public void append(byte[] block,
132 int offset,
133 int length)
134 {
135 while (true)
136 {
137 if (length >= reminder)
138 {
139 System.arraycopy(block, offset, buffer,
140 (int) (counter & 63L), reminder);
141 transform(buffer);
142 counter += reminder;
143 offset += reminder;
144 length -= reminder;
145 reminder = 64;
146 }
147 else
148 {
149 System.arraycopy(block, offset, buffer,
150 (int) (counter & 63L), length);
151 counter += length;
152 reminder -= length;
153 break;
154 }
155 }
156 }
157
158 /************************ Byte/Int utilities ***********************/
159
160 /***
161 * Converts a 64-byte array into a 16-int array.
162 *
163 * @param in A byte[].
164 * @param out An int[].
165 */
166 private static void byte2int(byte[] in,
167 int[] out)
168 {
169 for (int inpos = 0, outpos = 0; outpos < 16; outpos++)
170 {
171 out[outpos] = ((((int) (in[inpos++] & 0xff))) |
172 (((int) (in[inpos++] & 0xff)) << 8) |
173 (((int) (in[inpos++] & 0xff)) << 16) |
174 (((int) (in[inpos++] & 0xff)) << 24));
175 }
176 }
177
178 /***
179 * Appends a message block with specified length starting from the
180 * given offset, and return its message digest.
181 *
182 * @param block A byte[].
183 * @param offset An int.
184 * @param length An int.
185 */
186 public byte[] digest(byte[] block,
187 int offset,
188 int length)
189 {
190 this.append(block, offset, length);
191
192 byte[] bits = toBytes(counter << 3);
193 byte[] digest = new byte[16];
194
195 if (reminder > 8)
196 {
197 append(padding, 0, reminder - 8);
198 }
199 else
200 {
201 append(padding, 0, 64 + (reminder - 8));
202 }
203
204 append(bits, 0, 8);
205
206 int2byte(state, digest);
207
208 this.reset();
209 return digest;
210 }
211
212 /*
213 * Method F.
214 *
215 * @param x An int.
216 * @param y An int.
217 * @param z An int.
218 * @return An int.
219 */
220 static private int F(int x,
221 int y,
222 int z)
223 {
224 return (z ^ (x & (y^z)));
225 }
226
227 /*
228 * Method FF.
229 *
230 * @param a An int.
231 * @param b An int.
232 * @param c An int.
233 * @param d An int.
234 * @param x An int.
235 * @param s An int.
236 * @param ac An int.
237 * @return An int.
238 */
239 static private int FF(int a,
240 int b,
241 int c,
242 int d,
243 int x,
244 int s,
245 int ac)
246 {
247 a += x + ac + F(b,c,d);
248 a = (a << s | a >>> -s);
249 return a + b;
250 }
251
252 /*
253 * Method G.
254 *
255 * @param x An int.
256 * @param y An int.
257 * @param z An int.
258 * @return An int.
259 */
260 static private int G(int x,
261 int y,
262 int z)
263 {
264 return (y ^ (z & (x^y)));
265 }
266
267 /*
268 * Method GG.
269 *
270 * @param a An int.
271 * @param b An int.
272 * @param c An int.
273 * @param d An int.
274 * @param x An int.
275 * @param s An int.
276 * @param ac An int.
277 * @return An int.
278 */
279 static private int GG(int a,
280 int b,
281 int c,
282 int d,
283 int x,
284 int s,
285 int ac)
286 {
287 a += x + ac + G(b,c,d);
288 a = (a << s | a >>> -s);
289 return a + b;
290 }
291
292 /*
293 * Method H.
294 *
295 * @param x An int.
296 * @param y An int.
297 * @param z An int.
298 * @return An int.
299 */
300 static private int H(int x,
301 int y,
302 int z)
303 {
304 return (x ^ y ^ z);
305 }
306
307 /*
308 * Method HH.
309 *
310 * @param a An int.
311 * @param b An int.
312 * @param c An int.
313 * @param d An int.
314 * @param x An int.
315 * @param s An int.
316 * @param ac An int.
317 * @return An int.
318 */
319 static private int HH(int a,
320 int b,
321 int c,
322 int d,
323 int x,
324 int s,
325 int ac)
326 {
327 a += x + ac + H(b,c,d);
328 a = (a << s | a >>> -s);
329 return a + b;
330 }
331
332 /*
333 * Method I.
334 *
335 * @param x An int.
336 * @param y An int.
337 * @param z An int.
338 * @return An int.
339 */
340 static private int I(int x,
341 int y,
342 int z)
343 {
344 return (y ^ (x | ~z));
345 }
346
347 /*
348 * Method II.
349 *
350 * @param a An int.
351 * @param b An int.
352 * @param c An int.
353 * @param d An int.
354 * @param x An int.
355 * @param s An int.
356 * @param ac An int.
357 * @return An int.
358 */
359 static private int II(int a,
360 int b,
361 int c,
362 int d,
363 int x,
364 int s,
365 int ac)
366 {
367 a += x + ac + I(b,c,d);
368 a = (a << s | a >>> -s);
369 return a + b;
370 }
371
372 /***
373 * Converts a 4-int array into a 16-byte array.
374 *
375 * @param in An int[].
376 * @param out A byte[].
377 */
378 private static void int2byte(int[] in,
379 byte[] out)
380 {
381 for (int inpos = 0, outpos = 0; inpos < 4; inpos++)
382 {
383 out[outpos++] = (byte) (in[inpos] & 0xff);
384 out[outpos++] = (byte) ((in[inpos] >>> 8) & 0xff);
385 out[outpos++] = (byte) ((in[inpos] >>> 16) & 0xff);
386 out[outpos++] = (byte) ((in[inpos] >>> 24) & 0xff);
387 }
388 }
389
390 /*
391 * Main routine, for testing purposes only.
392 *
393 * @param ignored A String[] with the command line arguments.
394 */
395 public static final void main(String[] ignored)
396 {
397 MD5 md5 = new MD5();
398
399 for (int i = 0; i < messages.length; i++)
400 {
401 String digest = org.apache.java.lang.Bytes.toString(
402 md5.digest(messages[i].getBytes()));
403 System.out.println("Computed: " + digest);
404 System.out.println("Correct: " + digests[i]);
405 if (digest.equalsIgnoreCase(digests[i]))
406 {
407 System.out.println("Test " + i + " passed.");
408 }
409 else
410 {
411 System.out.println("Test " + i + " failed.");
412 }
413 }
414 }
415
416 /***
417 * Resets the state of the class. <b>Beware</b>: calling this
418 * method erases all data previously inserted.
419 */
420 public void reset()
421 {
422 buffer = new byte[64];
423 state = new int[4];
424 x = new int[16];
425
426 state[0] = 0x67452301;
427 state[1] = 0xefcdab89;
428 state[2] = 0x98badcfe;
429 state[3] = 0x10325476;
430
431 counter = 0;
432 reminder = 64;
433 }
434
435 /***
436 * Converts a long to a 8-byte array using low order first.
437 *
438 * @param n A long.
439 * @return A byte[].
440 */
441 public static byte[] toBytes(long n)
442 {
443 byte[] b = new byte[8];
444
445 b[0] = (byte) (n);
446 n >>>= 8;
447 b[1] = (byte) (n);
448 n >>>= 8;
449 b[2] = (byte) (n);
450 n >>>= 8;
451 b[3] = (byte) (n);
452 n >>>= 8;
453 b[4] = (byte) (n);
454 n >>>= 8;
455 b[5] = (byte) (n);
456 n >>>= 8;
457 b[6] = (byte) (n);
458 n >>>= 8;
459 b[7] = (byte) (n);
460
461 return b;
462 }
463
464 /*
465 * TODO: Document.
466 *
467 * @param buffer A byte[].
468 */
469 private void transform(byte[] buffer)
470 {
471 int a, b, c, d;
472
473 byte2int(buffer, x);
474
475 a = state[0];
476 b = state[1];
477 c = state[2];
478 d = state[3];
479
480 a = FF(a, b, c, d, x[ 0], 7, 0xd76aa478);
481 d = FF(d, a, b, c, x[ 1], 12, 0xe8c7b756);
482 c = FF(c, d, a, b, x[ 2], 17, 0x242070db);
483 b = FF(b, c, d, a, x[ 3], 22, 0xc1bdceee);
484 a = FF(a, b, c, d, x[ 4], 7, 0xf57c0faf);
485 d = FF(d, a, b, c, x[ 5], 12, 0x4787c62a);
486 c = FF(c, d, a, b, x[ 6], 17, 0xa8304613);
487 b = FF(b, c, d, a, x[ 7], 22, 0xfd469501);
488 a = FF(a, b, c, d, x[ 8], 7, 0x698098d8);
489 d = FF(d, a, b, c, x[ 9], 12, 0x8b44f7af);
490 c = FF(c, d, a, b, x[10], 17, 0xffff5bb1);
491 b = FF(b, c, d, a, x[11], 22, 0x895cd7be);
492 a = FF(a, b, c, d, x[12], 7, 0x6b901122);
493 d = FF(d, a, b, c, x[13], 12, 0xfd987193);
494 c = FF(c, d, a, b, x[14], 17, 0xa679438e);
495 b = FF(b, c, d, a, x[15], 22, 0x49b40821);
496
497 a = GG(a, b, c, d, x[ 1], 5, 0xf61e2562);
498 d = GG(d, a, b, c, x[ 6], 9, 0xc040b340);
499 c = GG(c, d, a, b, x[11], 14, 0x265e5a51);
500 b = GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa);
501 a = GG(a, b, c, d, x[ 5], 5, 0xd62f105d);
502 d = GG(d, a, b, c, x[10], 9, 0x2441453);
503 c = GG(c, d, a, b, x[15], 14, 0xd8a1e681);
504 b = GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8);
505 a = GG(a, b, c, d, x[ 9], 5, 0x21e1cde6);
506 d = GG(d, a, b, c, x[14], 9, 0xc33707d6);
507 c = GG(c, d, a, b, x[ 3], 14, 0xf4d50d87);
508 b = GG(b, c, d, a, x[ 8], 20, 0x455a14ed);
509 a = GG(a, b, c, d, x[13], 5, 0xa9e3e905);
510 d = GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8);
511 c = GG(c, d, a, b, x[ 7], 14, 0x676f02d9);
512 b = GG(b, c, d, a, x[12], 20, 0x8d2a4c8a);
513
514 a = HH(a, b, c, d, x[ 5], 4, 0xfffa3942);
515 d = HH(d, a, b, c, x[ 8], 11, 0x8771f681);
516 c = HH(c, d, a, b, x[11], 16, 0x6d9d6122);
517 b = HH(b, c, d, a, x[14], 23, 0xfde5380c);
518 a = HH(a, b, c, d, x[ 1], 4, 0xa4beea44);
519 d = HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9);
520 c = HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60);
521 b = HH(b, c, d, a, x[10], 23, 0xbebfbc70);
522 a = HH(a, b, c, d, x[13], 4, 0x289b7ec6);
523 d = HH(d, a, b, c, x[ 0], 11, 0xeaa127fa);
524 c = HH(c, d, a, b, x[ 3], 16, 0xd4ef3085);
525 b = HH(b, c, d, a, x[ 6], 23, 0x4881d05);
526 a = HH(a, b, c, d, x[ 9], 4, 0xd9d4d039);
527 d = HH(d, a, b, c, x[12], 11, 0xe6db99e5);
528 c = HH(c, d, a, b, x[15], 16, 0x1fa27cf8);
529 b = HH(b, c, d, a, x[ 2], 23, 0xc4ac5665);
530
531 a = II(a, b, c, d, x[ 0], 6, 0xf4292244);
532 d = II(d, a, b, c, x[ 7], 10, 0x432aff97);
533 c = II(c, d, a, b, x[14], 15, 0xab9423a7);
534 b = II(b, c, d, a, x[ 5], 21, 0xfc93a039);
535 a = II(a, b, c, d, x[12], 6, 0x655b59c3);
536 d = II(d, a, b, c, x[ 3], 10, 0x8f0ccc92);
537 c = II(c, d, a, b, x[10], 15, 0xffeff47d);
538 b = II(b, c, d, a, x[ 1], 21, 0x85845dd1);
539 a = II(a, b, c, d, x[ 8], 6, 0x6fa87e4f);
540 d = II(d, a, b, c, x[15], 10, 0xfe2ce6e0);
541 c = II(c, d, a, b, x[ 6], 15, 0xa3014314);
542 b = II(b, c, d, a, x[13], 21, 0x4e0811a1);
543 a = II(a, b, c, d, x[ 4], 6, 0xf7537e82);
544 d = II(d, a, b, c, x[11], 10, 0xbd3af235);
545 c = II(c, d, a, b, x[ 2], 15, 0x2ad7d2bb);
546 b = II(b, c, d, a, x[ 9], 21, 0xeb86d391);
547
548 state[0] += a;
549 state[1] += b;
550 state[2] += c;
551 state[3] += d;
552 }
553 }
This page was automatically generated by Maven