📄 ripemd160.java
字号:
// $Id: RIPEMD160.java,v 1.1.1.1 2002/08/27 12:32:12 grosbois Exp $//// $Log: RIPEMD160.java,v $// Revision 1.1.1.1 2002/08/27 12:32:12 grosbois// Add cryptix 3.2//// Revision 1.4 1997/12/07 07:25:29 hopwood// + Committed changes below.//// Revision 1.3.1 1997/12/07 hopwood// + Removed '-' from RIPEMD-160 in comments and debug output, since RIPEMD160 is// the standard name.//// Revision 1.3 1997/12/05 19:09:02 raif// + removed '-' from string in Debug.getLevel() invocation.// + cosmetics.//// 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.1.0.3 1997/08/14 David Hopwood// + Added CONTEXT_LENGTH constant.// + Implemented engineGetDigestLength method.// + Required native code version is 2.3.//// Revision 0.1.0.2 1997/07/21 David Hopwood// + 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.//// Revision 0.1.0.1 1997/07/15 David Hopwood// + 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.//// Revision 0.1.0.0 1997/07/14 R. Naffah// + Original version//// $Endlog$/* * 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;/** * Implements the RIPEMD160 message digest algorithm in Java as per the * reference below. * <p> * <b>References:</b> * <ol> * <li> Hans Dobbertin, Antoon Bosselaers and Bart Preneel, * "RIPEMD160: A Strengthened Version of RIPEMD," 18 April 1996. * A joint publication by the German Information Security Agency * (POB 20 03 63, D-53133 Bonn, Germany) * and the Katholieke Universiteit Leuven, ESAT-COSIC * (K. Mercierlaan 94, B-3001 Heverlee, Belgium). * </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.1.1.1 $</b> * @author Raif S. Naffah * @author David Hopwood * @since Cryptix 2.2.2 */public class RIPEMD160extends MessageDigestimplements 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 int debuglevel = DEBUG ? Debug.getLevel("RIPEMD160") : 0; private static final PrintWriter err = DEBUG ? Debug.getOutput() : null; private static void debug(String s) { err.println("RIPEMD160: " + s); }// Native library linking methods and vars.//........................................................................... private static NativeLink linkStatus = new NativeLink("RIPEMD", 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 of RIPEMD-128/160 hash//........................................................................... /** The functions that get the library version. */ private native static int getLibMajorVersion(); private native static int getLibMinorVersion(); /** * Transforms context based on 512 bits from input block starting from * the offset'th byte. * * @param context 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. * @return an error string if one occurs, or null otherwise. */ private native static String native_hash (int[] context, byte[] block, int offset);// RIPEMD160 specific object variables//........................................................................... private static final int BLOCK_LENGTH = 64; private static final int CONTEXT_LENGTH = 5; /** 160-bit h0, h1, h2, h3, h4 (interim result) */ private int[] context = new int[CONTEXT_LENGTH]; /** number of bytes processed so far. */ private long count; /** 512 bits input buffer = 16 x 32-bit words = 8 x 64 bytes */ private byte[] buffer = new byte[BLOCK_LENGTH]; /** 512 bits work buffer = 16 x 32-bit words */ private int[] X = new int[16]; /** * Constants for the transform method. They're defined as static because * they're common to all RIPEMD160 instantiated objects; and final since * they're non-modifiable. */ private static final int[] // selection of message word R = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13}, Rp = { 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11}, // amount for rotate left (rol) S = { 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6}, Sp = { 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11};// Constructors//........................................................................... public RIPEMD160 () { super("RIPEMD160"); engineReset(); link(); } /** This constructor is here to implement cloneability of this class. */ private RIPEMD160 (RIPEMD160 md) { this(); context = (int[])md.context.clone(); buffer = (byte[])md.buffer.clone(); count = md.count; }// Cloneable method implementation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -