📄 sha1.java
字号:
// $Id: SHA1.java,v 1.1.1.1 2002/08/27 12:32:12 grosbois Exp $//// $Log: SHA1.java,v $// Revision 1.1.1.1 2002/08/27 12:32:12 grosbois// Add cryptix 3.2//// Revision 1.7 1999/07/13 18:13:22 edwin// Fixed cloning.// This is a fix to: http://130.89.235.121/root/cryptix/old/cryptix/FAQ.html#bj_blockmessagedigest_clone//// Revision 1.6 1998/01/05 01:33:59 iang// BUG: missing the engineUpdate() method.//// Revision 1.5 1997/12/19 05:44:13 hopwood// + Committed changes below.//// Revision 1.4.1 1997/12/19 hopwood// + Added URL for FIPS 180-1.//// Revision 1.4 1997/12/16 21:58:25 iang// + MD5, SHA{01} debugged, got working, internal self_tests ok.// + BlockMessageDigest.bitcount() made long, was int, check calling// where it is assumed by digest algorithms to be a long.//// Revision 1.3 1997/12/07 07:25:29 hopwood// + Committed changes below.//// Revision 1.2.1 1997/11/30 hopwood// + Added references in documentation.//// Revision 1.2 1997/11/20 19:36:30 hopwood// + cryptix.util.* name changes.//// Revision 1.1.1.1 1997/11/03 22:36:56 hopwood// + Imported to CVS (tagged as 'start').//// Revision 0.3.0.0 1997/08/15 David Hopwood// + Renamed to SHA1 (from SHA), and moved to cryptix.provider.md// package.// + This class is now a pure JCA MessageDigest.//// Revision 0.2.5.1 1997/03/15 Jill Baker// + Moved this file here from old namespace.//// Revision 0.2.5.0 1997/02/24 Original Author not stated// + Original version.//// $Endlog$/* * Copyright (c) 1995-1997 Systemics Ltd * on behalf of the Cryptix Development Team. All rights reserved. */package cryptix.provider.md;import java.io.PrintWriter;import java.security.MessageDigest;import cryptix.util.core.Debug;import cryptix.util.core.Hex;/** * This class implements the SHA-1 message digest algorithm. * <p> * <b>BUG</b>: The update method is missing. * <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.1.1.1 $</b> * @author Systemics Ltd * @author David Hopwood * @since Cryptix 2.2.2 */public final class SHA1extends BlockMessageDigestimplements Cloneable{// Debugging methods and vars.//........................................................................... private static final boolean DEBUG = Debug.GLOBAL_DEBUG; private static final boolean DEBUG_SLOW = Debug.GLOBAL_DEBUG_SLOW; private static final int debuglevel = DEBUG ? Debug.getLevel("SHA-1") : 0; private static final PrintWriter err = DEBUG ? Debug.getOutput() : null; private static void debug(String s) { err.println("SHA-1: " + s); }// 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; /** * Returns the length of the hash (in bytes). */ protected int engineGetDigestLength() { return HASH_LENGTH; } /** * Returns the length of the data (in bytes) hashed in every transform. */ protected int engineGetDataLength() { return DATA_LENGTH; } /** * Constructs a SHA-1 message digest. */ public SHA1() { super("SHA-1"); java_init(); reset(); } 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(); } /** * Returns a copy of this MD object. */ public Object clone() { return new SHA1(this); } /** * Initializes (resets) the message digest. */ protected void engineReset() { super.engineReset(); 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) { while (pos < DATA_LENGTH) tmp[pos++] = 0; byte2int(tmp, 0, data, 0, DATA_LENGTH/4); transform(data); pos = 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -