📄 sha0.java
字号:
// $Id: SHA0.java,v 1.1.1.1 2002/08/27 12:32:12 grosbois Exp $//// $Log: SHA0.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/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.4 1997/12/07 07:25:29 hopwood// + Committed changes below.//// Revision 1.3.1 1997/11/30 hopwood// + Added references in documentation.//// Revision 1.3 1997/11/20 19:36:30 hopwood// + cryptix.util.* name changes.//// Revision 1.2 1997/11/10 07:31:32 raif// + Removed reference to core.Cryptix.//// 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/27 David Hopwood// + Split from cryptix.security.SHA0.//// Revision 0.2.5.2 1997/06/29 David Hopwood// + Changes to support JCA.// + Deprecated the static convenience methods.//// 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 cryptix.util.core.Debug;import cryptix.util.core.Hex;import java.io.PrintWriter;import java.security.MessageDigest;/** * This class implements the SHA-0 message digest algorithm * (not to be confused with the revised SHA-1 algorithm). SHA-1 * is preferred for new applications. * <p> * <b>BUG</b>: The update method is missing. * <p> * <b>References:</b> * <ol> * <li> NIST FIPS PUB 180, * "Secure Hash Standard", * U.S. Department of Commerce, May 1993. * </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 SHA0extends 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-0") : 0; private static final PrintWriter err = DEBUG ? Debug.getOutput() : null; private static void debug(String s) { err.println("SHA-0: " + s); }// SHA-0 constants and variables//........................................................................... /** * Length of the final hash (in bytes). */ public static final int HASH_LENGTH = 20; /** * Length of a block (i.e. the number of bytes hashed in every transform). */ public 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; } /** * The public constructor. */ public SHA0() { super("SHA-0"); 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 SHA0 (SHA0 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 SHA0(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; } while (pos < DATA_LENGTH - 8) tmp[pos++] = 0; byte2int(tmp, 0, data, 0, (DATA_LENGTH/4)-2); // Big endian // WARNING: int>>>32 != 0 !!! // bitcount() used to return a long, now it's an int. long bc = bitcount(); data[14] = (int) (bc>>>32); data[15] = (int) bc; transform(data);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -