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