KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > crypto > digest > MD5


1
2 package ch.ethz.ssh2.crypto.digest;
3
4 /**
5  * MD5. Based on the example code in RFC 1321. Optimized (...a little).
6  *
7  * @author Christian Plattner, plattner@inf.ethz.ch
8  * @version $Id: MD5.java,v 1.2 2006/02/02 09:11:03 cplattne Exp $
9  */

10
11 /*
12  * The following disclaimer has been copied from RFC 1321:
13  *
14  * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights
15  * reserved.
16  *
17  * License to copy and use this software is granted provided that it is
18  * identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm" in
19  * all material mentioning or referencing this software or this function.
20  *
21  * License is also granted to make and use derivative works provided that such
22  * works are identified as "derived from the RSA Data Security, Inc. MD5
23  * Message-Digest Algorithm" in all material mentioning or referencing the
24  * derived work.
25  *
26  * RSA Data Security, Inc. makes no representations concerning either the
27  * merchantability of this software or the suitability of this software for any
28  * particular purpose. It is provided "as is" without express or implied
29  * warranty of any kind.
30  *
31  * These notices must be retained in any copies of any part of this
32  * documentation and/or software.
33  *
34  */

35
36 public final class MD5 implements Digest
37 {
38     private int state0, state1, state2, state3;
39     private long count;
40     private final byte[] block = new byte[64];
41     private final int x[] = new int[16];
42
43     private static final byte[] padding = new byte[] { (byte) 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45             0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
46
47     public MD5()
48     {
49         reset();
50     }
51
52     private static final int FF(int a, int b, int c, int d, int x, int s, int ac)
53     {
54         a += ((b & c) | ((~b) & d)) + x + ac;
55         return ((a << s) | (a >>> (32 - s))) + b;
56     }
57
58     private static final int GG(int a, int b, int c, int d, int x, int s, int ac)
59     {
60         a += ((b & d) | (c & (~d))) + x + ac;
61         return ((a << s) | (a >>> (32 - s))) + b;
62     }
63
64     private static final int HH(int a, int b, int c, int d, int x, int s, int ac)
65     {
66         a += (b ^ c ^ d) + x + ac;
67         return ((a << s) | (a >>> (32 - s))) + b;
68     }
69
70     private static final int II(int a, int b, int c, int d, int x, int s, int ac)
71     {
72         a += (c ^ (b | (~d))) + x + ac;
73         return ((a << s) | (a >>> (32 - s))) + b;
74     }
75
76     private static final void encode(byte[] dst, int dstoff, int word)
77     {
78         dst[dstoff] = (byte) (word);
79         dst[dstoff + 1] = (byte) (word >> 8);
80         dst[dstoff + 2] = (byte) (word >> 16);
81         dst[dstoff + 3] = (byte) (word >> 24);
82     }
83
84     private final void transform(byte[] src, int pos)
85     {
86         int a = state0;
87         int b = state1;
88         int c = state2;
89         int d = state3;
90
91         for (int i = 0; i < 16; i++, pos += 4)
92         {
93             x[i] = (src[pos] & 0xff) | ((src[pos + 1] & 0xff) << 8) | ((src[pos + 2] & 0xff) << 16)
94                     | ((src[pos + 3] & 0xff) << 24);
95         }
96
97         /* Round 1 */
98
99         a = FF(a, b, c, d, x[0], 7, 0xd76aa478); /* 1 */
100         d = FF(d, a, b, c, x[1], 12, 0xe8c7b756); /* 2 */
101         c = FF(c, d, a, b, x[2], 17, 0x242070db); /* 3 */
102         b = FF(b, c, d, a, x[3], 22, 0xc1bdceee); /* 4 */
103         a = FF(a, b, c, d, x[4], 7, 0xf57c0faf); /* 5 */
104         d = FF(d, a, b, c, x[5], 12, 0x4787c62a); /* 6 */
105         c = FF(c, d, a, b, x[6], 17, 0xa8304613); /* 7 */
106         b = FF(b, c, d, a, x[7], 22, 0xfd469501); /* 8 */
107         a = FF(a, b, c, d, x[8], 7, 0x698098d8); /* 9 */
108         d = FF(d, a, b, c, x[9], 12, 0x8b44f7af); /* 10 */
109         c = FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
110         b = FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
111         a = FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
112         d = FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
113         c = FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
114         b = FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
115
116         /* Round 2 */
117         a = GG(a, b, c, d, x[1], 5, 0xf61e2562); /* 17 */
118         d = GG(d, a, b, c, x[6], 9, 0xc040b340); /* 18 */
119         c = GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
120         b = GG(b, c, d, a, x[0], 20, 0xe9b6c7aa); /* 20 */
121         a = GG(a, b, c, d, x[5], 5, 0xd62f105d); /* 21 */
122         d = GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */
123         c = GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
124         b = GG(b, c, d, a, x[4], 20, 0xe7d3fbc8); /* 24 */
125         a = GG(a, b, c, d, x[9], 5, 0x21e1cde6); /* 25 */
126         d = GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
127         c = GG(c, d, a, b, x[3], 14, 0xf4d50d87); /* 27 */
128         b = GG(b, c, d, a, x[8], 20, 0x455a14ed); /* 28 */
129         a = GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
130         d = GG(d, a, b, c, x[2], 9, 0xfcefa3f8); /* 30 */
131         c = GG(c, d, a, b, x[7], 14, 0x676f02d9); /* 31 */
132         b = GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
133
134         /* Round 3 */
135         a = HH(a, b, c, d, x[5], 4, 0xfffa3942); /* 33 */
136         d = HH(d, a, b, c, x[8], 11, 0x8771f681); /* 34 */
137         c = HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
138         b = HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
139         a = HH(a, b, c, d, x[1], 4, 0xa4beea44); /* 37 */
140         d = HH(d, a, b, c, x[4], 11, 0x4bdecfa9); /* 38 */
141         c = HH(c, d, a, b, x[7], 16, 0xf6bb4b60); /* 39 */
142         b = HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
143         a = HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
144         d = HH(d, a, b, c, x[0], 11, 0xeaa127fa); /* 42 */
145         c = HH(c, d, a, b, x[3], 16, 0xd4ef3085); /* 43 */
146         b = HH(b, c, d, a, x[6], 23, 0x4881d05); /* 44 */
147         a = HH(a, b, c, d, x[9], 4, 0xd9d4d039); /* 45 */
148         d = HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
149         c = HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
150         b = HH(b, c, d, a, x[2], 23, 0xc4ac5665); /* 48 */
151
152         /* Round 4 */
153         a = II(a, b, c, d, x[0], 6, 0xf4292244); /* 49 */
154         d = II(d, a, b, c, x[7], 10, 0x432aff97); /* 50 */
155         c = II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
156         b = II(b, c, d, a, x[5], 21, 0xfc93a039); /* 52 */
157         a = II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
158         d = II(d, a, b, c, x[3], 10, 0x8f0ccc92); /* 54 */
159         c = II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
160         b = II(b, c, d, a, x[1], 21, 0x85845dd1); /* 56 */
161         a = II(a, b, c, d, x[8], 6, 0x6fa87e4f); /* 57 */
162         d = II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
163         c = II(c, d, a, b, x[6], 15, 0xa3014314); /* 59 */
164         b = II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
165         a = II(a, b, c, d, x[4], 6, 0xf7537e82); /* 61 */
166         d = II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
167         c = II(c, d, a, b, x[2], 15, 0x2ad7d2bb); /* 63 */
168         b = II(b, c, d, a, x[9], 21, 0xeb86d391); /* 64 */
169
170         state0 += a;
171         state1 += b;
172         state2 += c;
173         state3 += d;
174     }
175
176     public final void reset()
177     {
178         count = 0;
179
180         state0 = 0x67452301;
181         state1 = 0xefcdab89;
182         state2 = 0x98badcfe;
183         state3 = 0x10325476;
184
185         /* Clear traces in memory... */
186
187         for (int i = 0; i < 16; i++)
188             x[i] = 0;
189     }
190
191     public final void update(byte b)
192     {
193         final int space = 64 - ((int) (count & 0x3f));
194
195         count++;
196
197         block[64 - space] = b;
198
199         if (space == 1)
200             transform(block, 0);
201     }
202
203     public final void update(byte[] buff, int pos, int len)
204     {
205         int space = 64 - ((int) (count & 0x3f));
206
207         count += len;
208
209         while (len > 0)
210         {
211             if (len < space)
212             {
213                 System.arraycopy(buff, pos, block, 64 - space, len);
214                 break;
215             }
216
217             if (space == 64)
218             {
219                 transform(buff, pos);
220             }
221             else
222             {
223                 System.arraycopy(buff, pos, block, 64 - space, space);
224                 transform(block, 0);
225             }
226
227             pos += space;
228             len -= space;
229             space = 64;
230         }
231     }
232
233     public final void update(byte[] b)
234     {
235         update(b, 0, b.length);
236     }
237
238     public final void digest(byte[] dst, int pos)
239     {
240         byte[] bits = new byte[8];
241
242         encode(bits, 0, (int) (count << 3));
243         encode(bits, 4, (int) (count >> 29));
244
245         int idx = (int) count & 0x3f;
246         int padLen = (idx < 56) ? (56 - idx) : (120 - idx);
247
248         update(padding, 0, padLen);
249         update(bits, 0, 8);
250
251         encode(dst, pos, state0);
252         encode(dst, pos + 4, state1);
253         encode(dst, pos + 8, state2);
254         encode(dst, pos + 12, state3);
255
256         reset();
257     }
258
259     public final void digest(byte[] dst)
260     {
261         digest(dst, 0);
262     }
263
264     public final int getDigestLength()
265     {
266         return 16;
267     }
268 }
269
Popular Tags