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