Source Code Cross Referenced for SHA256Digest.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:        import org.bouncycastle.crypto.digests.GeneralDigest;
004:
005:        /**
006:         * FIPS 180-2 implementation of SHA-256.
007:         *
008:         * <pre>
009:         *         block  word  digest
010:         * SHA-1   512    32    160
011:         * SHA-256 512    32    256
012:         * SHA-384 1024   64    384
013:         * SHA-512 1024   64    512
014:         * </pre>
015:         */
016:        public class SHA256Digest extends GeneralDigest {
017:            private static final int DIGEST_LENGTH = 32;
018:
019:            private int H1, H2, H3, H4, H5, H6, H7, H8;
020:
021:            private int[] X = new int[64];
022:            private int xOff;
023:
024:            /**
025:             * Standard constructor
026:             */
027:            public SHA256Digest() {
028:                reset();
029:            }
030:
031:            /**
032:             * Copy constructor.  This will copy the state of the provided
033:             * message digest.
034:             */
035:            public SHA256Digest(SHA256Digest t) {
036:                super (t);
037:
038:                H1 = t.H1;
039:                H2 = t.H2;
040:                H3 = t.H3;
041:                H4 = t.H4;
042:                H5 = t.H5;
043:                H6 = t.H6;
044:                H7 = t.H7;
045:                H8 = t.H8;
046:
047:                System.arraycopy(t.X, 0, X, 0, t.X.length);
048:                xOff = t.xOff;
049:            }
050:
051:            public String getAlgorithmName() {
052:                return "SHA-256";
053:            }
054:
055:            public int getDigestSize() {
056:                return DIGEST_LENGTH;
057:            }
058:
059:            protected void processWord(byte[] in, int inOff) {
060:                X[xOff++] = ((in[inOff] & 0xff) << 24)
061:                        | ((in[inOff + 1] & 0xff) << 16)
062:                        | ((in[inOff + 2] & 0xff) << 8)
063:                        | ((in[inOff + 3] & 0xff));
064:
065:                if (xOff == 16) {
066:                    processBlock();
067:                }
068:            }
069:
070:            private void unpackWord(int word, byte[] out, int outOff) {
071:                out[outOff] = (byte) (word >>> 24);
072:                out[outOff + 1] = (byte) (word >>> 16);
073:                out[outOff + 2] = (byte) (word >>> 8);
074:                out[outOff + 3] = (byte) word;
075:            }
076:
077:            protected void processLength(long bitLength) {
078:                if (xOff > 14) {
079:                    processBlock();
080:                }
081:
082:                X[14] = (int) (bitLength >>> 32);
083:                X[15] = (int) (bitLength & 0xffffffff);
084:            }
085:
086:            public int doFinal(byte[] out, int outOff) {
087:                finish();
088:
089:                unpackWord(H1, out, outOff);
090:                unpackWord(H2, out, outOff + 4);
091:                unpackWord(H3, out, outOff + 8);
092:                unpackWord(H4, out, outOff + 12);
093:                unpackWord(H5, out, outOff + 16);
094:                unpackWord(H6, out, outOff + 20);
095:                unpackWord(H7, out, outOff + 24);
096:                unpackWord(H8, out, outOff + 28);
097:
098:                reset();
099:
100:                return DIGEST_LENGTH;
101:            }
102:
103:            /**
104:             * reset the chaining variables
105:             */
106:            public void reset() {
107:                super .reset();
108:
109:                /* SHA-256 initial hash value
110:                 * The first 32 bits of the fractional parts of the square roots
111:                 * of the first eight prime numbers
112:                 */
113:
114:                H1 = 0x6a09e667;
115:                H2 = 0xbb67ae85;
116:                H3 = 0x3c6ef372;
117:                H4 = 0xa54ff53a;
118:                H5 = 0x510e527f;
119:                H6 = 0x9b05688c;
120:                H7 = 0x1f83d9ab;
121:                H8 = 0x5be0cd19;
122:
123:                xOff = 0;
124:                for (int i = 0; i != X.length; i++) {
125:                    X[i] = 0;
126:                }
127:            }
128:
129:            protected void processBlock() {
130:                //
131:                // expand 16 word block into 64 word blocks.
132:                //
133:                for (int t = 16; t <= 63; t++) {
134:                    X[t] = Theta1(X[t - 2]) + X[t - 7] + Theta0(X[t - 15])
135:                            + X[t - 16];
136:                }
137:
138:                //
139:                // set up working variables.
140:                //
141:                int a = H1;
142:                int b = H2;
143:                int c = H3;
144:                int d = H4;
145:                int e = H5;
146:                int f = H6;
147:                int g = H7;
148:                int h = H8;
149:
150:                int t = 0;
151:                for (int i = 0; i < 8; i++) {
152:                    // t = 8 * i
153:                    h += Sum1(e) + Ch(e, f, g) + K[t] + X[t++];
154:                    d += h;
155:                    h += Sum0(a) + Maj(a, b, c);
156:
157:                    // t = 8 * i + 1
158:                    g += Sum1(d) + Ch(d, e, f) + K[t] + X[t++];
159:                    c += g;
160:                    g += Sum0(h) + Maj(h, a, b);
161:
162:                    // t = 8 * i + 2
163:                    f += Sum1(c) + Ch(c, d, e) + K[t] + X[t++];
164:                    b += f;
165:                    f += Sum0(g) + Maj(g, h, a);
166:
167:                    // t = 8 * i + 3
168:                    e += Sum1(b) + Ch(b, c, d) + K[t] + X[t++];
169:                    a += e;
170:                    e += Sum0(f) + Maj(f, g, h);
171:
172:                    // t = 8 * i + 4
173:                    d += Sum1(a) + Ch(a, b, c) + K[t] + X[t++];
174:                    h += d;
175:                    d += Sum0(e) + Maj(e, f, g);
176:
177:                    // t = 8 * i + 5
178:                    c += Sum1(h) + Ch(h, a, b) + K[t] + X[t++];
179:                    g += c;
180:                    c += Sum0(d) + Maj(d, e, f);
181:
182:                    // t = 8 * i + 6
183:                    b += Sum1(g) + Ch(g, h, a) + K[t] + X[t++];
184:                    f += b;
185:                    b += Sum0(c) + Maj(c, d, e);
186:
187:                    // t = 8 * i + 7
188:                    a += Sum1(f) + Ch(f, g, h) + K[t] + X[t++];
189:                    e += a;
190:                    a += Sum0(b) + Maj(b, c, d);
191:                }
192:
193:                H1 += a;
194:                H2 += b;
195:                H3 += c;
196:                H4 += d;
197:                H5 += e;
198:                H6 += f;
199:                H7 += g;
200:                H8 += h;
201:
202:                //
203:                // reset the offset and clean out the word buffer.
204:                //
205:                xOff = 0;
206:                for (int i = 0; i < 16; i++) {
207:                    X[i] = 0;
208:                }
209:            }
210:
211:            /* SHA-256 functions */
212:            private int Ch(int x, int y, int z) {
213:                return (x & y) ^ ((~x) & z);
214:            }
215:
216:            private int Maj(int x, int y, int z) {
217:                return (x & y) ^ (x & z) ^ (y & z);
218:            }
219:
220:            private int Sum0(int x) {
221:                return ((x >>> 2) | (x << 30)) ^ ((x >>> 13) | (x << 19))
222:                        ^ ((x >>> 22) | (x << 10));
223:            }
224:
225:            private int Sum1(int x) {
226:                return ((x >>> 6) | (x << 26)) ^ ((x >>> 11) | (x << 21))
227:                        ^ ((x >>> 25) | (x << 7));
228:            }
229:
230:            private int Theta0(int x) {
231:                return ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14))
232:                        ^ (x >>> 3);
233:            }
234:
235:            private int Theta1(int x) {
236:                return ((x >>> 17) | (x << 15)) ^ ((x >>> 19) | (x << 13))
237:                        ^ (x >>> 10);
238:            }
239:
240:            /* SHA-256 Constants
241:             * (represent the first 32 bits of the fractional parts of the
242:             * cube roots of the first sixty-four prime numbers)
243:             */
244:            static final int K[] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf,
245:                    0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
246:                    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74,
247:                    0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
248:                    0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc,
249:                    0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
250:                    0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85,
251:                    0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb,
252:                    0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70,
253:                    0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
254:                    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3,
255:                    0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f,
256:                    0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
257:                    0xc67178f2 };
258:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.