001: /*
002: * $Header: /cvsroot/skunkdav/skunkdav/HTTPClient/MD5.java,v 1.2 2000/12/18 18:15:44 smulloni Exp $
003: *
004: * MD5 in Java JDK Beta-2
005: * written Santeri Paavolainen, Helsinki Finland 1996
006: * (c) Santeri Paavolainen, Helsinki Finland 1996
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: *
023: * See http://www.cs.hut.fi/~santtu/java/ for more information on this
024: * class.
025: *
026: * This is rather straight re-implementation of the reference implementation
027: * given in RFC1321 by RSA.
028: *
029: * Passes MD5 test suite as defined in RFC1321.
030: *
031: *
032: * This Java class has been derived from the RSA Data Security, Inc. MD5
033: * Message-Digest Algorithm and its reference implementation.
034: *
035: *
036: * $Log: MD5.java,v $
037: * Revision 1.2 2000/12/18 18:15:44 smulloni
038: * earlier checkin of HTTPClient library contained the logging extensions
039: * Joachim Feise added for the DAVExplorer project. These have now been
040: * removed.
041: *
042: * Revision 1.6 1998/09/22 18:34:00 ronald
043: * Made MD5State a private inner class
044: *
045: * Revision 1.5 1998/03/23 02:30:00 ronald
046: * Optimized: removed uadd()'s (were unnecessary)
047: * made FF(), GG(), HH(), and II() static and final (allows inlining)
048: *
049: * Revision 1.4 1997/01/12 00:05:54 ronald
050: * Made part of HTTPClient package.
051: *
052: * Revision 1.3 1996/04/15 07:28:09 santtu
053: * Added GPL statemets, and RSA derivate statements.
054: *
055: * Revision 1.2 1996/03/04 08:05:48 santtu
056: * Added offsets to Update method
057: *
058: * Revision 1.1 1996/01/07 20:51:59 santtu
059: * Initial revision
060: *
061: */
062:
063: package HTTPClient;
064:
065: /**
066: * Implementation of RSA's MD5 hash generator
067: *
068: * @version 0.3-2 18/06/1999
069: * @author Santeri Paavolainen <sjpaavol@cc.helsinki.fi>
070: */
071:
072: class MD5 {
073: /**
074: * MD5 state
075: */
076: MD5State state;
077:
078: /**
079: * If Final() has been called, finals is set to the current finals
080: * state. Any Update() causes this to be set to null.
081: */
082: MD5State finals;
083:
084: /**
085: * Padding for Final()
086: */
087: static byte padding[] = { (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0,
088: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
089: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
090: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
091:
092: /**
093: * Initialize MD5 internal state (object can be reused just by
094: * calling Init() after every Final()
095: */
096: public synchronized void Init() {
097: state = new MD5State();
098: finals = null;
099: }
100:
101: /**
102: * Class constructor
103: */
104: public MD5() {
105: this .Init();
106: }
107:
108: /**
109: * Initialize class, and update hash with ob.toString()
110: *
111: * @param ob Object, ob.toString() is used to update hash
112: * after initialization
113: */
114: public MD5(Object ob) {
115: this ();
116: Update(ob.toString());
117: }
118:
119: private static final int rotate_left(int x, int n) {
120: return (x << n) | (x >>> (32 - n));
121: }
122:
123: private static final int FF(int a, int b, int c, int d, int x,
124: int s, int ac) {
125: a += ((b & c) | (~b & d)) + x + ac;
126: return rotate_left(a, s) + b;
127: }
128:
129: private static final int GG(int a, int b, int c, int d, int x,
130: int s, int ac) {
131: a += ((b & d) | (c & ~d)) + x + ac;
132: return rotate_left(a, s) + b;
133: }
134:
135: private static final int HH(int a, int b, int c, int d, int x,
136: int s, int ac) {
137: a += (b ^ c ^ d) + x + ac;
138: return rotate_left(a, s) + b;
139: }
140:
141: private static final int II(int a, int b, int c, int d, int x,
142: int s, int ac) {
143: a += (c ^ (b | ~d)) + x + ac;
144: return rotate_left(a, s) + b;
145: }
146:
147: private static final int[] Decode(byte buffer[], int len, int shift) {
148: int out[];
149: int i, j;
150:
151: out = new int[16];
152:
153: for (i = j = 0; j < len; i++, j += 4) {
154: out[i] = ((int) (buffer[j + shift] & 0xff))
155: | (((int) (buffer[j + 1 + shift] & 0xff)) << 8)
156: | (((int) (buffer[j + 2 + shift] & 0xff)) << 16)
157: | (((int) (buffer[j + 3 + shift] & 0xff)) << 24);
158:
159: /* System.out.println("out[" + i + "] = \t" +
160: ((int) buffer[j + 0 + shift] & 0xff) + "\t|\t" +
161: ((int) buffer[j + 1 + shift] & 0xff) + "\t|\t" +
162: ((int) buffer[j + 2 + shift] & 0xff) + "\t|\t" +
163: ((int) buffer[j + 3 + shift] & 0xff));*/
164: }
165:
166: return out;
167: }
168:
169: private void Transform(MD5State state, byte buffer[], int shift) {
170: int a = state.state[0], b = state.state[1], c = state.state[2], d = state.state[3], x[];
171:
172: x = Decode(buffer, 64, shift);
173:
174: /* Round 1 */
175: a = FF(a, b, c, d, x[0], 7, 0xd76aa478); /* 1 */
176: d = FF(d, a, b, c, x[1], 12, 0xe8c7b756); /* 2 */
177: c = FF(c, d, a, b, x[2], 17, 0x242070db); /* 3 */
178: b = FF(b, c, d, a, x[3], 22, 0xc1bdceee); /* 4 */
179: a = FF(a, b, c, d, x[4], 7, 0xf57c0faf); /* 5 */
180: d = FF(d, a, b, c, x[5], 12, 0x4787c62a); /* 6 */
181: c = FF(c, d, a, b, x[6], 17, 0xa8304613); /* 7 */
182: b = FF(b, c, d, a, x[7], 22, 0xfd469501); /* 8 */
183: a = FF(a, b, c, d, x[8], 7, 0x698098d8); /* 9 */
184: d = FF(d, a, b, c, x[9], 12, 0x8b44f7af); /* 10 */
185: c = FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
186: b = FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
187: a = FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
188: d = FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
189: c = FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
190: b = FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
191:
192: /* Round 2 */
193: a = GG(a, b, c, d, x[1], 5, 0xf61e2562); /* 17 */
194: d = GG(d, a, b, c, x[6], 9, 0xc040b340); /* 18 */
195: c = GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
196: b = GG(b, c, d, a, x[0], 20, 0xe9b6c7aa); /* 20 */
197: a = GG(a, b, c, d, x[5], 5, 0xd62f105d); /* 21 */
198: d = GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */
199: c = GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
200: b = GG(b, c, d, a, x[4], 20, 0xe7d3fbc8); /* 24 */
201: a = GG(a, b, c, d, x[9], 5, 0x21e1cde6); /* 25 */
202: d = GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
203: c = GG(c, d, a, b, x[3], 14, 0xf4d50d87); /* 27 */
204: b = GG(b, c, d, a, x[8], 20, 0x455a14ed); /* 28 */
205: a = GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
206: d = GG(d, a, b, c, x[2], 9, 0xfcefa3f8); /* 30 */
207: c = GG(c, d, a, b, x[7], 14, 0x676f02d9); /* 31 */
208: b = GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
209:
210: /* Round 3 */
211: a = HH(a, b, c, d, x[5], 4, 0xfffa3942); /* 33 */
212: d = HH(d, a, b, c, x[8], 11, 0x8771f681); /* 34 */
213: c = HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
214: b = HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
215: a = HH(a, b, c, d, x[1], 4, 0xa4beea44); /* 37 */
216: d = HH(d, a, b, c, x[4], 11, 0x4bdecfa9); /* 38 */
217: c = HH(c, d, a, b, x[7], 16, 0xf6bb4b60); /* 39 */
218: b = HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
219: a = HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
220: d = HH(d, a, b, c, x[0], 11, 0xeaa127fa); /* 42 */
221: c = HH(c, d, a, b, x[3], 16, 0xd4ef3085); /* 43 */
222: b = HH(b, c, d, a, x[6], 23, 0x4881d05); /* 44 */
223: a = HH(a, b, c, d, x[9], 4, 0xd9d4d039); /* 45 */
224: d = HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
225: c = HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
226: b = HH(b, c, d, a, x[2], 23, 0xc4ac5665); /* 48 */
227:
228: /* Round 4 */
229: a = II(a, b, c, d, x[0], 6, 0xf4292244); /* 49 */
230: d = II(d, a, b, c, x[7], 10, 0x432aff97); /* 50 */
231: c = II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
232: b = II(b, c, d, a, x[5], 21, 0xfc93a039); /* 52 */
233: a = II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
234: d = II(d, a, b, c, x[3], 10, 0x8f0ccc92); /* 54 */
235: c = II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
236: b = II(b, c, d, a, x[1], 21, 0x85845dd1); /* 56 */
237: a = II(a, b, c, d, x[8], 6, 0x6fa87e4f); /* 57 */
238: d = II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
239: c = II(c, d, a, b, x[6], 15, 0xa3014314); /* 59 */
240: b = II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
241: a = II(a, b, c, d, x[4], 6, 0xf7537e82); /* 61 */
242: d = II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
243: c = II(c, d, a, b, x[2], 15, 0x2ad7d2bb); /* 63 */
244: b = II(b, c, d, a, x[9], 21, 0xeb86d391); /* 64 */
245:
246: state.state[0] += a;
247: state.state[1] += b;
248: state.state[2] += c;
249: state.state[3] += d;
250: }
251:
252: /**
253: * Updates hash with the bytebuffer given (using at maximum length bytes from
254: * that buffer)
255: *
256: * @param state Which state is updated
257: * @param buffer Array of bytes to be hashed
258: * @param offset Offset to buffer array
259: * @param length Use at maximum `length' bytes (absolute
260: * maximum is buffer.length)
261: */
262: public void Update(MD5State stat, byte buffer[], int offset,
263: int length) {
264: int index, partlen, i, start;
265:
266: /* System.out.print("Offset = " + offset + "\tLength = " + length + "\t");
267: System.out.print("Buffer = ");
268: for (i = 0; i < buffer.length; i++)
269: System.out.print((int) (buffer[i] & 0xff) + " ");
270: System.out.print("\n");*/
271:
272: finals = null;
273:
274: /* Length can be told to be shorter, but not inter */
275: if ((length - offset) > buffer.length)
276: length = buffer.length - offset;
277:
278: /* compute number of bytes mod 64 */
279: index = (int) (stat.count[0] >>> 3) & 0x3f;
280:
281: if ((stat.count[0] += (length << 3)) < (length << 3))
282: stat.count[1]++;
283:
284: stat.count[1] += length >>> 29;
285:
286: partlen = 64 - index;
287:
288: if (length >= partlen) {
289: for (i = 0; i < partlen; i++)
290: stat.buffer[i + index] = buffer[i + offset];
291:
292: Transform(stat, stat.buffer, 0);
293:
294: for (i = partlen; (i + 63) < length; i += 64)
295: Transform(stat, buffer, i);
296:
297: index = 0;
298: } else
299: i = 0;
300:
301: /* buffer remaining input */
302: if (i < length) {
303: start = i;
304: for (; i < length; i++)
305: stat.buffer[index + i - start] = buffer[i + offset];
306: }
307: }
308:
309: /*
310: * Update()s for other datatypes than byte[] also. Update(byte[], int)
311: * is only the main driver.
312: */
313:
314: /**
315: * Plain update, updates this object
316: */
317:
318: public void Update(byte buffer[], int offset, int length) {
319: Update(this .state, buffer, offset, length);
320: }
321:
322: public void Update(byte buffer[], int length) {
323: Update(this .state, buffer, 0, length);
324: }
325:
326: /**
327: * Updates hash with given array of bytes
328: *
329: * @param buffer Array of bytes to use for updating the hash
330: */
331: public void Update(byte buffer[]) {
332: Update(buffer, 0, buffer.length);
333: }
334:
335: /**
336: * Updates hash with a single byte
337: *
338: * @param b Single byte to update the hash
339: */
340: public void Update(byte b) {
341: byte buffer[] = new byte[1];
342: buffer[0] = b;
343:
344: Update(buffer, 1);
345: }
346:
347: /**
348: * Update buffer with given string.
349: *
350: * @param s String to be update to hash (is used as
351: * s.getBytes())
352: */
353: public void Update(String s) {
354: byte chars[];
355:
356: chars = new byte[s.length()];
357: s.getBytes(0, s.length(), chars, 0);
358:
359: Update(chars, chars.length);
360: }
361:
362: /**
363: * Update buffer with a single integer (only & 0xff part is used,
364: * as a byte)
365: *
366: * @param i Integer value, which is then converted to
367: * byte as i & 0xff
368: */
369:
370: public void Update(int i) {
371: Update((byte) (i & 0xff));
372: }
373:
374: private byte[] Encode(int input[], int len) {
375: int i, j;
376: byte out[];
377:
378: out = new byte[len];
379:
380: for (i = j = 0; j < len; i++, j += 4) {
381: out[j] = (byte) (input[i] & 0xff);
382: out[j + 1] = (byte) ((input[i] >>> 8) & 0xff);
383: out[j + 2] = (byte) ((input[i] >>> 16) & 0xff);
384: out[j + 3] = (byte) ((input[i] >>> 24) & 0xff);
385: }
386:
387: return out;
388: }
389:
390: /**
391: * Returns array of bytes (16 bytes) representing hash as of the
392: * current state of this object. Note: getting a hash does not
393: * invalidate the hash object, it only creates a copy of the real
394: * state which is finalized.
395: *
396: * @return Array of 16 bytes, the hash of all updated bytes
397: */
398: public synchronized byte[] Final() {
399: byte bits[];
400: int index, padlen;
401: MD5State fin;
402:
403: if (finals == null) {
404: fin = new MD5State(state);
405:
406: bits = Encode(fin.count, 8);
407:
408: index = (int) ((fin.count[0] >>> 3) & 0x3f);
409: padlen = (index < 56) ? (56 - index) : (120 - index);
410:
411: Update(fin, padding, 0, padlen);
412: /**/
413: Update(fin, bits, 0, 8);
414:
415: /* Update() sets finalds to null */
416: finals = fin;
417: }
418:
419: return Encode(finals.state, 16);
420: }
421:
422: /**
423: * Turns array of bytes into string representing each byte as
424: * unsigned hex number.
425: *
426: * @param hash Array of bytes to convert to hex-string
427: * @return Generated hex string
428: */
429: public static String asHex(byte hash[]) {
430: StringBuffer buf = new StringBuffer(hash.length * 2);
431: int i;
432:
433: for (i = 0; i < hash.length; i++) {
434: if (((int) hash[i] & 0xff) < 0x10)
435: buf.append("0");
436:
437: buf.append(Integer.toString((int) hash[i] & 0xff, 16));
438: }
439:
440: return buf.toString();
441: }
442:
443: /**
444: * Returns 32-character hex representation of this objects hash
445: *
446: * @return String of this object's hash
447: */
448: public String asHex() {
449: return asHex(this .Final());
450: }
451:
452: /**
453: * Contains internal state of the MD5 class
454: */
455:
456: private class MD5State {
457: /**
458: * 128-byte state
459: */
460: int state[];
461:
462: /**
463: * 64-bit character count (could be true Java long?)
464: */
465: int count[];
466:
467: /**
468: * 64-byte buffer (512 bits) for storing to-be-hashed characters
469: */
470: byte buffer[];
471:
472: public MD5State() {
473: buffer = new byte[64];
474: count = new int[2];
475: state = new int[4];
476:
477: state[0] = 0x67452301;
478: state[1] = 0xefcdab89;
479: state[2] = 0x98badcfe;
480: state[3] = 0x10325476;
481:
482: count[0] = count[1] = 0;
483: }
484:
485: /** Create this State as a copy of another state */
486: public MD5State(MD5State from) {
487: this ();
488:
489: int i;
490:
491: for (i = 0; i < buffer.length; i++)
492: this .buffer[i] = from.buffer[i];
493:
494: for (i = 0; i < state.length; i++)
495: this .state[i] = from.state[i];
496:
497: for (i = 0; i < count.length; i++)
498: this.count[i] = from.count[i];
499: }
500: }
501: }
|