Source Code Cross Referenced for MD4Digest.java in  » Security » Bouncy-Castle » org » bouncycastle » crypto » digests » 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 » Security » Bouncy Castle » org.bouncycastle.crypto.digests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.crypto.digests;
002:
003:        /**
004:         * implementation of MD4 as RFC 1320 by R. Rivest, MIT Laboratory for
005:         * Computer Science and RSA Data Security, Inc.
006:         * <p>
007:         * <b>NOTE</b>: This algorithm is only included for backwards compatability
008:         * with legacy applications, it's not secure, don't use it for anything new!
009:         */
010:        public class MD4Digest extends GeneralDigest {
011:            private static final int DIGEST_LENGTH = 16;
012:
013:            private int H1, H2, H3, H4; // IV's
014:
015:            private int[] X = new int[16];
016:            private int xOff;
017:
018:            /**
019:             * Standard constructor
020:             */
021:            public MD4Digest() {
022:                reset();
023:            }
024:
025:            /**
026:             * Copy constructor.  This will copy the state of the provided
027:             * message digest.
028:             */
029:            public MD4Digest(MD4Digest t) {
030:                super (t);
031:
032:                H1 = t.H1;
033:                H2 = t.H2;
034:                H3 = t.H3;
035:                H4 = t.H4;
036:
037:                System.arraycopy(t.X, 0, X, 0, t.X.length);
038:                xOff = t.xOff;
039:            }
040:
041:            public String getAlgorithmName() {
042:                return "MD4";
043:            }
044:
045:            public int getDigestSize() {
046:                return DIGEST_LENGTH;
047:            }
048:
049:            protected void processWord(byte[] in, int inOff) {
050:                X[xOff++] = (in[inOff] & 0xff) | ((in[inOff + 1] & 0xff) << 8)
051:                        | ((in[inOff + 2] & 0xff) << 16)
052:                        | ((in[inOff + 3] & 0xff) << 24);
053:
054:                if (xOff == 16) {
055:                    processBlock();
056:                }
057:            }
058:
059:            protected void processLength(long bitLength) {
060:                if (xOff > 14) {
061:                    processBlock();
062:                }
063:
064:                X[14] = (int) (bitLength & 0xffffffff);
065:                X[15] = (int) (bitLength >>> 32);
066:            }
067:
068:            private void unpackWord(int word, byte[] out, int outOff) {
069:                out[outOff] = (byte) word;
070:                out[outOff + 1] = (byte) (word >>> 8);
071:                out[outOff + 2] = (byte) (word >>> 16);
072:                out[outOff + 3] = (byte) (word >>> 24);
073:            }
074:
075:            public int doFinal(byte[] out, int outOff) {
076:                finish();
077:
078:                unpackWord(H1, out, outOff);
079:                unpackWord(H2, out, outOff + 4);
080:                unpackWord(H3, out, outOff + 8);
081:                unpackWord(H4, out, outOff + 12);
082:
083:                reset();
084:
085:                return DIGEST_LENGTH;
086:            }
087:
088:            /**
089:             * reset the chaining variables to the IV values.
090:             */
091:            public void reset() {
092:                super .reset();
093:
094:                H1 = 0x67452301;
095:                H2 = 0xefcdab89;
096:                H3 = 0x98badcfe;
097:                H4 = 0x10325476;
098:
099:                xOff = 0;
100:
101:                for (int i = 0; i != X.length; i++) {
102:                    X[i] = 0;
103:                }
104:            }
105:
106:            //
107:            // round 1 left rotates
108:            //
109:            private static final int S11 = 3;
110:            private static final int S12 = 7;
111:            private static final int S13 = 11;
112:            private static final int S14 = 19;
113:
114:            //
115:            // round 2 left rotates
116:            //
117:            private static final int S21 = 3;
118:            private static final int S22 = 5;
119:            private static final int S23 = 9;
120:            private static final int S24 = 13;
121:
122:            //
123:            // round 3 left rotates
124:            //
125:            private static final int S31 = 3;
126:            private static final int S32 = 9;
127:            private static final int S33 = 11;
128:            private static final int S34 = 15;
129:
130:            /*
131:             * rotate int x left n bits.
132:             */
133:            private int rotateLeft(int x, int n) {
134:                return (x << n) | (x >>> (32 - n));
135:            }
136:
137:            /*
138:             * F, G, H and I are the basic MD4 functions.
139:             */
140:            private int F(int u, int v, int w) {
141:                return (u & v) | (~u & w);
142:            }
143:
144:            private int G(int u, int v, int w) {
145:                return (u & v) | (u & w) | (v & w);
146:            }
147:
148:            private int H(int u, int v, int w) {
149:                return u ^ v ^ w;
150:            }
151:
152:            protected void processBlock() {
153:                int a = H1;
154:                int b = H2;
155:                int c = H3;
156:                int d = H4;
157:
158:                //
159:                // Round 1 - F cycle, 16 times.
160:                //
161:                a = rotateLeft(a + F(b, c, d) + X[0], S11);
162:                d = rotateLeft(d + F(a, b, c) + X[1], S12);
163:                c = rotateLeft(c + F(d, a, b) + X[2], S13);
164:                b = rotateLeft(b + F(c, d, a) + X[3], S14);
165:                a = rotateLeft(a + F(b, c, d) + X[4], S11);
166:                d = rotateLeft(d + F(a, b, c) + X[5], S12);
167:                c = rotateLeft(c + F(d, a, b) + X[6], S13);
168:                b = rotateLeft(b + F(c, d, a) + X[7], S14);
169:                a = rotateLeft(a + F(b, c, d) + X[8], S11);
170:                d = rotateLeft(d + F(a, b, c) + X[9], S12);
171:                c = rotateLeft(c + F(d, a, b) + X[10], S13);
172:                b = rotateLeft(b + F(c, d, a) + X[11], S14);
173:                a = rotateLeft(a + F(b, c, d) + X[12], S11);
174:                d = rotateLeft(d + F(a, b, c) + X[13], S12);
175:                c = rotateLeft(c + F(d, a, b) + X[14], S13);
176:                b = rotateLeft(b + F(c, d, a) + X[15], S14);
177:
178:                //
179:                // Round 2 - G cycle, 16 times.
180:                //
181:                a = rotateLeft(a + G(b, c, d) + X[0] + 0x5a827999, S21);
182:                d = rotateLeft(d + G(a, b, c) + X[4] + 0x5a827999, S22);
183:                c = rotateLeft(c + G(d, a, b) + X[8] + 0x5a827999, S23);
184:                b = rotateLeft(b + G(c, d, a) + X[12] + 0x5a827999, S24);
185:                a = rotateLeft(a + G(b, c, d) + X[1] + 0x5a827999, S21);
186:                d = rotateLeft(d + G(a, b, c) + X[5] + 0x5a827999, S22);
187:                c = rotateLeft(c + G(d, a, b) + X[9] + 0x5a827999, S23);
188:                b = rotateLeft(b + G(c, d, a) + X[13] + 0x5a827999, S24);
189:                a = rotateLeft(a + G(b, c, d) + X[2] + 0x5a827999, S21);
190:                d = rotateLeft(d + G(a, b, c) + X[6] + 0x5a827999, S22);
191:                c = rotateLeft(c + G(d, a, b) + X[10] + 0x5a827999, S23);
192:                b = rotateLeft(b + G(c, d, a) + X[14] + 0x5a827999, S24);
193:                a = rotateLeft(a + G(b, c, d) + X[3] + 0x5a827999, S21);
194:                d = rotateLeft(d + G(a, b, c) + X[7] + 0x5a827999, S22);
195:                c = rotateLeft(c + G(d, a, b) + X[11] + 0x5a827999, S23);
196:                b = rotateLeft(b + G(c, d, a) + X[15] + 0x5a827999, S24);
197:
198:                //
199:                // Round 3 - H cycle, 16 times.
200:                //
201:                a = rotateLeft(a + H(b, c, d) + X[0] + 0x6ed9eba1, S31);
202:                d = rotateLeft(d + H(a, b, c) + X[8] + 0x6ed9eba1, S32);
203:                c = rotateLeft(c + H(d, a, b) + X[4] + 0x6ed9eba1, S33);
204:                b = rotateLeft(b + H(c, d, a) + X[12] + 0x6ed9eba1, S34);
205:                a = rotateLeft(a + H(b, c, d) + X[2] + 0x6ed9eba1, S31);
206:                d = rotateLeft(d + H(a, b, c) + X[10] + 0x6ed9eba1, S32);
207:                c = rotateLeft(c + H(d, a, b) + X[6] + 0x6ed9eba1, S33);
208:                b = rotateLeft(b + H(c, d, a) + X[14] + 0x6ed9eba1, S34);
209:                a = rotateLeft(a + H(b, c, d) + X[1] + 0x6ed9eba1, S31);
210:                d = rotateLeft(d + H(a, b, c) + X[9] + 0x6ed9eba1, S32);
211:                c = rotateLeft(c + H(d, a, b) + X[5] + 0x6ed9eba1, S33);
212:                b = rotateLeft(b + H(c, d, a) + X[13] + 0x6ed9eba1, S34);
213:                a = rotateLeft(a + H(b, c, d) + X[3] + 0x6ed9eba1, S31);
214:                d = rotateLeft(d + H(a, b, c) + X[11] + 0x6ed9eba1, S32);
215:                c = rotateLeft(c + H(d, a, b) + X[7] + 0x6ed9eba1, S33);
216:                b = rotateLeft(b + H(c, d, a) + X[15] + 0x6ed9eba1, S34);
217:
218:                H1 += a;
219:                H2 += b;
220:                H3 += c;
221:                H4 += d;
222:
223:                //
224:                // reset the offset and clean out the word buffer.
225:                //
226:                xOff = 0;
227:                for (int i = 0; i != X.length; i++) {
228:                    X[i] = 0;
229:                }
230:            }
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.