KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > transformers > MD5


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.transformers;
11
12 /**
13  * Do MD5 encoding. Decoding is of course not possible. MD5 encoding
14  * can not be efficiently 'piped', because the complete String is
15  * needed. So, be careful 'chaining' it.
16  *
17  * @author Michiel Meeuwissen
18  * @version $Id: MD5.java,v 1.7 2003/05/12 14:33:00 kees Exp $
19  */

20
21 public class MD5 extends StringTransformer implements CharTransformer {
22     private final static String JavaDoc ENCODING = "MD5";
23     private MD5Implementation transformer = new MD5Implementation();
24
25     public String JavaDoc toString() {
26         return ENCODING;
27     }
28
29     public String JavaDoc transform(String JavaDoc r) {
30         return transformer.calcMD5(r);
31     }
32
33     public String JavaDoc transformBack(String JavaDoc w) {
34         throw new UnsupportedOperationException JavaDoc("transformBack(String) can never be done for MD5(i hope so :p)");
35     }
36
37     // from http://pajhome.org.uk/crypt/md5/md5.java.txt
38
// With permission of Thomas Weber (tw@orange-interactive.de)
39
class MD5Implementation {
40         /*
41          * A Java implementation of the RSA Data Security, Inc. MD5 Message
42          * Digest Algorithm, as defined in RFC 1321.
43          * Based on the JavaScript implementation of Paul Johnston
44          * Copyright (C) Paul Johnston 1999 - 2000.
45          * See http://pajhome.org.uk/site/legal.html for details.
46          * Java Version by Thomas Weber (Orange Interactive GmbH)
47          */

48
49         /*
50          * Convert a 32-bit number to a hex string with ls-byte first
51          */

52         String JavaDoc hex_chr = "0123456789abcdef";
53         private String JavaDoc rhex(int num) {
54             String JavaDoc str = "";
55             for (int j = 0; j <= 3; j++)
56                 str = str + hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) + hex_chr.charAt((num >> (j * 8)) & 0x0F);
57             return str;
58         }
59
60         /*
61          * Convert a string to a sequence of 16-word blocks, stored as an array.
62          * Append padding bits and the length, as described in the MD5 standard.
63          */

64         private int[] str2blks_MD5(String JavaDoc str) {
65             int nblk = ((str.length() + 8) >> 6) + 1;
66             int[] blks = new int[nblk * 16];
67             int i = 0;
68             for (i = 0; i < nblk * 16; i++) {
69                 blks[i] = 0;
70             }
71             for (i = 0; i < str.length(); i++) {
72                 blks[i >> 2] |= str.charAt(i) << ((i % 4) * 8);
73             }
74             blks[i >> 2] |= 0x80 << ((i % 4) * 8);
75             blks[nblk * 16 - 2] = str.length() * 8;
76
77             return blks;
78         }
79
80         /*
81          * Add integers, wrapping at 2^32
82          */

83         private int add(int x, int y) {
84             return ((x & 0x7FFFFFFF) + (y & 0x7FFFFFFF)) ^ (x & 0x80000000) ^ (y & 0x80000000);
85         }
86
87         /*
88          * Bitwise rotate a 32-bit number to the left
89          */

90         private int rol(int num, int cnt) {
91             return (num << cnt) | (num >>> (32 - cnt));
92         }
93
94         /*
95          * These functions implement the basic operation for each round of the
96          * algorithm.
97          */

98         private int cmn(int q, int a, int b, int x, int s, int t) {
99             return add(rol(add(add(a, q), add(x, t)), s), b);
100         }
101         private int ff(int a, int b, int c, int d, int x, int s, int t) {
102             return cmn((b & c) | ((~b) & d), a, b, x, s, t);
103         }
104         private int gg(int a, int b, int c, int d, int x, int s, int t) {
105             return cmn((b & d) | (c & (~d)), a, b, x, s, t);
106         }
107         private int hh(int a, int b, int c, int d, int x, int s, int t) {
108             return cmn(b ^ c ^ d, a, b, x, s, t);
109         }
110         private int ii(int a, int b, int c, int d, int x, int s, int t) {
111             return cmn(c ^ (b | (~d)), a, b, x, s, t);
112         }
113
114         /*
115          * Take a string and return the hex representation of its MD5.
116          */

117         public String JavaDoc calcMD5(String JavaDoc str) {
118             int[] x = str2blks_MD5(str);
119             int a = 0x67452301;
120             int b = 0xEFCDAB89;
121             int c = 0x98BADCFE;
122             int d = 0x10325476;
123
124             for (int i = 0; i < x.length; i += 16) {
125                 int olda = a;
126                 int oldb = b;
127                 int oldc = c;
128                 int oldd = d;
129
130                 a = ff(a, b, c, d, x[i + 0], 7, 0xD76AA478);
131                 d = ff(d, a, b, c, x[i + 1], 12, 0xE8C7B756);
132                 c = ff(c, d, a, b, x[i + 2], 17, 0x242070DB);
133                 b = ff(b, c, d, a, x[i + 3], 22, 0xC1BDCEEE);
134                 a = ff(a, b, c, d, x[i + 4], 7, 0xF57C0FAF);
135                 d = ff(d, a, b, c, x[i + 5], 12, 0x4787C62A);
136                 c = ff(c, d, a, b, x[i + 6], 17, 0xA8304613);
137                 b = ff(b, c, d, a, x[i + 7], 22, 0xFD469501);
138                 a = ff(a, b, c, d, x[i + 8], 7, 0x698098D8);
139                 d = ff(d, a, b, c, x[i + 9], 12, 0x8B44F7AF);
140                 c = ff(c, d, a, b, x[i + 10], 17, 0xFFFF5BB1);
141                 b = ff(b, c, d, a, x[i + 11], 22, 0x895CD7BE);
142                 a = ff(a, b, c, d, x[i + 12], 7, 0x6B901122);
143                 d = ff(d, a, b, c, x[i + 13], 12, 0xFD987193);
144                 c = ff(c, d, a, b, x[i + 14], 17, 0xA679438E);
145                 b = ff(b, c, d, a, x[i + 15], 22, 0x49B40821);
146
147                 a = gg(a, b, c, d, x[i + 1], 5, 0xF61E2562);
148                 d = gg(d, a, b, c, x[i + 6], 9, 0xC040B340);
149                 c = gg(c, d, a, b, x[i + 11], 14, 0x265E5A51);
150                 b = gg(b, c, d, a, x[i + 0], 20, 0xE9B6C7AA);
151                 a = gg(a, b, c, d, x[i + 5], 5, 0xD62F105D);
152                 d = gg(d, a, b, c, x[i + 10], 9, 0x02441453);
153                 c = gg(c, d, a, b, x[i + 15], 14, 0xD8A1E681);
154                 b = gg(b, c, d, a, x[i + 4], 20, 0xE7D3FBC8);
155                 a = gg(a, b, c, d, x[i + 9], 5, 0x21E1CDE6);
156                 d = gg(d, a, b, c, x[i + 14], 9, 0xC33707D6);
157                 c = gg(c, d, a, b, x[i + 3], 14, 0xF4D50D87);
158                 b = gg(b, c, d, a, x[i + 8], 20, 0x455A14ED);
159                 a = gg(a, b, c, d, x[i + 13], 5, 0xA9E3E905);
160                 d = gg(d, a, b, c, x[i + 2], 9, 0xFCEFA3F8);
161                 c = gg(c, d, a, b, x[i + 7], 14, 0x676F02D9);
162                 b = gg(b, c, d, a, x[i + 12], 20, 0x8D2A4C8A);
163
164                 a = hh(a, b, c, d, x[i + 5], 4, 0xFFFA3942);
165                 d = hh(d, a, b, c, x[i + 8], 11, 0x8771F681);
166                 c = hh(c, d, a, b, x[i + 11], 16, 0x6D9D6122);
167                 b = hh(b, c, d, a, x[i + 14], 23, 0xFDE5380C);
168                 a = hh(a, b, c, d, x[i + 1], 4, 0xA4BEEA44);
169                 d = hh(d, a, b, c, x[i + 4], 11, 0x4BDECFA9);
170                 c = hh(c, d, a, b, x[i + 7], 16, 0xF6BB4B60);
171                 b = hh(b, c, d, a, x[i + 10], 23, 0xBEBFBC70);
172                 a = hh(a, b, c, d, x[i + 13], 4, 0x289B7EC6);
173                 d = hh(d, a, b, c, x[i + 0], 11, 0xEAA127FA);
174                 c = hh(c, d, a, b, x[i + 3], 16, 0xD4EF3085);
175                 b = hh(b, c, d, a, x[i + 6], 23, 0x04881D05);
176                 a = hh(a, b, c, d, x[i + 9], 4, 0xD9D4D039);
177                 d = hh(d, a, b, c, x[i + 12], 11, 0xE6DB99E5);
178                 c = hh(c, d, a, b, x[i + 15], 16, 0x1FA27CF8);
179                 b = hh(b, c, d, a, x[i + 2], 23, 0xC4AC5665);
180
181                 a = ii(a, b, c, d, x[i + 0], 6, 0xF4292244);
182                 d = ii(d, a, b, c, x[i + 7], 10, 0x432AFF97);
183                 c = ii(c, d, a, b, x[i + 14], 15, 0xAB9423A7);
184                 b = ii(b, c, d, a, x[i + 5], 21, 0xFC93A039);
185                 a = ii(a, b, c, d, x[i + 12], 6, 0x655B59C3);
186                 d = ii(d, a, b, c, x[i + 3], 10, 0x8F0CCC92);
187                 c = ii(c, d, a, b, x[i + 10], 15, 0xFFEFF47D);
188                 b = ii(b, c, d, a, x[i + 1], 21, 0x85845DD1);
189                 a = ii(a, b, c, d, x[i + 8], 6, 0x6FA87E4F);
190                 d = ii(d, a, b, c, x[i + 15], 10, 0xFE2CE6E0);
191                 c = ii(c, d, a, b, x[i + 6], 15, 0xA3014314);
192                 b = ii(b, c, d, a, x[i + 13], 21, 0x4E0811A1);
193                 a = ii(a, b, c, d, x[i + 4], 6, 0xF7537E82);
194                 d = ii(d, a, b, c, x[i + 11], 10, 0xBD3AF235);
195                 c = ii(c, d, a, b, x[i + 2], 15, 0x2AD7D2BB);
196                 b = ii(b, c, d, a, x[i + 9], 21, 0xEB86D391);
197
198                 a = add(a, olda);
199                 b = add(b, oldb);
200                 c = add(c, oldc);
201                 d = add(d, oldd);
202             }
203             return rhex(a) + rhex(b) + rhex(c) + rhex(d);
204         }
205     }
206 }
207
Popular Tags