Source code: com/ssttr/crypto/MD5.java
1 /*
2 * @(#)MD5.java 1.9 95/08/07
3 *
4 * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
5 *
6 * Permission to use, copy, modify, and distribute this software
7 * and its documentation for NON-COMMERCIAL purposes and without
8 * fee is hereby granted provided that this copyright notice
9 * appears in all copies. Please refer to the file "copyright.html"
10 * for further important copyright and licensing information.
11 *
12 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
13 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
14 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
15 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
16 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
17 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
18 *
19 * Updated to the JDK 1.0.2 release by Chuck McManis
20 *
21 * This file was obtained from: http://www.mcmanis.com/~cmcmanis/java/src/util/crypt/MD5.java
22 * More information can be found here: http://www.mcmanis.com/~cmcmanis/java/
23 */
24
25 package com.ssttr.crypto;
26
27 /**
28 * The MD5 class is used to compute an MD5 message digest over a given
29 * buffer of bytes. It is an implementation of the RSA Data Security Inc
30 * MD5 algorithim as described in internet RFC 1321.
31 * @version 06 Oct 1996, 1.9.1
32 * @author Chuck McManis
33 */
34 public final class MD5
35 extends MessageDigest
36 {
37 /** containss the computed message digest */
38
39 private int state[];
40 private long count;
41 private byte buffer[];
42 private static int transformBuffer[];
43
44 private static final int S11 = 7;
45 private static final int S12 = 12;
46 private static final int S13 = 17;
47 private static final int S14 = 22;
48 private static final int S21 = 5;
49 private static final int S22 = 9;
50 private static final int S23 = 14;
51 private static final int S24 = 20;
52 private static final int S31 = 4;
53 private static final int S32 = 11;
54 private static final int S33 = 16;
55 private static final int S34 = 23;
56 private static final int S41 = 6;
57 private static final int S42 = 10;
58 private static final int S43 = 15;
59 private static final int S44 = 21;
60
61 /**
62 * Standard constructor, creates a new MD5 instance, allocates its
63 * buffers from the heap.
64 */
65 public MD5() {
66 state = new int[4];
67 count = 0;
68 if (transformBuffer == null)
69 transformBuffer = new int[16];
70 buffer = new byte[64];
71 digestBits = new byte[16];
72 digestValid = false;
73 }
74
75 /**
76 * Alternate constructor, allows you to pass in the buffer where you
77 * want the resulting digest stored.
78 */
79 public MD5(byte mydigest[]) {
80 state = new int[4];
81 count = 0;
82 if (transformBuffer == null)
83 transformBuffer = new int[16];
84 buffer = new byte[64];
85 digestBits = mydigest;
86 digestValid = false;
87 }
88
89 /* **********************************************************
90 * The MD5 Functions. These are copied verbatim from
91 * the RFC to insure accuracy. The results of this
92 * implementation were checked against the RSADSI version.
93 * **********************************************************
94 */
95
96 private int F(int x, int y, int z) {
97 return ((x & y) | ((~x) & z));
98 }
99
100 private int G(int x, int y, int z) {
101 return ((x & z) | (y & (~z)));
102 }
103
104 private int H(int x, int y, int z) {
105 return ((x ^ y) ^ z);
106 }
107
108 private int I(int x, int y, int z) {
109 return (y ^ (x | (~z)));
110 }
111
112 private int rotateLeft(int a, int n) {
113 return ((a << n) | (a >>> (32 - n)));
114 }
115
116 private int FF(int a, int b, int c, int d, int x, int s, int ac) {
117 a += F(b, c, d) + x + ac;
118 a = rotateLeft(a, s);
119 a += b;
120 return a;
121 }
122
123 private int GG(int a, int b, int c, int d, int x, int s, int ac) {
124 a += G(b, c, d) + x + ac;
125 a = rotateLeft(a, s);
126 a += b;
127 return a;
128 }
129
130 private int HH(int a, int b, int c, int d, int x, int s, int ac) {
131 a += H(b, c, d) + x + ac;
132 a = rotateLeft(a, s);
133 a += b;
134 return a;
135 }
136
137 private int II(int a, int b, int c, int d, int x, int s, int ac) {
138 a += I(b, c, d) + x + ac;
139 a = rotateLeft(a, s);
140 a += b;
141 return a;
142 }
143
144 /**
145 * This is where the functions come together as the generic MD5
146 * transformation operation, it is called by update() which is
147 * synchronized (to protect transformBuffer)
148 */
149 void transform(byte buf[], int offset) {
150 int a, b, c, d;
151 int x[] = transformBuffer;
152
153 a = state[0];
154 b = state[1];
155 c = state[2];
156 d = state[3];
157
158 for (int i = 0; i < 16; i++) {
159 x[i] = (int)buf[i*4+offset] & 0xff;
160 for (int j = 1; j < 4; j++) {
161 x[i] += ((int)buf[i*4+j+offset] & 0xff) << (j * 8);
162 }
163 }
164
165 /* Round 1 */
166 a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
167 d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
168 c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
169 b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
170 a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
171 d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
172 c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
173 b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
174 a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
175 d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
176 c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
177 b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
178 a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
179 d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
180 c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
181 b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
182
183 /* Round 2 */
184 a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
185 d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
186 c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
187 b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
188 a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
189 d = GG ( d, a, b, c, x[10], S22, 0x2441453); /* 22 */
190 c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
191 b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
192 a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
193 d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
194 c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
195 b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
196 a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
197 d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
198 c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
199 b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
200
201 /* Round 3 */
202 a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
203 d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
204 c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
205 b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
206 a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
207 d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
208 c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
209 b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
210 a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
211 d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
212 c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
213 b = HH ( b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
214 a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
215 d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
216 c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
217 b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
218
219 /* Round 4 */
220 a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
221 d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
222 c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
223 b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
224 a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
225 d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
226 c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
227 b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
228 a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
229 d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
230 c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
231 b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
232 a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
233 d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
234 c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
235 b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
236
237 state[0] += a;
238 state[1] += b;
239 state[2] += c;
240 state[3] += d;
241 }
242
243 /**
244 * Initialize the MD5 state information and reset the bit count
245 * to 0. Given this implementation you are constrained to counting
246 * 2^64 bits.
247 */
248 public void init() {
249 count = 0;
250 // Load magic initialization constants.
251 state[0] = 0x67452301;
252 state[1] = 0xefcdab89;
253 state[2] = 0x98badcfe;
254 state[3] = 0x10325476;
255 digestValid = false;
256 for (int i = 0; i < digestBits.length; i++)
257 digestBits[i] = 0;
258 }
259
260 /**
261 * update adds the passed type to the input buffer
262 */
263 public synchronized void update(byte b) {
264 int index;
265
266 index = (int) ((count >>> 3) & 0x3f);
267 count += 8;
268 buffer[index] = b;
269 if (index >= 63) {
270 transform(buffer, 0);
271 }
272 }
273
274 /**
275 * Perform the final computations, any buffered bytes are added
276 * to the digest, the count is added to the digest, and the resulting
277 * digest is stored. After calling final you will need to call
278 * init() again to do another digest.
279 */
280 public void finish() {
281 byte bits[] = new byte[8];
282 byte padding[];
283 int i, index, padLen;
284
285 for (i = 0; i < 8; i++) {
286 bits[i] = (byte)((count >>> (i * 8)) & 0xff);
287 }
288
289 index = (int)(count >> 3) & 0x3f;
290 padLen = (index < 56) ? (56 - index) : (120 - index);
291 padding = new byte[padLen];
292 padding[0] = (byte) 0x80;
293 update(padding);
294 update(bits);
295
296 for (i = 0; i < 4; i++) {
297 for (int j = 0; j < 4; j++) {
298 digestBits[i*4+j] = (byte)((state[i] >>> (j * 8)) & 0xff);
299 }
300 }
301 digestValid = true;
302 }
303
304 public String getAlg() {
305 return ("MD5");
306 }
307 }