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