001: // Md5.java
002: // $Id: Md5.java,v 1.5 2000/08/16 21:37:48 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.crypt;
007:
008: import java.io.ByteArrayInputStream;
009: import java.io.File;
010: import java.io.FileInputStream;
011: import java.io.IOException;
012: import java.io.InputStream;
013: import java.io.PrintStream;
014: import java.io.UnsupportedEncodingException;
015:
016: public class Md5 {
017: private static final int BUFFER_SIZE = 1024;
018:
019: private static final int S11 = 7;
020: private static final int S12 = 12;
021: private static final int S13 = 17;
022: private static final int S14 = 22;
023: private static final int S21 = 5;
024: private static final int S22 = 9;
025: private static final int S23 = 14;
026: private static final int S24 = 20;
027: private static final int S31 = 4;
028: private static final int S32 = 11;
029: private static final int S33 = 16;
030: private static final int S34 = 23;
031: private static final int S41 = 6;
032: private static final int S42 = 10;
033: private static final int S43 = 15;
034: private static final int S44 = 21;
035:
036: private static byte padding[] = { (byte) 0x80, (byte) 0, (byte) 0,
037: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
038: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
039: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
040: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
041: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
042: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
043: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
044: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
045: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
046: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
047: (byte) 0 };
048:
049: private InputStream in = null;
050: private boolean stringp = false;
051: private int state[] = null;
052: private long count = 0;
053: private byte buffer[] = null;
054: private byte digest[] = null;
055:
056: private static String stringify(byte buf[]) {
057: StringBuffer sb = new StringBuffer(2 * buf.length);
058: for (int i = 0; i < buf.length; i++) {
059: int h = (buf[i] & 0xf0) >> 4;
060: int l = (buf[i] & 0x0f);
061: sb.append(new Character((char) ((h > 9) ? 'a' + h - 10
062: : '0' + h)));
063: sb.append(new Character((char) ((l > 9) ? 'a' + l - 10
064: : '0' + l)));
065: }
066: return sb.toString();
067: }
068:
069: private final int F(int x, int y, int z) {
070: return ((x & y) | ((~x) & z));
071: }
072:
073: private final int G(int x, int y, int z) {
074: return ((x & z) | (y & (~z)));
075: }
076:
077: private final int H(int x, int y, int z) {
078: return (x ^ y ^ z);
079: }
080:
081: private final int I(int x, int y, int z) {
082: return (y ^ (x | (~z)));
083: }
084:
085: private final int rotate_left(int x, int n) {
086: return ((x << n) | (x >>> (32 - n)));
087: }
088:
089: private final int FF(int a, int b, int c, int d, int x, int s,
090: int ac) {
091: a += (F(b, c, d) + x + ac);
092: a = rotate_left(a, s);
093: a += b;
094: return a;
095: }
096:
097: private final int GG(int a, int b, int c, int d, int x, int s,
098: int ac) {
099: a += (G(b, c, d) + x + ac);
100: a = rotate_left(a, s);
101: a += b;
102: return a;
103: }
104:
105: private final int HH(int a, int b, int c, int d, int x, int s,
106: int ac) {
107: a += (H(b, c, d) + x + ac);
108: a = rotate_left(a, s);
109: a += b;
110: return a;
111: }
112:
113: private final int II(int a, int b, int c, int d, int x, int s,
114: int ac) {
115: a += (I(b, c, d) + x + ac);
116: a = rotate_left(a, s);
117: a += b;
118: return a;
119: }
120:
121: private final void decode(int output[], byte input[], int off,
122: int len) {
123: int i = 0;
124: int j = 0;
125: for (; j < len; i++, j += 4) {
126: output[i] = (((int) (input[off + j] & 0xff))
127: | (((int) (input[off + j + 1] & 0xff)) << 8)
128: | (((int) (input[off + j + 2] & 0xff)) << 16) | (((int) (input[off
129: + j + 3] & 0xff)) << 24));
130: }
131: }
132:
133: private final void transform(byte block[], int offset) {
134: int a = state[0];
135: int b = state[1];
136: int c = state[2];
137: int d = state[3];
138: int x[] = new int[16];
139:
140: decode(x, block, offset, 64);
141: /* Round 1 */
142: a = FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
143: d = FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
144: c = FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
145: b = FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
146: a = FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
147: d = FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
148: c = FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
149: b = FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
150: a = FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
151: d = FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
152: c = FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
153: b = FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
154: a = FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
155: d = FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
156: c = FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
157: b = FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
158: /* Round 2 */
159: a = GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
160: d = GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
161: c = GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
162: b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
163: a = GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
164: d = GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
165: c = GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
166: b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
167: a = GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
168: d = GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
169: c = GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
170: b = GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
171: a = GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
172: d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
173: c = GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
174: b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
175:
176: /* Round 3 */
177: a = HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
178: d = HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
179: c = HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
180: b = HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
181: a = HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
182: d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
183: c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
184: b = HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
185: a = HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
186: d = HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
187: c = HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
188: b = HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
189: a = HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
190: d = HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
191: c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
192: b = HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
193:
194: /* Round 4 */
195: a = II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
196: d = II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
197: c = II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
198: b = II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
199: a = II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
200: d = II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
201: c = II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
202: b = II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
203: a = II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
204: d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
205: c = II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
206: b = II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
207: a = II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
208: d = II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
209: c = II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
210: b = II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
211:
212: state[0] += a;
213: state[1] += b;
214: state[2] += c;
215: state[3] += d;
216: }
217:
218: private final void update(byte input[], int len) {
219: int index = ((int) (count >> 3)) & 0x3f;
220: count += (len << 3);
221: int partLen = 64 - index;
222: int i = 0;
223: if (len >= partLen) {
224: System.arraycopy(input, 0, buffer, index, partLen);
225: transform(buffer, 0);
226: for (i = partLen; i + 63 < len; i += 64)
227: transform(input, i);
228: index = 0;
229: } else {
230: i = 0;
231: }
232: System.arraycopy(input, i, buffer, index, len - i);
233: }
234:
235: private byte[] end() {
236: byte bits[] = new byte[8];
237: for (int i = 0; i < 8; i++)
238: bits[i] = (byte) ((count >>> (i * 8)) & 0xff);
239: int index = ((int) (count >> 3)) & 0x3f;
240: int padlen = (index < 56) ? (56 - index) : (120 - index);
241: update(padding, padlen);
242: update(bits, 8);
243: return encode(state, 16);
244: }
245:
246: // Encode the content.state array into 16 bytes array
247: private byte[] encode(int input[], int len) {
248: byte output[] = new byte[len];
249: int i = 0;
250: int j = 0;
251: for (; j < len; i++, j += 4) {
252: output[j] = (byte) ((input[i]) & 0xff);
253: output[j + 1] = (byte) ((input[i] >> 8) & 0xff);
254: output[j + 2] = (byte) ((input[i] >> 16) & 0xff);
255: output[j + 3] = (byte) ((input[i] >> 24) & 0xff);
256: }
257: return output;
258: }
259:
260: /**
261: * Get the digest for our input stream.
262: * This method constructs the input stream digest, and return it, as a
263: * a String, following the MD5 (rfc1321) algorithm,
264: * @return An instance of String, giving the message digest.
265: * @exception IOException Thrown if the digestifier was unable to read the
266: * input stream.
267: */
268:
269: public byte[] getDigest() throws IOException {
270: byte buffer[] = new byte[BUFFER_SIZE];
271: int got = -1;
272:
273: if (digest != null)
274: return digest;
275: while ((got = in.read(buffer)) > 0)
276: update(buffer, got);
277: this .digest = end();
278: return digest;
279: }
280:
281: /**
282: * Get the digest, for this string digestifier.
283: * This method doesn't throw any IOException, since it knows that the
284: * underlying stream ws built from a String.
285: */
286:
287: public byte[] processString() {
288: if (!stringp)
289: throw new RuntimeException(this .getClass().getName()
290: + "[processString]" + " not a string.");
291: try {
292: return getDigest();
293: } catch (IOException ex) {
294: }
295: throw new RuntimeException(this .getClass().getName()
296: + "[processString]" + ": implementation error.");
297: }
298:
299: /**
300: * Get the digest, as a proper string.
301: */
302:
303: public String getStringDigest() {
304: if (digest == null)
305: throw new RuntimeException(this .getClass().getName()
306: + "[getStringDigest]"
307: + ": called before processing.");
308: return stringify(digest);
309: }
310:
311: /**
312: * Construct a digestifier for the given string.
313: * @param input The string to be digestified.
314: * @param encoding the encoding name used (such as UTF8)
315: */
316:
317: public Md5(String input, String enc) {
318: byte bytes[] = null;
319: try {
320: bytes = input.getBytes(enc);
321: } catch (UnsupportedEncodingException e) {
322: throw new RuntimeException("no " + enc + " encoding!!!");
323: }
324: this .stringp = true;
325: this .in = new ByteArrayInputStream(bytes);
326: this .state = new int[4];
327: this .buffer = new byte[64];
328: this .count = 0;
329: state[0] = 0x67452301;
330: state[1] = 0xefcdab89;
331: state[2] = 0x98badcfe;
332: state[3] = 0x10325476;
333: }
334:
335: /**
336: * Construct a digestifier for the given string.
337: * @param input The string to be digestified.
338: */
339:
340: public Md5(String input) {
341: this (input, "UTF8");
342: }
343:
344: /**
345: * Construct a digestifier for the given input stream.
346: * @param in The input stream to be digestified.
347: */
348:
349: public Md5(InputStream in) {
350: this .stringp = false;
351: this .in = in;
352: this .state = new int[4];
353: this .buffer = new byte[64];
354: this .count = 0;
355: state[0] = 0x67452301;
356: state[1] = 0xefcdab89;
357: state[2] = 0x98badcfe;
358: state[3] = 0x10325476;
359: }
360:
361: public static void main(String args[]) throws IOException {
362: if (args.length != 1) {
363: System.out.println("Md5 <file>");
364: System.exit(1);
365: }
366: Md5 md5 = new Md5(new FileInputStream(new File(args[0])));
367: byte b[] = md5.getDigest();
368: System.out.println(stringify(b));
369: }
370:
371: }
|