001: /*
002: * SHA1.java - An implementation of the SHA-1 Algorithm
003: *
004: * Modified for Jython by Finn Bock. The original was split
005: * into two files.
006: *
007: * Original author and copyright:
008: *
009: * Copyright (c) 1997 Systemics Ltd
010: * on behalf of the Cryptix Development Team. All rights reserved.
011: * @author David Hopwood
012: *
013: * Cryptix General License
014: * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 The Cryptix Foundation
015: * Limited.
016: * All rights reserved.
017: *
018: * Redistribution and use in source and binary forms, with or
019: * without modification, are permitted provided that the
020: * following conditions are met:
021: *
022: * - Redistributions of source code must retain the copyright notice, this
023: * list of conditions and the following disclaimer.
024: * - Redistributions in binary form must reproduce the above
025: * copyright notice, this list of conditions and the following
026: * disclaimer in the documentation and/or other materials
027: * provided with the distribution.
028: *
029: * THIS SOFTWARE IS PROVIDED BY THE CRYPTIX FOUNDATION LIMITED
030: * AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
031: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
032: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
033: * DISCLAIMED. IN NO EVENT SHALL THE CRYPTIX FOUNDATION LIMITED
034: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
035: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
036: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
037: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
038: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
039: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
040: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: */
043:
044: package org.python.modules;
045:
046: import java.io.UnsupportedEncodingException;
047: import org.python.core.*;
048:
049: /**
050: * This class implements the SHA-1 message digest algorithm.
051: * <p>
052: * <b>References:</b>
053: * <ol>
054: * <li> Bruce Schneier,
055: * "Section 18.7 Secure Hash Algorithm (SHA),"
056: * <cite>Applied Cryptography, 2nd edition</cite>,
057: * John Wiley & Sons, 1996
058: * <p>
059: * <li> NIST FIPS PUB 180-1,
060: * "Secure Hash Standard",
061: * U.S. Department of Commerce, May 1993.<br>
062: * <a href="http://www.itl.nist.gov/div897/pubs/fip180-1.htm">
063: * http://www.itl.nist.gov/div897/pubs/fip180-1.htm</a>
064: * </ol>
065: * <p>
066: * <b>Copyright</b> © 1995-1997
067: * <a href="http://www.systemics.com/">Systemics Ltd</a> on behalf of the
068: * <a href="http://www.systemics.com/docs/cryptix/">Cryptix Development
069: * Team</a>.
070: * <br>All rights reserved.
071: * <p>
072: * <b>Revision: 1.7</b>
073: * @author Systemics Ltd
074: * @author David Hopwood
075: * @since Cryptix 2.2.2
076: */
077: public final class SHA1 {
078: /**
079: * The buffer used to store the last incomplete block.
080: */
081: private byte[] buffer;
082:
083: /**
084: * The number of bytes currently stored in <code>buffer</code>.
085: */
086: private int buffered;
087:
088: /**
089: * The number of bytes that have been input to the digest.
090: */
091: private long count;
092:
093: public int digest_size = 20;
094:
095: /**
096: * <b>SPI</b>: Updates the message digest with a byte of new data.
097: *
098: * @param b the byte to be added.
099: */
100: protected void engineUpdate(byte b) {
101: byte[] data = { b };
102: engineUpdate(data, 0, 1);
103: }
104:
105: /**
106: * <b>SPI</b>: Updates the message digest with new data.
107: *
108: * @param data the data to be added.
109: * @param offset the start of the data in the array.
110: * @param length the number of bytes of data to add.
111: */
112: protected void engineUpdate(byte[] data, int offset, int length) {
113: count += length;
114:
115: int datalen = DATA_LENGTH;
116: int remainder;
117:
118: while (length >= (remainder = datalen - buffered)) {
119: System.arraycopy(data, offset, buffer, buffered, remainder);
120: engineTransform(buffer);
121: length -= remainder;
122: offset += remainder;
123: buffered = 0;
124: }
125:
126: if (length > 0) {
127: System.arraycopy(data, offset, buffer, buffered, length);
128: buffered += length;
129: }
130: }
131:
132: /**
133: * <b>SPI</b>: Calculates the final digest. BlockMessageDigest
134: * subclasses should not usually override this method.
135: *
136: * @return the digest as a byte array.
137: */
138: protected byte[] engineDigest() {
139: return engineDigest(buffer, buffered);
140: }
141:
142: // SHA-1 constants and variables
143: //...........................................................................
144:
145: /**
146: * Length of the final hash (in bytes).
147: */
148: private static final int HASH_LENGTH = 20;
149:
150: /**
151: * Length of a block (i.e. the number of bytes hashed in every transform).
152: */
153: private static final int DATA_LENGTH = 64;
154:
155: private int[] data;
156: private int[] digest;
157: private byte[] tmp;
158: private int[] w;
159:
160: /**
161: * Constructs a SHA-1 message digest.
162: */
163: public SHA1() {
164: buffer = new byte[DATA_LENGTH];
165: java_init();
166: engineReset();
167: }
168:
169: private void java_init() {
170: digest = new int[HASH_LENGTH / 4];
171: data = new int[DATA_LENGTH / 4];
172: tmp = new byte[DATA_LENGTH];
173: w = new int[80];
174: }
175:
176: /**
177: * This constructor is here to implement cloneability of this class.
178: */
179: private SHA1(SHA1 md) {
180: this ();
181: data = (int[]) md.data.clone();
182: digest = (int[]) md.digest.clone();
183: tmp = (byte[]) md.tmp.clone();
184: w = (int[]) md.w.clone();
185: buffer = (byte[]) md.buffer.clone();
186: buffered = md.buffered;
187: count = md.count;
188: }
189:
190: /**
191: * Initializes (resets) the message digest.
192: */
193: protected void engineReset() {
194: buffered = 0;
195: count = 0;
196: java_reset();
197: }
198:
199: private void java_reset() {
200: digest[0] = 0x67452301;
201: digest[1] = 0xefcdab89;
202: digest[2] = 0x98badcfe;
203: digest[3] = 0x10325476;
204: digest[4] = 0xc3d2e1f0;
205: }
206:
207: /**
208: * Adds data to the message digest.
209: *
210: * @param data The data to be added.
211: * @param offset The start of the data in the array.
212: * @param length The amount of data to add.
213: */
214: protected void engineTransform(byte[] in) {
215: java_transform(in);
216: }
217:
218: private void java_transform(byte[] in) {
219: byte2int(in, 0, data, 0, DATA_LENGTH / 4);
220: transform(data);
221: }
222:
223: /**
224: * Returns the digest of the data added and resets the digest.
225: * @return the digest of all the data added to the message digest
226: * as a byte array.
227: */
228: protected byte[] engineDigest(byte[] in, int length) {
229: byte b[] = java_digest(in, length);
230: return b;
231: }
232:
233: private byte[] java_digest(byte[] in, int pos) {
234: int[] digest_save = (int[]) digest.clone();
235: if (pos != 0)
236: System.arraycopy(in, 0, tmp, 0, pos);
237:
238: tmp[pos++] = (byte) 0x80;
239:
240: if (pos > DATA_LENGTH - 8) {
241: while (pos < DATA_LENGTH)
242: tmp[pos++] = 0;
243:
244: byte2int(tmp, 0, data, 0, DATA_LENGTH / 4);
245: transform(data);
246: pos = 0;
247: }
248:
249: while (pos < DATA_LENGTH - 8)
250: tmp[pos++] = 0;
251:
252: byte2int(tmp, 0, data, 0, (DATA_LENGTH / 4) - 2);
253:
254: // Big endian
255: // WARNING: int>>>32 != 0 !!!
256: // bitcount() used to return a long, now it's an int.
257: long bc = count * 8;
258: data[14] = (int) (bc >>> 32);
259: data[15] = (int) bc;
260:
261: transform(data);
262:
263: byte buf[] = new byte[HASH_LENGTH];
264:
265: // Big endian
266: int off = 0;
267: for (int i = 0; i < HASH_LENGTH / 4; ++i) {
268: int d = digest[i];
269: buf[off++] = (byte) (d >>> 24);
270: buf[off++] = (byte) (d >>> 16);
271: buf[off++] = (byte) (d >>> 8);
272: buf[off++] = (byte) d;
273: }
274: digest = digest_save;
275: return buf;
276: }
277:
278: // SHA-1 transform routines
279: //...........................................................................
280:
281: private static int f1(int a, int b, int c) {
282: return (c ^ (a & (b ^ c))) + 0x5A827999;
283: }
284:
285: private static int f2(int a, int b, int c) {
286: return (a ^ b ^ c) + 0x6ED9EBA1;
287: }
288:
289: private static int f3(int a, int b, int c) {
290: return ((a & b) | (c & (a | b))) + 0x8F1BBCDC;
291: }
292:
293: private static int f4(int a, int b, int c) {
294: return (a ^ b ^ c) + 0xCA62C1D6;
295: }
296:
297: private void transform(int[] X) {
298: int A = digest[0];
299: int B = digest[1];
300: int C = digest[2];
301: int D = digest[3];
302: int E = digest[4];
303:
304: int W[] = w;
305: for (int i = 0; i < 16; i++) {
306: W[i] = X[i];
307: }
308: for (int i = 16; i < 80; i++) {
309: int j = W[i - 16] ^ W[i - 14] ^ W[i - 8] ^ W[i - 3];
310: W[i] = j;
311: W[i] = (j << 1) | (j >>> -1);
312: }
313:
314: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[0];
315: B = ((B << 30) | (B >>> -30));
316: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[1];
317: A = ((A << 30) | (A >>> -30));
318: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[2];
319: E = ((E << 30) | (E >>> -30));
320: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[3];
321: D = ((D << 30) | (D >>> -30));
322: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[4];
323: C = ((C << 30) | (C >>> -30));
324: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[5];
325: B = ((B << 30) | (B >>> -30));
326: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[6];
327: A = ((A << 30) | (A >>> -30));
328: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[7];
329: E = ((E << 30) | (E >>> -30));
330: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[8];
331: D = ((D << 30) | (D >>> -30));
332: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[9];
333: C = ((C << 30) | (C >>> -30));
334: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[10];
335: B = ((B << 30) | (B >>> -30));
336: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[11];
337: A = ((A << 30) | (A >>> -30));
338: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[12];
339: E = ((E << 30) | (E >>> -30));
340: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[13];
341: D = ((D << 30) | (D >>> -30));
342: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[14];
343: C = ((C << 30) | (C >>> -30));
344: E += ((A << 5) | (A >>> -5)) + f1(B, C, D) + W[15];
345: B = ((B << 30) | (B >>> -30));
346: D += ((E << 5) | (E >>> -5)) + f1(A, B, C) + W[16];
347: A = ((A << 30) | (A >>> -30));
348: C += ((D << 5) | (D >>> -5)) + f1(E, A, B) + W[17];
349: E = ((E << 30) | (E >>> -30));
350: B += ((C << 5) | (C >>> -5)) + f1(D, E, A) + W[18];
351: D = ((D << 30) | (D >>> -30));
352: A += ((B << 5) | (B >>> -5)) + f1(C, D, E) + W[19];
353: C = ((C << 30) | (C >>> -30));
354: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[20];
355: B = ((B << 30) | (B >>> -30));
356: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[21];
357: A = ((A << 30) | (A >>> -30));
358: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[22];
359: E = ((E << 30) | (E >>> -30));
360: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[23];
361: D = ((D << 30) | (D >>> -30));
362: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[24];
363: C = ((C << 30) | (C >>> -30));
364: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[25];
365: B = ((B << 30) | (B >>> -30));
366: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[26];
367: A = ((A << 30) | (A >>> -30));
368: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[27];
369: E = ((E << 30) | (E >>> -30));
370: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[28];
371: D = ((D << 30) | (D >>> -30));
372: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[29];
373: C = ((C << 30) | (C >>> -30));
374: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[30];
375: B = ((B << 30) | (B >>> -30));
376: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[31];
377: A = ((A << 30) | (A >>> -30));
378: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[32];
379: E = ((E << 30) | (E >>> -30));
380: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[33];
381: D = ((D << 30) | (D >>> -30));
382: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[34];
383: C = ((C << 30) | (C >>> -30));
384: E += ((A << 5) | (A >>> -5)) + f2(B, C, D) + W[35];
385: B = ((B << 30) | (B >>> -30));
386: D += ((E << 5) | (E >>> -5)) + f2(A, B, C) + W[36];
387: A = ((A << 30) | (A >>> -30));
388: C += ((D << 5) | (D >>> -5)) + f2(E, A, B) + W[37];
389: E = ((E << 30) | (E >>> -30));
390: B += ((C << 5) | (C >>> -5)) + f2(D, E, A) + W[38];
391: D = ((D << 30) | (D >>> -30));
392: A += ((B << 5) | (B >>> -5)) + f2(C, D, E) + W[39];
393: C = ((C << 30) | (C >>> -30));
394: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[40];
395: B = ((B << 30) | (B >>> -30));
396: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[41];
397: A = ((A << 30) | (A >>> -30));
398: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[42];
399: E = ((E << 30) | (E >>> -30));
400: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[43];
401: D = ((D << 30) | (D >>> -30));
402: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[44];
403: C = ((C << 30) | (C >>> -30));
404: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[45];
405: B = ((B << 30) | (B >>> -30));
406: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[46];
407: A = ((A << 30) | (A >>> -30));
408: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[47];
409: E = ((E << 30) | (E >>> -30));
410: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[48];
411: D = ((D << 30) | (D >>> -30));
412: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[49];
413: C = ((C << 30) | (C >>> -30));
414: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[50];
415: B = ((B << 30) | (B >>> -30));
416: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[51];
417: A = ((A << 30) | (A >>> -30));
418: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[52];
419: E = ((E << 30) | (E >>> -30));
420: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[53];
421: D = ((D << 30) | (D >>> -30));
422: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[54];
423: C = ((C << 30) | (C >>> -30));
424: E += ((A << 5) | (A >>> -5)) + f3(B, C, D) + W[55];
425: B = ((B << 30) | (B >>> -30));
426: D += ((E << 5) | (E >>> -5)) + f3(A, B, C) + W[56];
427: A = ((A << 30) | (A >>> -30));
428: C += ((D << 5) | (D >>> -5)) + f3(E, A, B) + W[57];
429: E = ((E << 30) | (E >>> -30));
430: B += ((C << 5) | (C >>> -5)) + f3(D, E, A) + W[58];
431: D = ((D << 30) | (D >>> -30));
432: A += ((B << 5) | (B >>> -5)) + f3(C, D, E) + W[59];
433: C = ((C << 30) | (C >>> -30));
434: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[60];
435: B = ((B << 30) | (B >>> -30));
436: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[61];
437: A = ((A << 30) | (A >>> -30));
438: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[62];
439: E = ((E << 30) | (E >>> -30));
440: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[63];
441: D = ((D << 30) | (D >>> -30));
442: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[64];
443: C = ((C << 30) | (C >>> -30));
444: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[65];
445: B = ((B << 30) | (B >>> -30));
446: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[66];
447: A = ((A << 30) | (A >>> -30));
448: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[67];
449: E = ((E << 30) | (E >>> -30));
450: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[68];
451: D = ((D << 30) | (D >>> -30));
452: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[69];
453: C = ((C << 30) | (C >>> -30));
454: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[70];
455: B = ((B << 30) | (B >>> -30));
456: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[71];
457: A = ((A << 30) | (A >>> -30));
458: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[72];
459: E = ((E << 30) | (E >>> -30));
460: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[73];
461: D = ((D << 30) | (D >>> -30));
462: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[74];
463: C = ((C << 30) | (C >>> -30));
464: E += ((A << 5) | (A >>> -5)) + f4(B, C, D) + W[75];
465: B = ((B << 30) | (B >>> -30));
466: D += ((E << 5) | (E >>> -5)) + f4(A, B, C) + W[76];
467: A = ((A << 30) | (A >>> -30));
468: C += ((D << 5) | (D >>> -5)) + f4(E, A, B) + W[77];
469: E = ((E << 30) | (E >>> -30));
470: B += ((C << 5) | (C >>> -5)) + f4(D, E, A) + W[78];
471: D = ((D << 30) | (D >>> -30));
472: A += ((B << 5) | (B >>> -5)) + f4(C, D, E) + W[79];
473: C = ((C << 30) | (C >>> -30));
474:
475: digest[0] += A;
476: digest[1] += B;
477: digest[2] += C;
478: digest[3] += D;
479: digest[4] += E;
480: }
481:
482: // why was this public?
483: // Note: parameter order changed to be consistent with System.arraycopy.
484: private static void byte2int(byte[] src, int srcOffset, int[] dst,
485: int dstOffset, int length) {
486: while (length-- > 0) {
487: // Big endian
488: dst[dstOffset++] = (src[srcOffset++] << 24)
489: | ((src[srcOffset++] & 0xFF) << 16)
490: | ((src[srcOffset++] & 0xFF) << 8)
491: | (src[srcOffset++] & 0xFF);
492: }
493: }
494:
495: public static PyString __doc__update = new PyString(
496: "Update this hashing object's state with the provided string.");
497:
498: /**
499: * Add an array of bytes to the digest.
500: */
501: public synchronized void update(byte input[]) {
502: engineUpdate(input, 0, input.length);
503: }
504:
505: public static PyString __doc__copy = new PyString(
506: "Return a copy of the hashing object.");
507:
508: /**
509: * Add an array of bytes to the digest.
510: */
511: public SHA1 copy() {
512: return new SHA1(this );
513: }
514:
515: public static PyString __doc__hexdigest = new PyString(
516: "Return the digest value as a string of hexadecimal digits.");
517:
518: /**
519: * Print out the digest in a form that can be easily compared
520: * to the test vectors.
521: */
522: public String hexdigest() {
523: byte[] digestBits = engineDigest();
524:
525: StringBuffer sb = new StringBuffer();
526: for (int i = 0; i < 20; i++) {
527: char c1, c2;
528:
529: c1 = (char) ((digestBits[i] >>> 4) & 0xf);
530: c2 = (char) (digestBits[i] & 0xf);
531: c1 = (char) ((c1 > 9) ? 'a' + (c1 - 10) : '0' + c1);
532: c2 = (char) ((c2 > 9) ? 'a' + (c2 - 10) : '0' + c2);
533: sb.append(c1);
534: sb.append(c2);
535: }
536: return sb.toString();
537: }
538:
539: public static PyString __doc__digest = new PyString(
540: "Return the digest value as a string of binary data.");
541:
542: public String digest() {
543: byte[] digestBits = engineDigest();
544: try {
545: return new String(digestBits, "ISO-8859-1");
546: } catch (UnsupportedEncodingException exc) {
547: throw Py.ValueError("encoding not supported");
548: }
549: }
550:
551: // XXX should become PyObject and use Py.idstr?
552: public String toString() {
553: return "<SHA object at" + System.identityHashCode(this ) + ">";
554: }
555: }
|