📄 sha1.java
字号:
/* * SHA1.java - An implementation of the SHA-1 Algorithm * * Modified for Jython by Finn Bock. The original was split * into two files. * * Original author and copyright: * * Copyright (c) 1997 Systemics Ltd * on behalf of the Cryptix Development Team. All rights reserved. * @author David Hopwood * * Cryptix General License * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 The Cryptix Foundation * Limited. * All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the * following conditions are met: * * - Redistributions of source code must retain the copyright notice, this * list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE CRYPTIX FOUNDATION LIMITED * AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE CRYPTIX FOUNDATION LIMITED * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.python.modules;import java.util.*;import org.python.core.*;/** * This class implements the SHA-1 message digest algorithm. * <p> * <b>References:</b> * <ol> * <li> Bruce Schneier, * "Section 18.7 Secure Hash Algorithm (SHA)," * <cite>Applied Cryptography, 2nd edition</cite>, * John Wiley & Sons, 1996 * <p> * <li> NIST FIPS PUB 180-1, * "Secure Hash Standard", * U.S. Department of Commerce, May 1993.<br> * <a href="http://www.itl.nist.gov/div897/pubs/fip180-1.htm"> * http://www.itl.nist.gov/div897/pubs/fip180-1.htm</a> * </ol> * <p> * <b>Copyright</b> © 1995-1997 * <a href="http://www.systemics.com/">Systemics Ltd</a> on behalf of the * <a href="http://www.systemics.com/docs/cryptix/">Cryptix Development * Team</a>. * <br>All rights reserved. * <p> * <b>Revision: 1.7</b> * @author Systemics Ltd * @author David Hopwood * @since Cryptix 2.2.2 */public final class SHA1 { /** * The buffer used to store the last incomplete block. */ private byte[] buffer; /** * The number of bytes currently stored in <code>buffer</code>. */ private int buffered; /** * The number of bytes that have been input to the digest. */ private long count; /** * <b>SPI</b>: Updates the message digest with a byte of new data. * * @param b the byte to be added. */ protected void engineUpdate(byte b) { byte[] data = { b }; engineUpdate(data, 0, 1); } /** * <b>SPI</b>: Updates the message digest with new data. * * @param data the data to be added. * @param offset the start of the data in the array. * @param length the number of bytes of data to add. */ protected void engineUpdate(byte[] data, int offset, int length) { count += length; int datalen = DATA_LENGTH; int remainder; while (length >= (remainder = datalen - buffered)) { System.arraycopy(data, offset, buffer, buffered, remainder); engineTransform(buffer); length -= remainder; offset += remainder; buffered = 0; } if (length > 0) { System.arraycopy(data, offset, buffer, buffered, length); buffered += length; } } /** * <b>SPI</b>: Calculates the final digest. BlockMessageDigest * subclasses should not usually override this method. * * @return the digest as a byte array. */ protected byte[] engineDigest() { return engineDigest(buffer, buffered); }// SHA-1 constants and variables//........................................................................... /** * Length of the final hash (in bytes). */ private static final int HASH_LENGTH = 20; /** * Length of a block (i.e. the number of bytes hashed in every transform). */ private static final int DATA_LENGTH = 64; private int[] data; private int[] digest; private byte[] tmp; private int[] w; private byte[] digestBits; /** * Constructs a SHA-1 message digest. */ public SHA1() { buffer = new byte[DATA_LENGTH]; java_init(); engineReset(); } private void java_init() { digest = new int[HASH_LENGTH/4]; data = new int[DATA_LENGTH/4]; tmp = new byte[DATA_LENGTH]; w = new int[80]; } /** * This constructor is here to implement cloneability of this class. */ private SHA1 (SHA1 md) { this(); data = (int[])md.data.clone(); digest = (int[])md.digest.clone(); tmp = (byte[])md.tmp.clone(); w = (int[])md.w.clone(); } /** * Initializes (resets) the message digest. */ protected void engineReset() { buffered = 0; count = 0; java_reset(); } private void java_reset() { digest[0] = 0x67452301; digest[1] = 0xefcdab89; digest[2] = 0x98badcfe; digest[3] = 0x10325476; digest[4] = 0xc3d2e1f0; } /** * Adds data to the message digest. * * @param data The data to be added. * @param offset The start of the data in the array. * @param length The amount of data to add. */ protected void engineTransform(byte[] in) { java_transform(in); } private void java_transform(byte[] in) { byte2int(in, 0, data, 0, DATA_LENGTH/4); transform(data); } /** * Returns the digest of the data added and resets the digest. * @return the digest of all the data added to the message digest * as a byte array. */ protected byte[] engineDigest(byte[] in, int length) { byte b[] = java_digest(in, length); engineReset(); return b; } private byte[] java_digest(byte[] in, int pos) { if (pos != 0) System.arraycopy(in, 0, tmp, 0, pos); tmp[pos++] = (byte)0x80; if (pos > DATA_LENGTH - 8)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -