haval.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 760 行 · 第 1/2 页
JAVA
760 行
/* Haval.java -- Copyright (C) 2003, 2006 Free Software Foundation, Inc.This file is a part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (atyour option) any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301USALinking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.java.security.hash;import gnu.java.security.Registry;import gnu.java.security.util.Util;/** * <p>The <i>HAVAL</i> message-digest algorithm is a variable output length, * with variable number of rounds. By default, this implementation allows * <i>HAVAL</i> to be used as a drop-in replacement for <i>MD5</i>.</p> * * <p>References:</p> * * <ol> * <li>HAVAL - A One-Way Hashing Algorithm with Variable Length of Output<br> * Advances in Cryptology - AUSCRYPT'92, Lecture Notes in Computer Science,<br> * Springer-Verlag, 1993; <br> * Y. Zheng, J. Pieprzyk and J. Seberry.</li> * </ol> */public class Haval extends BaseHash{ // Constants and variables // ------------------------------------------------------------------------- public static final int HAVAL_VERSION = 1; public static final int HAVAL_128_BIT = 16; public static final int HAVAL_160_BIT = 20; public static final int HAVAL_192_BIT = 24; public static final int HAVAL_224_BIT = 28; public static final int HAVAL_256_BIT = 32; public static final int HAVAL_3_ROUND = 3; public static final int HAVAL_4_ROUND = 4; public static final int HAVAL_5_ROUND = 5; private static final int BLOCK_SIZE = 128; // inner block size in bytes private static final String DIGEST0 = "C68F39913F901F3DDF44C707357A7D70"; /** caches the result of the correctness test, once executed. */ private static Boolean valid; /** * Number of HAVAL rounds. Allowed values are integers in the range <code>3 * .. 5</code>. The default is <code>3</code>. */ private int rounds = HAVAL_3_ROUND; /** 128-bit interim result. */ private int h0, h1, h2, h3, h4, h5, h6, h7; // Constructor(s) // ------------------------------------------------------------------------- /** * <p>Calls the constructor with two argument using {@link #HAVAL_128_BIT} as * the value for the output size (i.e. <code>128</code> bits, and * {@link #HAVAL_3_ROUND} for the value of number of rounds.</p> */ public Haval() { this(HAVAL_128_BIT, HAVAL_3_ROUND); } /** * <p>Calls the constructor with two arguments using the designated output * size, and {@link #HAVAL_3_ROUND} for the value of number of rounds.</p> * * @param size the output size in bytes of this instance. * @throws IllegalArgumentException if the designated output size is invalid. * @see #HAVAL_128_BIT * @see #HAVAL_160_BIT * @see #HAVAL_192_BIT * @see #HAVAL_224_BIT * @see #HAVAL_256_BIT */ public Haval(int size) { this(size, HAVAL_3_ROUND); } /** * <p>Constructs a <code>Haval</code> instance with the designated output * size (in bytes). Valid output <code>size</code> values are <code>16</code>, * <code>20</code>, <code>24</code>, <code>28</code> and <code>32</code>. * Valid values for <code>rounds</code> are in the range <code>3..5</code> * inclusive.</p> * * @param size the output size in bytes of this instance. * @param rounds the number of rounds to apply when transforming data. * @throws IllegalArgumentException if the designated output size is invalid, * or if the number of rounds is invalid. * @see #HAVAL_128_BIT * @see #HAVAL_160_BIT * @see #HAVAL_192_BIT * @see #HAVAL_224_BIT * @see #HAVAL_256_BIT * @see #HAVAL_3_ROUND * @see #HAVAL_4_ROUND * @see #HAVAL_5_ROUND */ public Haval(int size, int rounds) { super(Registry.HAVAL_HASH, size, BLOCK_SIZE); if (size != HAVAL_128_BIT && size != HAVAL_160_BIT && size != HAVAL_192_BIT && size != HAVAL_224_BIT && size != HAVAL_256_BIT) { throw new IllegalArgumentException("Invalid HAVAL output size"); } if (rounds != HAVAL_3_ROUND && rounds != HAVAL_4_ROUND && rounds != HAVAL_5_ROUND) { throw new IllegalArgumentException("Invalid HAVAL number of rounds"); } this.rounds = rounds; } /** * <p>Private constructor for cloning purposes.</p> * * @param md the instance to clone. */ private Haval(Haval md) { this(md.hashSize, md.rounds); this.h0 = md.h0; this.h1 = md.h1; this.h2 = md.h2; this.h3 = md.h3; this.h4 = md.h4; this.h5 = md.h5; this.h6 = md.h6; this.h7 = md.h7; this.count = md.count; this.buffer = (byte[]) md.buffer.clone(); } // Constructor(s) // ------------------------------------------------------------------------- // Class methods // ------------------------------------------------------------------------- // Instance methods // ------------------------------------------------------------------------- // java.lang.Cloneable interface implementation ---------------------------- public Object clone() { return new Haval(this); } // Implementation of concrete methods in BaseHash -------------------------- protected synchronized void transform(byte[] in, int i) { int X0 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X1 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X2 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X3 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X4 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X5 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X6 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X7 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X8 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X9 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X10 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X11 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X12 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X13 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X14 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X15 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X16 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X17 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X18 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X19 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X20 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X21 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X22 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X23 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X24 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X25 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X26 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X27 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X28 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X29 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X30 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int X31 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16 | (in[i++] & 0xFF) << 24; int t0 = h0, t1 = h1, t2 = h2, t3 = h3, t4 = h4, t5 = h5, t6 = h6, t7 = h7; // Pass 1 t7 = FF1(t7, t6, t5, t4, t3, t2, t1, t0, X0); t6 = FF1(t6, t5, t4, t3, t2, t1, t0, t7, X1); t5 = FF1(t5, t4, t3, t2, t1, t0, t7, t6, X2); t4 = FF1(t4, t3, t2, t1, t0, t7, t6, t5, X3); t3 = FF1(t3, t2, t1, t0, t7, t6, t5, t4, X4); t2 = FF1(t2, t1, t0, t7, t6, t5, t4, t3, X5); t1 = FF1(t1, t0, t7, t6, t5, t4, t3, t2, X6); t0 = FF1(t0, t7, t6, t5, t4, t3, t2, t1, X7); t7 = FF1(t7, t6, t5, t4, t3, t2, t1, t0, X8); t6 = FF1(t6, t5, t4, t3, t2, t1, t0, t7, X9); t5 = FF1(t5, t4, t3, t2, t1, t0, t7, t6, X10); t4 = FF1(t4, t3, t2, t1, t0, t7, t6, t5, X11); t3 = FF1(t3, t2, t1, t0, t7, t6, t5, t4, X12); t2 = FF1(t2, t1, t0, t7, t6, t5, t4, t3, X13); t1 = FF1(t1, t0, t7, t6, t5, t4, t3, t2, X14); t0 = FF1(t0, t7, t6, t5, t4, t3, t2, t1, X15); t7 = FF1(t7, t6, t5, t4, t3, t2, t1, t0, X16); t6 = FF1(t6, t5, t4, t3, t2, t1, t0, t7, X17); t5 = FF1(t5, t4, t3, t2, t1, t0, t7, t6, X18); t4 = FF1(t4, t3, t2, t1, t0, t7, t6, t5, X19); t3 = FF1(t3, t2, t1, t0, t7, t6, t5, t4, X20); t2 = FF1(t2, t1, t0, t7, t6, t5, t4, t3, X21); t1 = FF1(t1, t0, t7, t6, t5, t4, t3, t2, X22); t0 = FF1(t0, t7, t6, t5, t4, t3, t2, t1, X23); t7 = FF1(t7, t6, t5, t4, t3, t2, t1, t0, X24); t6 = FF1(t6, t5, t4, t3, t2, t1, t0, t7, X25); t5 = FF1(t5, t4, t3, t2, t1, t0, t7, t6, X26); t4 = FF1(t4, t3, t2, t1, t0, t7, t6, t5, X27); t3 = FF1(t3, t2, t1, t0, t7, t6, t5, t4, X28); t2 = FF1(t2, t1, t0, t7, t6, t5, t4, t3, X29); t1 = FF1(t1, t0, t7, t6, t5, t4, t3, t2, X30); t0 = FF1(t0, t7, t6, t5, t4, t3, t2, t1, X31); // Pass 2 t7 = FF2(t7, t6, t5, t4, t3, t2, t1, t0, X5, 0x452821E6); t6 = FF2(t6, t5, t4, t3, t2, t1, t0, t7, X14, 0x38D01377); t5 = FF2(t5, t4, t3, t2, t1, t0, t7, t6, X26, 0xBE5466CF); t4 = FF2(t4, t3, t2, t1, t0, t7, t6, t5, X18, 0x34E90C6C); t3 = FF2(t3, t2, t1, t0, t7, t6, t5, t4, X11, 0xC0AC29B7); t2 = FF2(t2, t1, t0, t7, t6, t5, t4, t3, X28, 0xC97C50DD); t1 = FF2(t1, t0, t7, t6, t5, t4, t3, t2, X7, 0x3F84D5B5); t0 = FF2(t0, t7, t6, t5, t4, t3, t2, t1, X16, 0xB5470917); t7 = FF2(t7, t6, t5, t4, t3, t2, t1, t0, X0, 0x9216D5D9); t6 = FF2(t6, t5, t4, t3, t2, t1, t0, t7, X23, 0x8979FB1B); t5 = FF2(t5, t4, t3, t2, t1, t0, t7, t6, X20, 0xD1310BA6); t4 = FF2(t4, t3, t2, t1, t0, t7, t6, t5, X22, 0x98DFB5AC); t3 = FF2(t3, t2, t1, t0, t7, t6, t5, t4, X1, 0x2FFD72DB); t2 = FF2(t2, t1, t0, t7, t6, t5, t4, t3, X10, 0xD01ADFB7); t1 = FF2(t1, t0, t7, t6, t5, t4, t3, t2, X4, 0xB8E1AFED); t0 = FF2(t0, t7, t6, t5, t4, t3, t2, t1, X8, 0x6A267E96); t7 = FF2(t7, t6, t5, t4, t3, t2, t1, t0, X30, 0xBA7C9045); t6 = FF2(t6, t5, t4, t3, t2, t1, t0, t7, X3, 0xF12C7F99); t5 = FF2(t5, t4, t3, t2, t1, t0, t7, t6, X21, 0x24A19947); t4 = FF2(t4, t3, t2, t1, t0, t7, t6, t5, X9, 0xB3916CF7); t3 = FF2(t3, t2, t1, t0, t7, t6, t5, t4, X17, 0x0801F2E2); t2 = FF2(t2, t1, t0, t7, t6, t5, t4, t3, X24, 0x858EFC16); t1 = FF2(t1, t0, t7, t6, t5, t4, t3, t2, X29, 0x636920D8); t0 = FF2(t0, t7, t6, t5, t4, t3, t2, t1, X6, 0x71574E69); t7 = FF2(t7, t6, t5, t4, t3, t2, t1, t0, X19, 0xA458FEA3); t6 = FF2(t6, t5, t4, t3, t2, t1, t0, t7, X12, 0xF4933D7E); t5 = FF2(t5, t4, t3, t2, t1, t0, t7, t6, X15, 0x0D95748F); t4 = FF2(t4, t3, t2, t1, t0, t7, t6, t5, X13, 0x728EB658); t3 = FF2(t3, t2, t1, t0, t7, t6, t5, t4, X2, 0x718BCD58); t2 = FF2(t2, t1, t0, t7, t6, t5, t4, t3, X25, 0x82154AEE); t1 = FF2(t1, t0, t7, t6, t5, t4, t3, t2, X31, 0x7B54A41D); t0 = FF2(t0, t7, t6, t5, t4, t3, t2, t1, X27, 0xC25A59B5); // Pass 3 t7 = FF3(t7, t6, t5, t4, t3, t2, t1, t0, X19, 0x9C30D539); t6 = FF3(t6, t5, t4, t3, t2, t1, t0, t7, X9, 0x2AF26013); t5 = FF3(t5, t4, t3, t2, t1, t0, t7, t6, X4, 0xC5D1B023); t4 = FF3(t4, t3, t2, t1, t0, t7, t6, t5, X20, 0x286085F0); t3 = FF3(t3, t2, t1, t0, t7, t6, t5, t4, X28, 0xCA417918); t2 = FF3(t2, t1, t0, t7, t6, t5, t4, t3, X17, 0xB8DB38EF); t1 = FF3(t1, t0, t7, t6, t5, t4, t3, t2, X8, 0x8E79DCB0); t0 = FF3(t0, t7, t6, t5, t4, t3, t2, t1, X22, 0x603A180E); t7 = FF3(t7, t6, t5, t4, t3, t2, t1, t0, X29, 0x6C9E0E8B); t6 = FF3(t6, t5, t4, t3, t2, t1, t0, t7, X14, 0xB01E8A3E); t5 = FF3(t5, t4, t3, t2, t1, t0, t7, t6, X25, 0xD71577C1); t4 = FF3(t4, t3, t2, t1, t0, t7, t6, t5, X12, 0xBD314B27); t3 = FF3(t3, t2, t1, t0, t7, t6, t5, t4, X24, 0x78AF2FDA); t2 = FF3(t2, t1, t0, t7, t6, t5, t4, t3, X30, 0x55605C60); t1 = FF3(t1, t0, t7, t6, t5, t4, t3, t2, X16, 0xE65525F3); t0 = FF3(t0, t7, t6, t5, t4, t3, t2, t1, X26, 0xAA55AB94); t7 = FF3(t7, t6, t5, t4, t3, t2, t1, t0, X31, 0x57489862); t6 = FF3(t6, t5, t4, t3, t2, t1, t0, t7, X15, 0x63E81440); t5 = FF3(t5, t4, t3, t2, t1, t0, t7, t6, X7, 0x55CA396A); t4 = FF3(t4, t3, t2, t1, t0, t7, t6, t5, X3, 0x2AAB10B6); t3 = FF3(t3, t2, t1, t0, t7, t6, t5, t4, X1, 0xB4CC5C34); t2 = FF3(t2, t1, t0, t7, t6, t5, t4, t3, X0, 0x1141E8CE); t1 = FF3(t1, t0, t7, t6, t5, t4, t3, t2, X18, 0xA15486AF); t0 = FF3(t0, t7, t6, t5, t4, t3, t2, t1, X27, 0x7C72E993); t7 = FF3(t7, t6, t5, t4, t3, t2, t1, t0, X13, 0xB3EE1411); t6 = FF3(t6, t5, t4, t3, t2, t1, t0, t7, X6, 0x636FBC2A); t5 = FF3(t5, t4, t3, t2, t1, t0, t7, t6, X21, 0x2BA9C55D); t4 = FF3(t4, t3, t2, t1, t0, t7, t6, t5, X10, 0x741831F6);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?