📄 haval.java
字号:
// $Id: HAVAL.java,v 1.4 1997/11/20 19:36:30 hopwood Exp $//// $Log: HAVAL.java,v $// Revision 1.4 1997/11/20 19:36:30 hopwood// + cryptix.util.* name changes.//// Revision 1.3.2 1997/11/18 David Hopwood// + Implement the java.security.Parameterized interface (a workaround for// Sun's version of MessageDigest not supporting set/getParameter).//// Revision 1.3.1 1997/11/15 David Hopwood// + Renamed "Passes" and "BitLength" parameters to "passes" and "bitLength"// respectively.// + Throw InvalidParameterException when the argument to setPasses or// setBitLength is out of range.// + Added engineSet/GetParameter methods (at the moment these only work if the// IJCE 'src10' classes are first on the CLASSPATH).//// Revision 1.3 1997/11/08 14:40:22 raif// *** empty log message ***//// Revision 1.2 1997/11/07 05:53:25 raif// + Changed native API to support variable number of passes.//// Revision 1.1.1.1 1997/11/03 22:36:56 hopwood// + Imported to CVS (tagged as 'start').//// Revision 0.1.0.2 1997/08/27 David Hopwood// + Use new debugging API.// + Misc. fixes.//// Revision 0.1.0.1 1997/08/14 David Hopwood// + Renamed setPass to setPasses.// + Renamed setLength to setBitLength.// + Renamed mdLength variable to bitLength.// + Added setDigestLength (corresponding to getDigestLength) which takes// a length in bytes.// + Implemented engineGetDigestLength method.// + Changed SPI methods from public to protected.// + Changed name of first parameter to native_hash from ctx to context,// to be consistent with native code.// + Check validity of arguments before calling native_hash.// + Changed 'Object native_lock' to 'boolean native_ok'.// + Removed 'synchronized (native_lock) { ... }' around native_hash call.// It isn't necessary to synchronize calls to native methods in this case,// because unlike the cipher classes, the native code for hash functions// is stateless.// + Required native code version is 2.3.// + Cosmetic changes.//// Revision 0.1.0.0 1997/07/14 R. Naffah// + Original version.//// $Endlog$/* * Ported to Java from Dr. Yuliang Zheng's 'C' code, last revised in * April 1997. * * Copyright (c) 1997 Systemics Ltd * on behalf of the Cryptix Development Team. All rights reserved. */package cryptix.provider.md;import cryptix.util.core.Debug;import java.io.PrintWriter;import java.security.MessageDigest;import java.security.Parameterized;import java.security.VariableLengthDigest;import java.security.Security;import java.security.InvalidParameterTypeException;import java.security.InvalidParameterException;import java.security.NoSuchParameterException;/** * A Java class to digest input according to the HAVAL algorithm. * <p> * HAVAL is a variable length MD with a variable number of passes. The values * for these two parameters are read from the provider '.properties' file. Here * is an example of the two property lines that do that: * <p> * <pre> * Alg.passes.HAVAL = 3 * Alg.bitLength.HAVAL = 256 * </pre> * <p> * <b>References:</b> * <ol> * <li> Y. Zheng, J. Pieprzyk and J. Seberry, * "HAVAL --- a one-way hashing algorithm with variable length of output", * <cite>Advances in Cryptology --- AUSCRYPT'92, * Lecture Notes in Computer Science</cite>, * Springer-Verlag, 1993. * <ol> * <p> * <b>Copyright</b> © 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.4 $</b> * @author Raif S. Naffah * @author David Hopwood * @since Cryptix 2.2.2 */public class HAVALextends MessageDigestimplements Parameterized, VariableLengthDigest, Cloneable{// Debugging methods and vars.//........................................................................... private static final boolean DEBUG = Debug.GLOBAL_DEBUG; private static final int debuglevel = DEBUG ? Debug.getLevel("HAVAL") : 0; private static final PrintWriter err = DEBUG ? Debug.getOutput() : null; private static void debug(String s) { err.println("HAVAL: " + s); }// Native library linking methods and vars.//........................................................................... private static NativeLink linkStatus = new NativeLink("HAVAL", 2, 3); public static cryptix.util.core.LinkStatus getLinkStatus() { return linkStatus; } /** * This flag is false if the native code is not being used (e.g. the * library did not load successfully, or the user disabled its use in * the properties file). */ private boolean native_ok; // defaults to false private void link() { synchronized(linkStatus) { try { if (linkStatus.attemptLoad()) linkStatus.checkVersion(getLibMajorVersion(), getLibMinorVersion()); if (linkStatus.useNative()) native_ok = true; } catch (UnsatisfiedLinkError e) { linkStatus.fail(e);if (DEBUG && debuglevel > 2) debug(e.getMessage()); }if (DEBUG && debuglevel > 2) debug("Using native library? " + native_ok); } }// Native implementation//........................................................................... /** The functions that get the library version. */ private native static int getLibMajorVersion(); private native static int getLibMinorVersion(); /** * Transforms context based on 1024 bits from input block starting from * the offset'th byte and given a specific number of passes. * * @param ctx the current context of this instance. * @param block input array containing data to hash. * @param offset index of where we should start reading from input. * @param pass number of passes to apply for this HAVAL instance. * @return an error string if one occurs or null otherwise. */ private native static String native_hash (int[] ctx, byte[] block, int offset, int pass);// HAVAL variables and constants//........................................................................... private static final int VERSION = 1; private static final int DEFAULT_PASSES = 3; private static final int DEFAULT_BITLENGTH = 256; private static final int BLOCK_LENGTH = 128; private static final int CONTEXT_LENGTH = 8; /** * Defines the number of passes the algorithm has to go through * to produce a message digest value. * <p> * Allowed values are integers in the range {3, 4, 5}. The default * is DEFAULT_PASSES (3). */ private int passes = DEFAULT_PASSES; /** * Defines the length of the message digest algorithm output * in bits. * <p> * Allowed values are chosen from the set: {128, 160, 192, 224, 256}. * The default is DEFAULT_BITLENGTH (256). */ private int bitLength = DEFAULT_BITLENGTH; /** Number of bits in the current message being digested. */ private long count; /** Current state of the algorithm transform main function. */ private int[] context = new int[CONTEXT_LENGTH]; /** * Temporary buffer containing unhashed bytes whose total length < 128. */ private byte[] buffer = new byte[BLOCK_LENGTH]; /** * Work buffer for the transformation functions. This object doesn't * get cloned! */ private int[] X = new int[32];// Constructors//........................................................................... public HAVAL () { super("HAVAL"); try { String ps = Security.getAlgorithmProperty("HAVAL", "passes"); int p = Integer.parseInt(ps); if (p >= 3 && p <= 5) passes = p; } catch (Exception e) {} try { String ps = Security.getAlgorithmProperty("HAVAL", "bitLength"); int len = Integer.parseInt(ps); if ((len % 32) == 0 && len >= 128 && len <= 256) bitLength = len; } catch (Exception e) {} engineReset(); link(); } /** This constructor is here to implement cloneability of this class. */ private HAVAL (HAVAL md) { this(); passes = md.passes; bitLength = md.bitLength; count = md.count; context = (int[]) (md.context.clone()); buffer = (byte[]) (md.buffer.clone()); }// Cloneable method implementation//........................................................................... /** Returns a copy of this MD object. */ public Object clone() { return new HAVAL(this); }// JCE methods//........................................................................... /** * Resets this object disregarding any temporary data present at the * time of the invocation of this call. */ protected void engineReset () { // algorithm context's initial values context[0] = 0x243F6A88; context[1] = 0x85A308D3; context[2] = 0x13198A2E; context[3] = 0x03707344; context[4] = 0xA4093822; context[5] = 0x299F31D0; context[6] = 0x082EFA98; context[7] = 0xEC4E6C89; count = 0L;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -