Source Code Cross Referenced for MD5.java in  » 6.0-JDK-Modules » j2me » sun » security » provider » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » sun.security.provider 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)MD5.java	1.35 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.  
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER  
006:         *   
007:         * This program is free software; you can redistribute it and/or  
008:         * modify it under the terms of the GNU General Public License version  
009:         * 2 only, as published by the Free Software Foundation.   
010:         *   
011:         * This program is distributed in the hope that it will be useful, but  
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of  
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
014:         * General Public License version 2 for more details (a copy is  
015:         * included at /legal/license.txt).   
016:         *   
017:         * You should have received a copy of the GNU General Public License  
018:         * version 2 along with this work; if not, write to the Free Software  
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
020:         * 02110-1301 USA   
021:         *   
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa  
023:         * Clara, CA 95054 or visit www.sun.com if you need additional  
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package sun.security.provider;
029:
030:        import java.util.*;
031:        import java.lang.*;
032:        import java.security.*;
033:
034:        /**
035:         * The MD5 class is used to compute an MD5 message digest over a given
036:         * buffer of bytes. It is an implementation of the RSA Data Security Inc
037:         * MD5 algorithim as described in internet RFC 1321.
038:         *
039:         * @version 	1.29 02/02/00
040:         * @author 	Chuck McManis
041:         * @author 	Benjamin Renaud
042:         */
043:
044:        public final class MD5 extends MessageDigestSpi implements  Cloneable {
045:
046:            /** contains the computed message digest */
047:            private byte[] digestBits;
048:
049:            private String algorithm;
050:
051:            private int state[];
052:            private long count; // bit count AND buffer[] index aid
053:            private byte buffer[];
054:            private int transformBuffer[];
055:
056:            private static final int S11 = 7;
057:            private static final int S12 = 12;
058:            private static final int S13 = 17;
059:            private static final int S14 = 22;
060:            private static final int S21 = 5;
061:            private static final int S22 = 9;
062:            private static final int S23 = 14;
063:            private static final int S24 = 20;
064:            private static final int S31 = 4;
065:            private static final int S32 = 11;
066:            private static final int S33 = 16;
067:            private static final int S34 = 23;
068:            private static final int S41 = 6;
069:            private static final int S42 = 10;
070:            private static final int S43 = 15;
071:            private static final int S44 = 21;
072:
073:            private static final int MD5_LENGTH = 16;
074:
075:            /**
076:             * Standard constructor, creates a new MD5 instance, allocates its
077:             * buffers from the heap.
078:             */
079:            public MD5() {
080:                init();
081:            }
082:
083:            private MD5(MD5 md5) {
084:                this ();
085:                this .state = (int[]) md5.state.clone();
086:                this .transformBuffer = (int[]) md5.transformBuffer.clone();
087:                this .buffer = (byte[]) md5.buffer.clone();
088:                this .digestBits = (byte[]) md5.digestBits.clone();
089:                this .count = md5.count;
090:            }
091:
092:            /* **********************************************************
093:             * The MD5 Functions. The results of this
094:             * implementation were checked against the RSADSI version.
095:             * **********************************************************
096:             */
097:
098:            private int FF(int a, int b, int c, int d, int x, int s, int ac) {
099:                a += ((b & c) | ((~b) & d)) + x + ac;
100:                return ((a << s) | (a >>> (32 - s))) + b;
101:            }
102:
103:            private int GG(int a, int b, int c, int d, int x, int s, int ac) {
104:                a += ((b & d) | (c & (~d))) + x + ac;
105:                return ((a << s) | (a >>> (32 - s))) + b;
106:            }
107:
108:            private int HH(int a, int b, int c, int d, int x, int s, int ac) {
109:                a += ((b ^ c) ^ d) + x + ac;
110:                return ((a << s) | (a >>> (32 - s))) + b;
111:            }
112:
113:            private int II(int a, int b, int c, int d, int x, int s, int ac) {
114:                a += (c ^ (b | (~d))) + x + ac;
115:                return ((a << s) | (a >>> (32 - s))) + b;
116:            }
117:
118:            /**
119:             * This is where the functions come together as the generic MD5
120:             * transformation operation, it is called by update() which is
121:             * synchronized (to protect transformBuffer).  It consumes sixteen
122:             * bytes from the buffer, beginning at the specified offset.
123:             */
124:            void transform(byte buf[], int offset) {
125:                int a, b, c, d;
126:                int x[] = transformBuffer;
127:
128:                a = state[0];
129:                b = state[1];
130:                c = state[2];
131:                d = state[3];
132:
133:                for (int i = 0; i < 16; i++) {
134:                    x[i] = (int) buf[i * 4 + offset] & 0xff;
135:                    for (int j = 1; j < 4; j++) {
136:                        x[i] += ((int) buf[i * 4 + j + offset] & 0xff) << (j * 8);
137:                    }
138:                }
139:
140:                /* Round 1 */
141:                a = FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
142:                d = FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
143:                c = FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
144:                b = FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
145:                a = FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
146:                d = FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
147:                c = FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
148:                b = FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
149:                a = FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
150:                d = FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
151:                c = FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
152:                b = FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
153:                a = FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
154:                d = FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
155:                c = FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
156:                b = FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
157:
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:            /**
219:             * Initialize the MD5 state information and reset the bit count
220:             * to 0. Given this implementation you are constrained to counting
221:             * 2^64 bits.
222:             */
223:            public void init() {
224:                state = new int[4];
225:                transformBuffer = new int[16];
226:                buffer = new byte[64];
227:                digestBits = new byte[16];
228:                count = 0;
229:                // Load magic initialization constants.
230:                state[0] = 0x67452301;
231:                state[1] = 0xefcdab89;
232:                state[2] = 0x98badcfe;
233:                state[3] = 0x10325476;
234:                for (int i = 0; i < digestBits.length; i++)
235:                    digestBits[i] = 0;
236:            }
237:
238:            protected void engineReset() {
239:                init();
240:            }
241:
242:            /**
243:             * Return the digest length in bytes
244:             */
245:            protected int engineGetDigestLength() {
246:                return (MD5_LENGTH);
247:            }
248:
249:            /**
250:             * Update adds the passed byte to the digested data.
251:             */
252:            protected synchronized void engineUpdate(byte b) {
253:                int index;
254:
255:                index = (int) ((count >>> 3) & 0x3f);
256:                count += 8;
257:                buffer[index] = b;
258:                if (index >= 63) {
259:                    transform(buffer, 0);
260:                }
261:            }
262:
263:            /**
264:             * Update adds the selected part of an array of bytes to the digest.
265:             * This version is more efficient than the byte-at-a-time version;
266:             * it avoids data copies and reduces per-byte call overhead.
267:             */
268:            protected synchronized void engineUpdate(byte input[], int offset,
269:                    int len) {
270:                int i;
271:
272:                for (i = offset; len > 0;) {
273:                    int index = (int) ((count >>> 3) & 0x3f);
274:
275:                    if (index == 0 && len > 64) {
276:                        count += (64 * 8);
277:                        transform(input, i);
278:                        len -= 64;
279:                        i += 64;
280:                    } else {
281:                        count += 8;
282:                        buffer[index] = input[i];
283:                        if (index >= 63)
284:                            transform(buffer, 0);
285:                        i++;
286:                        len--;
287:                    }
288:                }
289:            }
290:
291:            /**
292:             * Perform the final computations, any buffered bytes are added
293:             * to the digest, the count is added to the digest, and the resulting
294:             * digest is stored. After calling final you will need to call
295:             * init() again to do another digest.
296:             */
297:            private void finish() {
298:                byte bits[] = new byte[8];
299:                byte padding[];
300:                int i, index, padLen;
301:
302:                for (i = 0; i < 8; i++) {
303:                    bits[i] = (byte) ((count >>> (i * 8)) & 0xff);
304:                }
305:
306:                index = (int) (count >> 3) & 0x3f;
307:                padLen = (index < 56) ? (56 - index) : (120 - index);
308:                padding = new byte[padLen];
309:                padding[0] = (byte) 0x80;
310:                engineUpdate(padding, 0, padding.length);
311:                engineUpdate(bits, 0, bits.length);
312:
313:                for (i = 0; i < 4; i++) {
314:                    for (int j = 0; j < 4; j++) {
315:                        digestBits[i * 4 + j] = (byte) ((state[i] >>> (j * 8)) & 0xff);
316:                    }
317:                }
318:            }
319:
320:            /**
321:             */
322:            protected byte[] engineDigest() {
323:                finish();
324:
325:                byte[] result = new byte[MD5_LENGTH];
326:                System.arraycopy(digestBits, 0, result, 0, MD5_LENGTH);
327:
328:                init();
329:
330:                return result;
331:            }
332:
333:            /**
334:             */
335:            protected int engineDigest(byte[] buf, int offset, int len)
336:                    throws DigestException {
337:                finish();
338:
339:                if (len < MD5_LENGTH)
340:                    throw new DigestException("partial digests not returned");
341:                if (buf.length - offset < MD5_LENGTH)
342:                    throw new DigestException(
343:                            "insufficient space in the output "
344:                                    + "buffer to store the digest");
345:
346:                System.arraycopy(digestBits, 0, buf, offset, MD5_LENGTH);
347:
348:                init();
349:
350:                return MD5_LENGTH;
351:            }
352:
353:            /*
354:             * Clones this object.
355:             */
356:            public Object clone() {
357:                MD5 that = null;
358:                try {
359:                    that = (MD5) super .clone();
360:                    that.state = (int[]) this .state.clone();
361:                    that.transformBuffer = (int[]) this .transformBuffer.clone();
362:                    that.buffer = (byte[]) this .buffer.clone();
363:                    that.digestBits = (byte[]) this .digestBits.clone();
364:                    that.count = this .count;
365:                    return that;
366:                } catch (CloneNotSupportedException e) {
367:                }
368:                return that;
369:            }
370:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.