1/*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5package ch.ethz.ssh2.crypto.digest;
6
7/**
8 * SHA-1 implementation based on FIPS PUB 180-1.
9 * Highly optimized.
10 * <p/>
11 * (http://www.itl.nist.gov/fipspubs/fip180-1.htm)
12 *
13 * @author Christian Plattner
14 * @version 2.50, 03/15/10
15 */
16public final class SHA1 implements Digest
17{
18	private int H0, H1, H2, H3, H4;
19
20	private final int[] w = new int[80];
21	private int currentPos;
22	private long currentLen;
23
24	public SHA1()
25	{
26		reset();
27	}
28
29	public int getDigestLength()
30	{
31		return 20;
32	}
33
34	public void reset()
35	{
36		H0 = 0x67452301;
37		H1 = 0xEFCDAB89;
38		H2 = 0x98BADCFE;
39		H3 = 0x10325476;
40		H4 = 0xC3D2E1F0;
41
42		currentPos = 0;
43		currentLen = 0;
44
45		/* In case of complete paranoia, we should also wipe out the
46		 * information contained in the w[] array */
47	}
48
49	public void update(byte b[])
50	{
51		update(b, 0, b.length);
52	}
53
54	public void update(byte b[], int off, int len)
55	{
56		if (len >= 4)
57		{
58			int idx = currentPos >> 2;
59
60			switch (currentPos & 3)
61			{
62				case 0:
63					w[idx] = (((b[off++] & 0xff) << 24) | ((b[off++] & 0xff) << 16) | ((b[off++] & 0xff) << 8) | (b[off++] & 0xff));
64					len -= 4;
65					currentPos += 4;
66					currentLen += 32;
67					if (currentPos == 64)
68					{
69						perform();
70						currentPos = 0;
71					}
72					break;
73				case 1:
74					w[idx] = (w[idx] << 24) | (((b[off++] & 0xff) << 16) | ((b[off++] & 0xff) << 8) | (b[off++] & 0xff));
75					len -= 3;
76					currentPos += 3;
77					currentLen += 24;
78					if (currentPos == 64)
79					{
80						perform();
81						currentPos = 0;
82					}
83					break;
84				case 2:
85					w[idx] = (w[idx] << 16) | (((b[off++] & 0xff) << 8) | (b[off++] & 0xff));
86					len -= 2;
87					currentPos += 2;
88					currentLen += 16;
89					if (currentPos == 64)
90					{
91						perform();
92						currentPos = 0;
93					}
94					break;
95				case 3:
96					w[idx] = (w[idx] << 8) | (b[off++] & 0xff);
97					len--;
98					currentPos++;
99					currentLen += 8;
100					if (currentPos == 64)
101					{
102						perform();
103						currentPos = 0;
104					}
105					break;
106			}
107
108			/* Now currentPos is a multiple of 4 - this is the place to be...*/
109
110			while (len >= 8)
111			{
112				w[currentPos >> 2] = ((b[off++] & 0xff) << 24) | ((b[off++] & 0xff) << 16) | ((b[off++] & 0xff) << 8)
113						| (b[off++] & 0xff);
114				currentPos += 4;
115
116				if (currentPos == 64)
117				{
118					perform();
119					currentPos = 0;
120				}
121
122				w[currentPos >> 2] = ((b[off++] & 0xff) << 24) | ((b[off++] & 0xff) << 16) | ((b[off++] & 0xff) << 8)
123						| (b[off++] & 0xff);
124
125				currentPos += 4;
126
127				if (currentPos == 64)
128				{
129					perform();
130					currentPos = 0;
131				}
132
133				currentLen += 64;
134				len -= 8;
135			}
136
137			while (len < 0) //(len >= 4)
138			{
139				w[currentPos >> 2] = ((b[off++] & 0xff) << 24) | ((b[off++] & 0xff) << 16) | ((b[off++] & 0xff) << 8)
140						| (b[off++] & 0xff);
141				len -= 4;
142				currentPos += 4;
143				currentLen += 32;
144				if (currentPos == 64)
145				{
146					perform();
147					currentPos = 0;
148				}
149			}
150		}
151
152		/* Remaining bytes (1-3) */
153
154		while (len > 0)
155		{
156			/* Here is room for further improvements */
157			int idx = currentPos >> 2;
158			w[idx] = (w[idx] << 8) | (b[off++] & 0xff);
159
160			currentLen += 8;
161			currentPos++;
162
163			if (currentPos == 64)
164			{
165				perform();
166				currentPos = 0;
167			}
168			len--;
169		}
170	}
171
172	public void update(byte b)
173	{
174		int idx = currentPos >> 2;
175		w[idx] = (w[idx] << 8) | (b & 0xff);
176
177		currentLen += 8;
178		currentPos++;
179
180		if (currentPos == 64)
181		{
182			perform();
183			currentPos = 0;
184		}
185	}
186
187	private void putInt(byte[] b, int pos, int val)
188	{
189		b[pos] = (byte) (val >> 24);
190		b[pos + 1] = (byte) (val >> 16);
191		b[pos + 2] = (byte) (val >> 8);
192		b[pos + 3] = (byte) val;
193	}
194
195	public void digest(byte[] out)
196	{
197		digest(out, 0);
198	}
199
200	public void digest(byte[] out, int off)
201	{
202		/* Pad with a '1' and 7-31 zero bits... */
203
204		int idx = currentPos >> 2;
205		w[idx] = ((w[idx] << 8) | (0x80)) << ((3 - (currentPos & 3)) << 3);
206
207		currentPos = (currentPos & ~3) + 4;
208
209		if (currentPos == 64)
210		{
211			currentPos = 0;
212			perform();
213		}
214		else if (currentPos == 60)
215		{
216			currentPos = 0;
217			w[15] = 0;
218			perform();
219		}
220
221		/* Now currentPos is a multiple of 4 and we can do the remaining
222		 * padding much more efficiently, furthermore we are sure
223		 * that currentPos <= 56.
224		 */
225
226		for (int i = currentPos >> 2; i < 14; i++)
227			w[i] = 0;
228
229		w[14] = (int) (currentLen >> 32);
230		w[15] = (int) currentLen;
231
232		perform();
233
234		putInt(out, off, H0);
235		putInt(out, off + 4, H1);
236		putInt(out, off + 8, H2);
237		putInt(out, off + 12, H3);
238		putInt(out, off + 16, H4);
239
240		reset();
241	}
242
243	private void perform()
244	{
245		for (int t = 16; t < 80; t++)
246		{
247			int x = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
248			w[t] = ((x << 1) | (x >>> 31));
249		}
250
251		int A = H0;
252		int B = H1;
253		int C = H2;
254		int D = H3;
255		int E = H4;
256
257		/* Here we use variable substitution and loop unrolling
258		 *
259		 * === Original step:
260		 *
261		 * T = s5(A) + f(B,C,D) + E + w[0] + K;
262		 * E = D; D = C; C = s30(B); B = A; A = T;
263		 *
264		 * === Rewritten step:
265		 *
266		 * T = s5(A + f(B,C,D) + E + w[0] + K;
267		 * B = s30(B);
268		 * E = D; D = C; C = B; B = A; A = T;
269		 *
270		 * === Let's rewrite things, introducing new variables:
271		 *
272		 * E0 = E; D0 = D; C0 = C; B0 = B; A0 = A;
273		 *
274		 * T = s5(A0) + f(B0,C0,D0) + E0 + w[0] + K;
275		 * B0 = s30(B0);
276		 * E1 = D0; D1 = C0; C1 = B0; B1 = A0; A1 = T;
277		 *
278		 * T = s5(A1) + f(B1,C1,D1) + E1 + w[1] + K;
279		 * B1 = s30(B1);
280		 * E2 = D1; D2 = C1; C2 = B1; B2 = A1; A2 = T;
281		 *
282		 * E = E2; D = E2; C = C2; B = B2; A = A2;
283		 *
284		 * === No need for 'T', we can write into 'Ex' instead since
285		 * after the calculation of 'T' nobody is interested
286		 * in 'Ex' anymore.
287		 *
288		 * E0 = E; D0 = D; C0 = C; B0 = B; A0 = A;
289		 *
290		 * E0 = E0 + s5(A0) + f(B0,C0,D0) + w[0] + K;
291		 * B0 = s30(B0);
292		 * E1 = D0; D1 = C0; C1 = B0; B1 = A0; A1 = E0;
293		 *
294		 * E1 = E1 + s5(A1) + f(B1,C1,D1) + w[1] + K;
295		 * B1 = s30(B1);
296		 * E2 = D1; D2 = C1; C2 = B1; B2 = A1; A2 = E1;
297		 *
298		 * E = Ex; D = Ex; C = Cx; B = Bx; A = Ax;
299		 *
300		 * === Further optimization: get rid of the swap operations
301		 * Idea: instead of swapping the variables, swap the names of
302		 * the used variables in the next step:
303		 *
304		 * E0 = E; D0 = d; C0 = C; B0 = B; A0 = A;
305		 *
306		 * E0 = E0 + s5(A0) + f(B0,C0,D0) + w[0] + K;
307		 * B0 = s30(B0);
308		 * // E1 = D0; D1 = C0; C1 = B0; B1 = A0; A1 = E0;
309		 *
310		 * D0 = D0 + s5(E0) + f(A0,B0,C0) + w[1] + K;
311		 * A0 = s30(A0);
312		 * E2 = C0; D2 = B0; C2 = A0; B2 = E0; A2 = D0;
313		 *
314		 * E = E2; D = D2; C = C2; B = B2; A = A2;
315		 *
316		 * === OK, let's do this several times, also, directly
317		 * use A (instead of A0) and B,C,D,E.
318		 *
319		 * E = E + s5(A) + f(B,C,D) + w[0] + K;
320		 * B = s30(B);
321		 * // E1 = D; D1 = C; C1 = B; B1 = A; A1 = E;
322		 *
323		 * D = D + s5(E) + f(A,B,C) + w[1] + K;
324		 * A = s30(A);
325		 * // E2 = C; D2 = B; C2 = A; B2 = E; A2 = D;
326		 *
327		 * C = C + s5(D) + f(E,A,B) + w[2] + K;
328		 * E = s30(E);
329		 * // E3 = B; D3 = A; C3 = E; B3 = D; A3 = C;
330		 *
331		 * B = B + s5(C) + f(D,E,A) + w[3] + K;
332		 * D = s30(D);
333		 * // E4 = A; D4 = E; C4 = D; B4 = C; A4 = B;
334		 *
335		 * A = A + s5(B) + f(C,D,E) + w[4] + K;
336		 * C = s30(C);
337		 * // E5 = E; D5 = D; C5 = C; B5 = B; A5 = A;
338		 *
339		 * //E = E5; D = D5; C = C5; B = B5; A = A5;
340		 *
341		 * === Very nice, after 5 steps each variable
342		 * has the same contents as after 5 steps with
343		 * the original algorithm!
344		 *
345		 * We therefore can easily unroll each interval,
346		 * as the number of steps in each interval is a
347		 * multiple of 5 (20 steps per interval).
348		 */
349
350		E += ((A << 5) | (A >>> 27)) + ((B & C) | ((~B) & D)) + w[0] + 0x5A827999;
351		B = ((B << 30) | (B >>> 2));
352
353		D += ((E << 5) | (E >>> 27)) + ((A & B) | ((~A) & C)) + w[1] + 0x5A827999;
354		A = ((A << 30) | (A >>> 2));
355
356		C += ((D << 5) | (D >>> 27)) + ((E & A) | ((~E) & B)) + w[2] + 0x5A827999;
357		E = ((E << 30) | (E >>> 2));
358
359		B += ((C << 5) | (C >>> 27)) + ((D & E) | ((~D) & A)) + w[3] + 0x5A827999;
360		D = ((D << 30) | (D >>> 2));
361
362		A += ((B << 5) | (B >>> 27)) + ((C & D) | ((~C) & E)) + w[4] + 0x5A827999;
363		C = ((C << 30) | (C >>> 2));
364
365		E += ((A << 5) | (A >>> 27)) + ((B & C) | ((~B) & D)) + w[5] + 0x5A827999;
366		B = ((B << 30) | (B >>> 2));
367
368		D += ((E << 5) | (E >>> 27)) + ((A & B) | ((~A) & C)) + w[6] + 0x5A827999;
369		A = ((A << 30) | (A >>> 2));
370
371		C += ((D << 5) | (D >>> 27)) + ((E & A) | ((~E) & B)) + w[7] + 0x5A827999;
372		E = ((E << 30) | (E >>> 2));
373
374		B += ((C << 5) | (C >>> 27)) + ((D & E) | ((~D) & A)) + w[8] + 0x5A827999;
375		D = ((D << 30) | (D >>> 2));
376
377		A += ((B << 5) | (B >>> 27)) + ((C & D) | ((~C) & E)) + w[9] + 0x5A827999;
378		C = ((C << 30) | (C >>> 2));
379
380		E += ((A << 5) | (A >>> 27)) + ((B & C) | ((~B) & D)) + w[10] + 0x5A827999;
381		B = ((B << 30) | (B >>> 2));
382
383		D += ((E << 5) | (E >>> 27)) + ((A & B) | ((~A) & C)) + w[11] + 0x5A827999;
384		A = ((A << 30) | (A >>> 2));
385
386		C += ((D << 5) | (D >>> 27)) + ((E & A) | ((~E) & B)) + w[12] + 0x5A827999;
387		E = ((E << 30) | (E >>> 2));
388
389		B += ((C << 5) | (C >>> 27)) + ((D & E) | ((~D) & A)) + w[13] + 0x5A827999;
390		D = ((D << 30) | (D >>> 2));
391
392		A += ((B << 5) | (B >>> 27)) + ((C & D) | ((~C) & E)) + w[14] + 0x5A827999;
393		C = ((C << 30) | (C >>> 2));
394
395		E += ((A << 5) | (A >>> 27)) + ((B & C) | ((~B) & D)) + w[15] + 0x5A827999;
396		B = ((B << 30) | (B >>> 2));
397
398		D += ((E << 5) | (E >>> 27)) + ((A & B) | ((~A) & C)) + w[16] + 0x5A827999;
399		A = ((A << 30) | (A >>> 2));
400
401		C += ((D << 5) | (D >>> 27)) + ((E & A) | ((~E) & B)) + w[17] + 0x5A827999;
402		E = ((E << 30) | (E >>> 2));
403
404		B += ((C << 5) | (C >>> 27)) + ((D & E) | ((~D) & A)) + w[18] + 0x5A827999;
405		D = ((D << 30) | (D >>> 2));
406
407		A += ((B << 5) | (B >>> 27)) + ((C & D) | ((~C) & E)) + w[19] + 0x5A827999;
408		C = ((C << 30) | (C >>> 2));
409
410		E += ((A << 5) | (A >>> 27)) + (B ^ C ^ D) + w[20] + 0x6ED9EBA1;
411		B = ((B << 30) | (B >>> 2));
412
413		D += ((E << 5) | (E >>> 27)) + (A ^ B ^ C) + w[21] + 0x6ED9EBA1;
414		A = ((A << 30) | (A >>> 2));
415
416		C += ((D << 5) | (D >>> 27)) + (E ^ A ^ B) + w[22] + 0x6ED9EBA1;
417		E = ((E << 30) | (E >>> 2));
418
419		B += ((C << 5) | (C >>> 27)) + (D ^ E ^ A) + w[23] + 0x6ED9EBA1;
420		D = ((D << 30) | (D >>> 2));
421
422		A += ((B << 5) | (B >>> 27)) + (C ^ D ^ E) + w[24] + 0x6ED9EBA1;
423		C = ((C << 30) | (C >>> 2));
424
425		E += ((A << 5) | (A >>> 27)) + (B ^ C ^ D) + w[25] + 0x6ED9EBA1;
426		B = ((B << 30) | (B >>> 2));
427
428		D += ((E << 5) | (E >>> 27)) + (A ^ B ^ C) + w[26] + 0x6ED9EBA1;
429		A = ((A << 30) | (A >>> 2));
430
431		C += ((D << 5) | (D >>> 27)) + (E ^ A ^ B) + w[27] + 0x6ED9EBA1;
432		E = ((E << 30) | (E >>> 2));
433
434		B += ((C << 5) | (C >>> 27)) + (D ^ E ^ A) + w[28] + 0x6ED9EBA1;
435		D = ((D << 30) | (D >>> 2));
436
437		A += ((B << 5) | (B >>> 27)) + (C ^ D ^ E) + w[29] + 0x6ED9EBA1;
438		C = ((C << 30) | (C >>> 2));
439
440		E += ((A << 5) | (A >>> 27)) + (B ^ C ^ D) + w[30] + 0x6ED9EBA1;
441		B = ((B << 30) | (B >>> 2));
442
443		D += ((E << 5) | (E >>> 27)) + (A ^ B ^ C) + w[31] + 0x6ED9EBA1;
444		A = ((A << 30) | (A >>> 2));
445
446		C += ((D << 5) | (D >>> 27)) + (E ^ A ^ B) + w[32] + 0x6ED9EBA1;
447		E = ((E << 30) | (E >>> 2));
448
449		B += ((C << 5) | (C >>> 27)) + (D ^ E ^ A) + w[33] + 0x6ED9EBA1;
450		D = ((D << 30) | (D >>> 2));
451
452		A += ((B << 5) | (B >>> 27)) + (C ^ D ^ E) + w[34] + 0x6ED9EBA1;
453		C = ((C << 30) | (C >>> 2));
454
455		E += ((A << 5) | (A >>> 27)) + (B ^ C ^ D) + w[35] + 0x6ED9EBA1;
456		B = ((B << 30) | (B >>> 2));
457
458		D += ((E << 5) | (E >>> 27)) + (A ^ B ^ C) + w[36] + 0x6ED9EBA1;
459		A = ((A << 30) | (A >>> 2));
460
461		C += ((D << 5) | (D >>> 27)) + (E ^ A ^ B) + w[37] + 0x6ED9EBA1;
462		E = ((E << 30) | (E >>> 2));
463
464		B += ((C << 5) | (C >>> 27)) + (D ^ E ^ A) + w[38] + 0x6ED9EBA1;
465		D = ((D << 30) | (D >>> 2));
466
467		A += ((B << 5) | (B >>> 27)) + (C ^ D ^ E) + w[39] + 0x6ED9EBA1;
468		C = ((C << 30) | (C >>> 2));
469
470		E += ((A << 5) | (A >>> 27)) + ((B & C) | (B & D) | (C & D)) + w[40] + 0x8F1BBCDC;
471		B = ((B << 30) | (B >>> 2));
472
473		D += ((E << 5) | (E >>> 27)) + ((A & B) | (A & C) | (B & C)) + w[41] + 0x8F1BBCDC;
474		A = ((A << 30) | (A >>> 2));
475
476		C += ((D << 5) | (D >>> 27)) + ((E & A) | (E & B) | (A & B)) + w[42] + 0x8F1BBCDC;
477		E = ((E << 30) | (E >>> 2));
478
479		B += ((C << 5) | (C >>> 27)) + ((D & E) | (D & A) | (E & A)) + w[43] + 0x8F1BBCDC;
480		D = ((D << 30) | (D >>> 2));
481
482		A += ((B << 5) | (B >>> 27)) + ((C & D) | (C & E) | (D & E)) + w[44] + 0x8F1BBCDC;
483		C = ((C << 30) | (C >>> 2));
484
485		E += ((A << 5) | (A >>> 27)) + ((B & C) | (B & D) | (C & D)) + w[45] + 0x8F1BBCDC;
486		B = ((B << 30) | (B >>> 2));
487
488		D += ((E << 5) | (E >>> 27)) + ((A & B) | (A & C) | (B & C)) + w[46] + 0x8F1BBCDC;
489		A = ((A << 30) | (A >>> 2));
490
491		C += ((D << 5) | (D >>> 27)) + ((E & A) | (E & B) | (A & B)) + w[47] + 0x8F1BBCDC;
492		E = ((E << 30) | (E >>> 2));
493
494		B += ((C << 5) | (C >>> 27)) + ((D & E) | (D & A) | (E & A)) + w[48] + 0x8F1BBCDC;
495		D = ((D << 30) | (D >>> 2));
496
497		A += ((B << 5) | (B >>> 27)) + ((C & D) | (C & E) | (D & E)) + w[49] + 0x8F1BBCDC;
498		C = ((C << 30) | (C >>> 2));
499
500		E += ((A << 5) | (A >>> 27)) + ((B & C) | (B & D) | (C & D)) + w[50] + 0x8F1BBCDC;
501		B = ((B << 30) | (B >>> 2));
502
503		D += ((E << 5) | (E >>> 27)) + ((A & B) | (A & C) | (B & C)) + w[51] + 0x8F1BBCDC;
504		A = ((A << 30) | (A >>> 2));
505
506		C += ((D << 5) | (D >>> 27)) + ((E & A) | (E & B) | (A & B)) + w[52] + 0x8F1BBCDC;
507		E = ((E << 30) | (E >>> 2));
508
509		B += ((C << 5) | (C >>> 27)) + ((D & E) | (D & A) | (E & A)) + w[53] + 0x8F1BBCDC;
510		D = ((D << 30) | (D >>> 2));
511
512		A += ((B << 5) | (B >>> 27)) + ((C & D) | (C & E) | (D & E)) + w[54] + 0x8F1BBCDC;
513		C = ((C << 30) | (C >>> 2));
514
515		E = E + ((A << 5) | (A >>> 27)) + ((B & C) | (B & D) | (C & D)) + w[55] + 0x8F1BBCDC;
516		B = ((B << 30) | (B >>> 2));
517
518		D += ((E << 5) | (E >>> 27)) + ((A & B) | (A & C) | (B & C)) + w[56] + 0x8F1BBCDC;
519		A = ((A << 30) | (A >>> 2));
520
521		C += ((D << 5) | (D >>> 27)) + ((E & A) | (E & B) | (A & B)) + w[57] + 0x8F1BBCDC;
522		E = ((E << 30) | (E >>> 2));
523
524		B += ((C << 5) | (C >>> 27)) + ((D & E) | (D & A) | (E & A)) + w[58] + 0x8F1BBCDC;
525		D = ((D << 30) | (D >>> 2));
526
527		A += ((B << 5) | (B >>> 27)) + ((C & D) | (C & E) | (D & E)) + w[59] + 0x8F1BBCDC;
528		C = ((C << 30) | (C >>> 2));
529
530		E += ((A << 5) | (A >>> 27)) + (B ^ C ^ D) + w[60] + 0xCA62C1D6;
531		B = ((B << 30) | (B >>> 2));
532
533		D += ((E << 5) | (E >>> 27)) + (A ^ B ^ C) + w[61] + 0xCA62C1D6;
534		A = ((A << 30) | (A >>> 2));
535
536		C += ((D << 5) | (D >>> 27)) + (E ^ A ^ B) + w[62] + 0xCA62C1D6;
537		E = ((E << 30) | (E >>> 2));
538
539		B += ((C << 5) | (C >>> 27)) + (D ^ E ^ A) + w[63] + 0xCA62C1D6;
540		D = ((D << 30) | (D >>> 2));
541
542		A += ((B << 5) | (B >>> 27)) + (C ^ D ^ E) + w[64] + 0xCA62C1D6;
543		C = ((C << 30) | (C >>> 2));
544
545		E += ((A << 5) | (A >>> 27)) + (B ^ C ^ D) + w[65] + 0xCA62C1D6;
546		B = ((B << 30) | (B >>> 2));
547
548		D += ((E << 5) | (E >>> 27)) + (A ^ B ^ C) + w[66] + 0xCA62C1D6;
549		A = ((A << 30) | (A >>> 2));
550
551		C += ((D << 5) | (D >>> 27)) + (E ^ A ^ B) + w[67] + 0xCA62C1D6;
552		E = ((E << 30) | (E >>> 2));
553
554		B += ((C << 5) | (C >>> 27)) + (D ^ E ^ A) + w[68] + 0xCA62C1D6;
555		D = ((D << 30) | (D >>> 2));
556
557		A += ((B << 5) | (B >>> 27)) + (C ^ D ^ E) + w[69] + 0xCA62C1D6;
558		C = ((C << 30) | (C >>> 2));
559
560		E += ((A << 5) | (A >>> 27)) + (B ^ C ^ D) + w[70] + 0xCA62C1D6;
561		B = ((B << 30) | (B >>> 2));
562
563		D += ((E << 5) | (E >>> 27)) + (A ^ B ^ C) + w[71] + 0xCA62C1D6;
564		A = ((A << 30) | (A >>> 2));
565
566		C += ((D << 5) | (D >>> 27)) + (E ^ A ^ B) + w[72] + 0xCA62C1D6;
567		E = ((E << 30) | (E >>> 2));
568
569		B += ((C << 5) | (C >>> 27)) + (D ^ E ^ A) + w[73] + 0xCA62C1D6;
570		D = ((D << 30) | (D >>> 2));
571
572		A += ((B << 5) | (B >>> 27)) + (C ^ D ^ E) + w[74] + 0xCA62C1D6;
573		C = ((C << 30) | (C >>> 2));
574
575		E += ((A << 5) | (A >>> 27)) + (B ^ C ^ D) + w[75] + 0xCA62C1D6;
576		B = ((B << 30) | (B >>> 2));
577
578		D += ((E << 5) | (E >>> 27)) + (A ^ B ^ C) + w[76] + 0xCA62C1D6;
579		A = ((A << 30) | (A >>> 2));
580
581		C += ((D << 5) | (D >>> 27)) + (E ^ A ^ B) + w[77] + 0xCA62C1D6;
582		E = ((E << 30) | (E >>> 2));
583
584		B += ((C << 5) | (C >>> 27)) + (D ^ E ^ A) + w[78] + 0xCA62C1D6;
585		D = ((D << 30) | (D >>> 2));
586
587		A += ((B << 5) | (B >>> 27)) + (C ^ D ^ E) + w[79] + 0xCA62C1D6;
588		C = ((C << 30) | (C >>> 2));
589
590		H0 += A;
591		H1 += B;
592		H2 += C;
593		H3 += D;
594		H4 += E;
595
596		// debug(80, H0, H1, H2, H3, H4);
597	}
598
599	private static String toHexString(byte[] b)
600	{
601		final String hexChar = "0123456789ABCDEF";
602
603		StringBuilder sb = new StringBuilder();
604		for (int i = 0; i < b.length; i++)
605		{
606			sb.append(hexChar.charAt((b[i] >> 4) & 0x0f));
607			sb.append(hexChar.charAt(b[i] & 0x0f));
608		}
609		return sb.toString();
610	}
611
612	public static void main(String[] args)
613	{
614		SHA1 sha = new SHA1();
615
616		byte[] dig1 = new byte[20];
617		byte[] dig2 = new byte[20];
618		byte[] dig3 = new byte[20];
619
620		/*
621		 * We do not specify a charset name for getBytes(), since we assume that
622		 * the JVM's default encoder maps the _used_ ASCII characters exactly as
623		 * getBytes("US-ASCII") would do. (Ah, yes, too lazy to catch the
624		 * exception that can be thrown by getBytes("US-ASCII")). Note: This has
625		 * no effect on the SHA-1 implementation, this is just for the following
626		 * test code.
627		 */
628
629		sha.update("abc".getBytes());
630		sha.digest(dig1);
631
632		sha.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".getBytes());
633		sha.digest(dig2);
634
635		for (int i = 0; i < 1000000; i++)
636			sha.update((byte) 'a');
637		sha.digest(dig3);
638
639		String dig1_res = toHexString(dig1);
640		String dig2_res = toHexString(dig2);
641		String dig3_res = toHexString(dig3);
642
643		String dig1_ref = "A9993E364706816ABA3E25717850C26C9CD0D89D";
644		String dig2_ref = "84983E441C3BD26EBAAE4AA1F95129E5E54670F1";
645		String dig3_ref = "34AA973CD4C4DAA4F61EEB2BDBAD27316534016F";
646
647		if (dig1_res.equals(dig1_ref))
648			System.out.println("SHA-1 Test 1 OK.");
649		else
650			System.out.println("SHA-1 Test 1 FAILED.");
651
652		if (dig2_res.equals(dig2_ref))
653			System.out.println("SHA-1 Test 2 OK.");
654		else
655			System.out.println("SHA-1 Test 2 FAILED.");
656
657		if (dig3_res.equals(dig3_ref))
658			System.out.println("SHA-1 Test 3 OK.");
659		else
660			System.out.println("SHA-1 Test 3 FAILED.");
661
662		if (dig3_res.equals(dig3_ref))
663			System.out.println("SHA-1 Test 3 OK.");
664		else
665			System.out.println("SHA-1 Test 3 FAILED.");
666	}
667}
668