whirlpool.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 627 行 · 第 1/2 页
JAVA
627 行
/* Whirlpool.java -- Copyright (C) 2001, 2002, 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>Whirlpool, a new 512-bit hashing function operating on messages less than * 2 ** 256 bits in length. The function structure is designed according to the * Wide Trail strategy and permits a wide variety of implementation trade-offs. * </p> * * <p><b>IMPORTANT</b>: This implementation is not thread-safe.</p> * * <p>References:</p> * * <ol> * <li><a href="http://planeta.terra.com.br/informatica/paulobarreto/WhirlpoolPage.html"> * The WHIRLPOOL Hashing Function</a>.<br> * <a href="mailto:paulo.barreto@terra.com.br">Paulo S.L.M. Barreto</a> and * <a href="mailto:vincent.rijmen@esat.kuleuven.ac.be">Vincent Rijmen</a>.</li> * </ol> */public final class Whirlpool extends BaseHash{ // Debugging methods and variables // ------------------------------------------------------------------------- private static final boolean DEBUG = false; private static final int debuglevel = 3; // Constants and variables // ------------------------------------------------------------------------- private static final int BLOCK_SIZE = 64; // inner block size in bytes /** The digest of the 0-bit long message. */ private static final String DIGEST0 = "470F0409ABAA446E49667D4EBE12A14387CEDBD10DD17B8243CAD550A089DC0F" + "EEA7AA40F6C2AAAB71C6EBD076E43C7CFCA0AD32567897DCB5969861049A0F5A"; private static final int R = 10; // default number of rounds private static final String Sd = // p. 19 [WHIRLPOOL] "\u1823\uc6E8\u87B8\u014F\u36A6\ud2F5\u796F\u9152" + "\u60Bc\u9B8E\uA30c\u7B35\u1dE0\ud7c2\u2E4B\uFE57" + "\u1577\u37E5\u9FF0\u4AdA\u58c9\u290A\uB1A0\u6B85" + "\uBd5d\u10F4\ucB3E\u0567\uE427\u418B\uA77d\u95d8" + "\uFBEE\u7c66\udd17\u479E\ucA2d\uBF07\uAd5A\u8333" + "\u6302\uAA71\uc819\u49d9\uF2E3\u5B88\u9A26\u32B0" + "\uE90F\ud580\uBEcd\u3448\uFF7A\u905F\u2068\u1AAE" + "\uB454\u9322\u64F1\u7312\u4008\uc3Ec\udBA1\u8d3d" + "\u9700\ucF2B\u7682\ud61B\uB5AF\u6A50\u45F3\u30EF" + "\u3F55\uA2EA\u65BA\u2Fc0\udE1c\uFd4d\u9275\u068A" + "\uB2E6\u0E1F\u62d4\uA896\uF9c5\u2559\u8472\u394c" + "\u5E78\u388c\ud1A5\uE261\uB321\u9c1E\u43c7\uFc04" + "\u5199\u6d0d\uFAdF\u7E24\u3BAB\ucE11\u8F4E\uB7EB" + "\u3c81\u94F7\uB913\u2cd3\uE76E\uc403\u5644\u7FA9" + "\u2ABB\uc153\udc0B\u9d6c\u3174\uF646\uAc89\u14E1" + "\u163A\u6909\u70B6\ud0Ed\ucc42\u98A4\u285c\uF886"; private static final long[] T0 = new long[256]; private static final long[] T1 = new long[256]; private static final long[] T2 = new long[256]; private static final long[] T3 = new long[256]; private static final long[] T4 = new long[256]; private static final long[] T5 = new long[256]; private static final long[] T6 = new long[256]; private static final long[] T7 = new long[256]; private static final long[] rc = new long[R]; /** caches the result of the correctness test, once executed. */ private static Boolean valid; /** The 512-bit context as 8 longs. */ private long H0, H1, H2, H3, H4, H5, H6, H7; /** Work area for computing the round key schedule. */ private long k00, k01, k02, k03, k04, k05, k06, k07; private long Kr0, Kr1, Kr2, Kr3, Kr4, Kr5, Kr6, Kr7; /** work area for transforming the 512-bit buffer. */ private long n0, n1, n2, n3, n4, n5, n6, n7; private long nn0, nn1, nn2, nn3, nn4, nn5, nn6, nn7; /** work area for holding block cipher's intermediate values. */ private long w0, w1, w2, w3, w4, w5, w6, w7; // Static code - to intialise lookup tables -------------------------------- static { long time = System.currentTimeMillis(); int ROOT = 0x11d; // para. 2.1 [WHIRLPOOL] int i, r, j; long s, s2, s3, s4, s5, s8, s9, t; char c; final byte[] S = new byte[256]; for (i = 0; i < 256; i++) { c = Sd.charAt(i >>> 1); s = ((i & 1) == 0 ? c >>> 8 : c) & 0xFFL; s2 = s << 1; if (s2 > 0xFFL) { s2 ^= ROOT; } s3 = s2 ^ s; s4 = s2 << 1; if (s4 > 0xFFL) { s4 ^= ROOT; } s5 = s4 ^ s; s8 = s4 << 1; if (s8 > 0xFFL) { s8 ^= ROOT; } s9 = s8 ^ s; S[i] = (byte) s; T0[i] = t = s << 56 | s << 48 | s3 << 40 | s << 32 | s5 << 24 | s8 << 16 | s9 << 8 | s5; T1[i] = t >>> 8 | t << 56; T2[i] = t >>> 16 | t << 48; T3[i] = t >>> 24 | t << 40; T4[i] = t >>> 32 | t << 32; T5[i] = t >>> 40 | t << 24; T6[i] = t >>> 48 | t << 16; T7[i] = t >>> 56 | t << 8; } for (r = 1, i = 0, j = 0; r < R + 1; r++) { rc[i++] = (S[j++] & 0xFFL) << 56 | (S[j++] & 0xFFL) << 48 | (S[j++] & 0xFFL) << 40 | (S[j++] & 0xFFL) << 32 | (S[j++] & 0xFFL) << 24 | (S[j++] & 0xFFL) << 16 | (S[j++] & 0xFFL) << 8 | (S[j++] & 0xFFL); } time = System.currentTimeMillis() - time; if (DEBUG && debuglevel > 8) { System.out.println("=========="); System.out.println(); System.out.println("Static data"); System.out.println(); System.out.println(); System.out.println("T0[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T0[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T1[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T1[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T2[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T2[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T3[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T3[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T4[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T4[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T5[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T5[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T6[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T5[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("T7[]:"); for (i = 0; i < 64; i++) { for (j = 0; j < 4; j++) { System.out.print("0x" + Util.toString(T5[i * 4 + j]) + ", "); } System.out.println(); } System.out.println(); System.out.println("rc[]:"); for (i = 0; i < R; i++) { System.out.println("0x" + Util.toString(rc[i])); } System.out.println(); System.out.println(); System.out.println("Total initialization time: " + time + " ms."); System.out.println(); } } // Constructor(s) // ------------------------------------------------------------------------- /** Trivial 0-arguments constructor. */ public Whirlpool() { super(Registry.WHIRLPOOL_HASH, 20, BLOCK_SIZE); } /** * <p>Private constructor for cloning purposes.</p> * * @param md the instance to clone. */ private Whirlpool(Whirlpool md) { this(); this.H0 = md.H0; this.H1 = md.H1;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?