KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HTTPClient > MD5


1 /*
2  * $Header: /cvsroot/jacorb/JacORB/src/HTTPClient/MD5.java,v 1.1.1.1 2001/03/17 18:09:08 brose Exp $
3  *
4  * MD5 in Java JDK Beta-2
5  * written Santeri Paavolainen, Helsinki Finland 1996
6  * (c) Santeri Paavolainen, Helsinki Finland 1996
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  *
23  * See http://www.cs.hut.fi/~santtu/java/ for more information on this
24  * class.
25  *
26  * This is rather straight re-implementation of the reference implementation
27  * given in RFC1321 by RSA.
28  *
29  * Passes MD5 test suite as defined in RFC1321.
30  *
31  *
32  * This Java class has been derived from the RSA Data Security, Inc. MD5
33  * Message-Digest Algorithm and its reference implementation.
34  *
35  *
36  * $Log: MD5.java,v $
37  * Revision 1.1.1.1 2001/03/17 18:09:08 brose
38  *
39  *
40  * Revision 1.1 2000/07/17 17:07:01 semu
41  * Utility package for HTTP requests.
42  *
43  * Revision 1.6 1998/09/22 18:34:00 ronald
44  * Made MD5State a private inner class
45  *
46  * Revision 1.5 1998/03/23 02:30:00 ronald
47  * Optimized: removed uadd()'s (were unnecessary)
48  * made FF(), GG(), HH(), and II() static and final (allows inlining)
49  *
50  * Revision 1.4 1997/01/12 00:05:54 ronald
51  * Made part of HTTPClient package.
52  *
53  * Revision 1.3 1996/04/15 07:28:09 santtu
54  * Added GPL statemets, and RSA derivate statements.
55  *
56  * Revision 1.2 1996/03/04 08:05:48 santtu
57  * Added offsets to Update method
58  *
59  * Revision 1.1 1996/01/07 20:51:59 santtu
60  * Initial revision
61  *
62  */

63
64 package HTTPClient;
65
66
67 /**
68  * Implementation of RSA's MD5 hash generator
69  *
70  * @version 0.3-2 18/06/1999
71  * @author Santeri Paavolainen <sjpaavol@cc.helsinki.fi>
72  */

73
74 class MD5 {
75   /**
76    * MD5 state
77    */

78   MD5State state;
79
80   /**
81    * If Final() has been called, finals is set to the current finals
82    * state. Any Update() causes this to be set to null.
83    */

84   MD5State finals;
85
86   /**
87    * Padding for Final()
88    */

89   static byte padding[] = {
90     (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
92     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
93   };
94
95   /**
96    * Initialize MD5 internal state (object can be reused just by
97    * calling Init() after every Final()
98    */

99   public synchronized void Init () {
100     state = new MD5State();
101     finals = null;
102   }
103
104   /**
105    * Class constructor
106    */

107   public MD5 () {
108     this.Init();
109   }
110
111   /**
112    * Initialize class, and update hash with ob.toString()
113    *
114    * @param ob Object, ob.toString() is used to update hash
115    * after initialization
116    */

117   public MD5 (Object JavaDoc ob) {
118     this();
119     Update(ob.toString());
120   }
121
122   private static final int rotate_left (int x, int n) {
123     return (x << n) | (x >>> (32 - n));
124   }
125
126   private static final int FF (int a, int b, int c, int d, int x, int s, int ac) {
127     a += ((b & c) | (~b & d)) + x + ac;
128     return rotate_left(a, s) + b;
129   }
130
131   private static final int GG (int a, int b, int c, int d, int x, int s, int ac) {
132     a += ((b & d) | (c & ~d)) + x + ac;
133     return rotate_left(a, s) + b;
134   }
135
136   private static final int HH (int a, int b, int c, int d, int x, 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, int s, int ac) {
142     a += (c ^ (b | ~d)) + x + ac;
143     return rotate_left(a, s) + b;
144   }
145
146   private static final int[] Decode (byte buffer[], int len, int shift) {
147     int out[];
148     int i, j;
149
150     out = new int[16];
151
152     for (i = j = 0; j < len; i++, j += 4) {
153       out[i] = ((int) (buffer[j + shift] & 0xff)) |
154     (((int) (buffer[j + 1 + shift] & 0xff)) << 8) |
155     (((int) (buffer[j + 2 + shift] & 0xff)) << 16) |
156     (((int) (buffer[j + 3 + shift] & 0xff)) << 24);
157
158 /* System.out.println("out[" + i + "] = \t" +
159              ((int) buffer[j + 0 + shift] & 0xff) + "\t|\t" +
160              ((int) buffer[j + 1 + shift] & 0xff) + "\t|\t" +
161              ((int) buffer[j + 2 + shift] & 0xff) + "\t|\t" +
162              ((int) buffer[j + 3 + shift] & 0xff));*/

163     }
164
165     return out;
166   }
167
168   private void Transform (MD5State state, byte buffer[], int shift) {
169     int
170       a = state.state[0],
171       b = state.state[1],
172       c = state.state[2],
173       d = state.state[3],
174       x[];
175
176     x = Decode(buffer, 64, shift);
177
178     /* Round 1 */
179     a = FF (a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */
180     d = FF (d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */
181     c = FF (c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */
182     b = FF (b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */
183     a = FF (a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */
184     d = FF (d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */
185     c = FF (c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */
186     b = FF (b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */
187     a = FF (a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */
188     d = FF (d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */
189     c = FF (c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
190     b = FF (b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
191     a = FF (a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
192     d = FF (d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
193     c = FF (c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
194     b = FF (b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
195
196     /* Round 2 */
197     a = GG (a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */
198     d = GG (d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */
199     c = GG (c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
200     b = GG (b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */
201     a = GG (a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */
202     d = GG (d, a, b, c, x[10], 9, 0x2441453); /* 22 */
203     c = GG (c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
204     b = GG (b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */
205     a = GG (a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */
206     d = GG (d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
207     c = GG (c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */
208     b = GG (b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */
209     a = GG (a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
210     d = GG (d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */
211     c = GG (c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */
212     b = GG (b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
213
214     /* Round 3 */
215     a = HH (a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */
216     d = HH (d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */
217     c = HH (c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
218     b = HH (b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
219     a = HH (a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */
220     d = HH (d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */
221     c = HH (c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */
222     b = HH (b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
223     a = HH (a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
224     d = HH (d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */
225     c = HH (c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */
226     b = HH (b, c, d, a, x[ 6], 23, 0x4881d05); /* 44 */
227     a = HH (a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */
228     d = HH (d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
229     c = HH (c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
230     b = HH (b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */
231
232     /* Round 4 */
233     a = II (a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */
234     d = II (d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */
235     c = II (c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
236     b = II (b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */
237     a = II (a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
238     d = II (d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */
239     c = II (c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
240     b = II (b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */
241     a = II (a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */
242     d = II (d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
243     c = II (c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */
244     b = II (b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
245     a = II (a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */
246     d = II (d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
247     c = II (c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */
248     b = II (b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */
249
250     state.state[0] += a;
251     state.state[1] += b;
252     state.state[2] += c;
253     state.state[3] += d;
254   }
255
256   /**
257    * Updates hash with the bytebuffer given (using at maximum length bytes from
258    * that buffer)
259    *
260    * @param state Which state is updated
261    * @param buffer Array of bytes to be hashed
262    * @param offset Offset to buffer array
263    * @param length Use at maximum `length' bytes (absolute
264    * maximum is buffer.length)
265    */

266   public void Update (MD5State stat, byte buffer[], int offset, int length) {
267     int index, partlen, i, start;
268
269 /* System.out.print("Offset = " + offset + "\tLength = " + length + "\t");
270     System.out.print("Buffer = ");
271     for (i = 0; i < buffer.length; i++)
272     System.out.print((int) (buffer[i] & 0xff) + " ");
273     System.out.print("\n");*/

274
275     finals = null;
276
277     /* Length can be told to be shorter, but not inter */
278     if ((length - offset)> buffer.length)
279       length = buffer.length - offset;
280
281     /* compute number of bytes mod 64 */
282     index = (int) (stat.count[0] >>> 3) & 0x3f;
283
284     if ((stat.count[0] += (length << 3)) <
285     (length << 3))
286       stat.count[1]++;
287
288     stat.count[1] += length >>> 29;
289
290     partlen = 64 - index;
291
292     if (length >= partlen) {
293       for (i = 0; i < partlen; i++)
294     stat.buffer[i + index] = buffer[i + offset];
295
296       Transform(stat, stat.buffer, 0);
297
298       for (i = partlen; (i + 63) < length; i+= 64)
299     Transform(stat, buffer, i);
300
301       index = 0;
302     } else
303       i = 0;
304
305     /* buffer remaining input */
306     if (i < length) {
307       start = i;
308       for (; i < length; i++)
309     stat.buffer[index + i - start] = buffer[i + offset];
310     }
311   }
312
313   /*
314    * Update()s for other datatypes than byte[] also. Update(byte[], int)
315    * is only the main driver.
316    */

317
318   /**
319    * Plain update, updates this object
320    */

321
322   public void Update (byte buffer[], int offset, int length) {
323       Update(this.state, buffer, offset, length);
324   }
325
326   public void Update (byte buffer[], int length) {
327       Update(this.state, buffer, 0, length);
328   }
329
330   /**
331    * Updates hash with given array of bytes
332    *
333    * @param buffer Array of bytes to use for updating the hash
334    */

335   public void Update (byte buffer[]) {
336       Update(buffer, 0, buffer.length);
337   }
338
339   /**
340    * Updates hash with a single byte
341    *
342    * @param b Single byte to update the hash
343    */

344   public void Update (byte b) {
345     byte buffer[] = new byte[1];
346     buffer[0] = b;
347
348     Update(buffer, 1);
349   }
350
351   /**
352    * Update buffer with given string.
353    *
354    * @param s String to be update to hash (is used as
355    * s.getBytes())
356    */

357   public void Update (String JavaDoc s) {
358     byte chars[];
359
360     chars = new byte[s.length()];
361     s.getBytes(0, s.length(), chars, 0);
362
363     Update(chars, chars.length);
364   }
365
366   /**
367    * Update buffer with a single integer (only & 0xff part is used,
368    * as a byte)
369    *
370    * @param i Integer value, which is then converted to
371    * byte as i & 0xff
372    */

373
374   public void Update (int i) {
375       Update((byte) (i & 0xff));
376   }
377
378   private byte[] Encode (int input[], int len) {
379     int i, j;
380     byte out[];
381
382     out = new byte[len];
383
384     for (i = j = 0; j < len; i++, j += 4) {
385       out[j] = (byte) (input[i] & 0xff);
386       out[j + 1] = (byte) ((input[i] >>> 8) & 0xff);
387       out[j + 2] = (byte) ((input[i] >>> 16) & 0xff);
388       out[j + 3] = (byte) ((input[i] >>> 24) & 0xff);
389     }
390
391     return out;
392   }
393
394   /**
395    * Returns array of bytes (16 bytes) representing hash as of the
396    * current state of this object. Note: getting a hash does not
397    * invalidate the hash object, it only creates a copy of the real
398    * state which is finalized.
399    *
400    * @return Array of 16 bytes, the hash of all updated bytes
401    */

402   public synchronized byte[] Final () {
403     byte bits[];
404     int index, padlen;
405     MD5State fin;
406
407     if (finals == null) {
408       fin = new MD5State(state);
409
410       bits = Encode(fin.count, 8);
411
412       index = (int) ((fin.count[0] >>> 3) & 0x3f);
413       padlen = (index < 56) ? (56 - index) : (120 - index);
414
415       Update(fin, padding, 0, padlen);
416       /**/
417       Update(fin, bits, 0, 8);
418
419       /* Update() sets finalds to null */
420       finals = fin;
421     }
422
423     return Encode(finals.state, 16);
424   }
425
426   /**
427    * Turns array of bytes into string representing each byte as
428    * unsigned hex number.
429    *
430    * @param hash Array of bytes to convert to hex-string
431    * @return Generated hex string
432    */

433   public static String JavaDoc asHex (byte hash[]) {
434     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(hash.length * 2);
435     int i;
436
437     for (i = 0; i < hash.length; i++) {
438       if (((int) hash[i] & 0xff) < 0x10)
439     buf.append("0");
440
441       buf.append(Integer.toString((int) hash[i] & 0xff, 16));
442     }
443
444     return buf.toString();
445   }
446
447   /**
448    * Returns 32-character hex representation of this objects hash
449    *
450    * @return String of this object's hash
451    */

452   public String JavaDoc asHex () {
453     return asHex(this.Final());
454   }
455
456
457   /**
458    * Contains internal state of the MD5 class
459    */

460
461   private class MD5State {
462     /**
463      * 128-byte state
464      */

465     int state[];
466
467     /**
468      * 64-bit character count (could be true Java long?)
469      */

470     int count[];
471
472     /**
473      * 64-byte buffer (512 bits) for storing to-be-hashed characters
474      */

475     byte buffer[];
476
477     public MD5State() {
478       buffer = new byte[64];
479       count = new int[2];
480       state = new int[4];
481
482       state[0] = 0x67452301;
483       state[1] = 0xefcdab89;
484       state[2] = 0x98badcfe;
485       state[3] = 0x10325476;
486
487       count[0] = count[1] = 0;
488     }
489
490     /** Create this State as a copy of another state */
491     public MD5State (MD5State from) {
492       this();
493
494       int i;
495
496       for (i = 0; i < buffer.length; i++)
497     this.buffer[i] = from.buffer[i];
498
499       for (i = 0; i < state.length; i++)
500     this.state[i] = from.state[i];
501
502       for (i = 0; i < count.length; i++)
503     this.count[i] = from.count[i];
504     }
505   }
506 }
507
508
Popular Tags