001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail.util;
009:
010: /**
011: * This class implements the MD5 algorithm.
012: * <p>
013: * The specification is available from RFC 1321,
014: * and there are numerous implementations out there,
015: * this one was tuned specifically for hashing short
016: * strings (i.e. passwords).
017: * <p>
018: * I do not recommend to use this implementation for
019: * anything else but that, and if anybody feels that
020: * this code is to "close" to something available on
021: * the net, please contact me, and I will certainly take
022: * steps to clear up the situation.
023: */
024: public final class MD5 {
025:
026: /**
027: * Returns the MD5 hash digest transformed into a hex
028: * representation as <tt>String</tt>.
029: *
030: * @return the MD5 hash of the input as <tt>String</tt>.
031: */
032: public static final String hash(String str) {
033: return MD5.asHex(MD5.digest(str.getBytes()));
034: }//hash
035:
036: /**
037: * Returns the MD5 hash of the input data.
038: * <p>
039: * Following the the MD5 standard specification, the result
040: * is returned least significant byte first, however,
041: * many applications use the most significant byte first (more conventional).
042: *
043: * @param msg the <tt>byte[]</tt> to be digested.
044: *
045: * @return the MD5 hash of the data as <tt>String</tt>.
046: */
047: public static final byte[] digest(byte[] msg) {
048: int padsize = (120 - (msg.length % 64)) % 64;
049: int i;
050: long l = msg.length * 8;
051:
052: //MD5 registers
053: int a = 0x67452301, aa, b = 0xefcdab89, bb, c = 0x98badcfe, cc, d = 0x10325476, dd;
054:
055: byte[] src = new byte[msg.length + padsize + 8];
056:
057: //padding
058: try {
059: System.arraycopy(msg, 0, src, 0, msg.length);
060: } catch (Exception ex) {
061: //should not happen, but calm's the compiler
062: }
063:
064: src[msg.length] = (byte) 0x80;
065:
066: for (i = msg.length + 1; i < msg.length + padsize; i++)
067: src[i] = 0;
068:
069: //append length
070: for (i = src.length - 8; i < src.length; i++) {
071: src[i] = (byte) l;
072: l >>>= 8;
073: }
074: int[] x = new int[16];
075:
076: //prcess the data 16-word blocks
077: for (i = 0; i < src.length; i += 64) {
078: //construct block
079: for (int j = 0; j < 16; j++) {
080: x[j] = 0;
081: for (int k = 3; k >= 0; k--) {
082: x[j] <<= 8;
083: x[j] += src[i + j * 4 + k] & 0xff;
084: }
085: }
086: aa = a;
087: bb = b;
088: cc = c;
089: dd = d;
090: a = round1(a, b, c, d, 0, 7, 1, x);
091: d = round1(d, a, b, c, 1, 12, 2, x);
092: c = round1(c, d, a, b, 2, 17, 3, x);
093: b = round1(b, c, d, a, 3, 22, 4, x);
094:
095: a = round1(a, b, c, d, 4, 7, 5, x);
096: d = round1(d, a, b, c, 5, 12, 6, x);
097: c = round1(c, d, a, b, 6, 17, 7, x);
098: b = round1(b, c, d, a, 7, 22, 8, x);
099:
100: a = round1(a, b, c, d, 8, 7, 9, x);
101: d = round1(d, a, b, c, 9, 12, 10, x);
102: c = round1(c, d, a, b, 10, 17, 11, x);
103: b = round1(b, c, d, a, 11, 22, 12, x);
104:
105: a = round1(a, b, c, d, 12, 7, 13, x);
106: d = round1(d, a, b, c, 13, 12, 14, x);
107: c = round1(c, d, a, b, 14, 17, 15, x);
108: b = round1(b, c, d, a, 15, 22, 16, x);
109:
110: a = round2(a, b, c, d, 1, 5, 17, x);
111: d = round2(d, a, b, c, 6, 9, 18, x);
112: c = round2(c, d, a, b, 11, 14, 19, x);
113: b = round2(b, c, d, a, 0, 20, 20, x);
114:
115: a = round2(a, b, c, d, 5, 5, 21, x);
116: d = round2(d, a, b, c, 10, 9, 22, x);
117: c = round2(c, d, a, b, 15, 14, 23, x);
118: b = round2(b, c, d, a, 4, 20, 24, x);
119:
120: a = round2(a, b, c, d, 9, 5, 25, x);
121: d = round2(d, a, b, c, 14, 9, 26, x);
122: c = round2(c, d, a, b, 3, 14, 27, x);
123: b = round2(b, c, d, a, 8, 20, 28, x);
124:
125: a = round2(a, b, c, d, 13, 5, 29, x);
126: d = round2(d, a, b, c, 2, 9, 30, x);
127: c = round2(c, d, a, b, 7, 14, 31, x);
128: b = round2(b, c, d, a, 12, 20, 32, x);
129:
130: a = round3(a, b, c, d, 5, 4, 33, x);
131: d = round3(d, a, b, c, 8, 11, 34, x);
132: c = round3(c, d, a, b, 11, 16, 35, x);
133: b = round3(b, c, d, a, 14, 23, 36, x);
134:
135: a = round3(a, b, c, d, 1, 4, 37, x);
136: d = round3(d, a, b, c, 4, 11, 38, x);
137: c = round3(c, d, a, b, 7, 16, 39, x);
138: b = round3(b, c, d, a, 10, 23, 40, x);
139:
140: a = round3(a, b, c, d, 13, 4, 41, x);
141: d = round3(d, a, b, c, 0, 11, 42, x);
142: c = round3(c, d, a, b, 3, 16, 43, x);
143: b = round3(b, c, d, a, 6, 23, 44, x);
144:
145: a = round3(a, b, c, d, 9, 4, 45, x);
146: d = round3(d, a, b, c, 12, 11, 46, x);
147: c = round3(c, d, a, b, 15, 16, 47, x);
148: b = round3(b, c, d, a, 2, 23, 48, x);
149:
150: a = round4(a, b, c, d, 0, 6, 49, x);
151: d = round4(d, a, b, c, 7, 10, 50, x);
152: c = round4(c, d, a, b, 14, 15, 51, x);
153: b = round4(b, c, d, a, 5, 21, 52, x);
154:
155: a = round4(a, b, c, d, 12, 6, 53, x);
156: d = round4(d, a, b, c, 3, 10, 54, x);
157: c = round4(c, d, a, b, 10, 15, 55, x);
158: b = round4(b, c, d, a, 1, 21, 56, x);
159:
160: a = round4(a, b, c, d, 8, 6, 57, x);
161: d = round4(d, a, b, c, 15, 10, 58, x);
162: c = round4(c, d, a, b, 6, 15, 59, x);
163: b = round4(b, c, d, a, 13, 21, 60, x);
164:
165: a = round4(a, b, c, d, 4, 6, 61, x);
166: d = round4(d, a, b, c, 11, 10, 62, x);
167: c = round4(c, d, a, b, 2, 15, 63, x);
168: b = round4(b, c, d, a, 9, 21, 64, x);
169:
170: a += aa;
171: b += bb;
172: c += cc;
173: d += dd;
174: }
175: byte[] ret = new byte[16];
176:
177: for (i = 0; i < 4; i++) {
178: ret[i] = (byte) a;
179: a >>>= 8;
180: }
181: for (; i < 8; i++) {
182: ret[i] = (byte) b;
183: b >>>= 8;
184: }
185: for (; i < 12; i++) {
186: ret[i] = (byte) c;
187: c >>>= 8;
188: }
189: for (; i < 16; i++) {
190: ret[i] = (byte) d;
191: d >>>= 8;
192: }
193: return ret;
194: }//digest
195:
196: /** MD5 Transformation routines *********************************************/
197:
198: private static final int rot(int x, int s) {
199: return x << s | x >>> (32 - s);
200: }//rot
201:
202: private static final int F(int x, int y, int z) {
203: return (x & y) | (~x & z);
204: }//F
205:
206: private static final int G(int x, int y, int z) {
207: return (x & z) | (y & ~z);
208: }//G
209:
210: private static final int H(int x, int y, int z) {
211: return x ^ y ^ z;
212: }//H
213:
214: private static final int I(int x, int y, int z) {
215: return y ^ (x | ~z);
216: }//I
217:
218: private static final int round1(int a, int b, int c, int d, int k,
219: int s, int i, int[] x) {
220: return b + rot((a + F(b, c, d) + x[k] + T[i - 1]), s);
221: }//round1
222:
223: private static final int round2(int a, int b, int c, int d, int k,
224: int s, int i, int[] x) {
225: return b + rot((a + G(b, c, d) + x[k] + T[i - 1]), s);
226: }//round2
227:
228: private static final int round3(int a, int b, int c, int d, int k,
229: int s, int i, int[] x) {
230: return b + rot((a + H(b, c, d) + x[k] + T[i - 1]), s);
231: }//round3
232:
233: private static final int round4(int a, int b, int c, int d, int k,
234: int s, int i, int[] x) {
235: return b + rot((a + I(b, c, d) + x[k] + T[i - 1]), s);
236: }//round4
237:
238: private static final int T[] = { 0xd76aa478, 0xe8c7b756,
239: 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613,
240: 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
241: 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562,
242: 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453,
243: 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87,
244: 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
245: 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44,
246: 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,
247: 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8,
248: 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
249: 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f,
250: 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235,
251: 0x2ad7d2bb, 0xeb86d391 };
252:
253: /** END MD5 Transformation routines *****************************************/
254:
255: /**
256: * Returns a <tt>String</tt> containing unsigned hexadecimal
257: * numbers as digits.
258: * <p>
259: * Contains two hex digit characters for each byte from the passed in
260: * <tt>byte[]</tt>.
261: *
262: * @param data the array of bytes to be converted into a hex-string.
263: * @return the generated hexadecimal representation as
264: * <tt>String</tt>.
265: */
266: public static final String asHex(byte[] data) {
267: //double size, two bytes (hex range) for one byte
268: StringBuffer buf = new StringBuffer(data.length * 2);
269: for (int i = 0; i < data.length; i++) {
270: //don't forget the second hex digit
271: if (((int) data[i] & 0xff) < 0x10) {
272: buf.append("0");
273: }
274: buf.append(Long.toString((int) data[i] & 0xff, 16));
275: }
276: return buf.toString();
277: }//asHex
278:
279: /**
280: * A main, to allow using this class from the command line.
281: */
282: public static void main(String[] args) {
283: try {
284: if (args == null || args.length == 0) {
285: System.out
286: .println("Usage: java dtw.webmail.util.MD5 <password>");
287: }
288: System.out.println("Hash=" + hash(args[0]));
289: } catch (Exception ex) {
290: ex.printStackTrace();
291: }
292: }//main
293:
294: }//class MD5
|