haval.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 760 行 · 第 1/2 页
JAVA
760 行
t3 = FF3(t3, t2, t1, t0, t7, t6, t5, t4, X23, 0xCE5C3E16); t2 = FF3(t2, t1, t0, t7, t6, t5, t4, t3, X11, 0x9B87931E); t1 = FF3(t1, t0, t7, t6, t5, t4, t3, t2, X5, 0xAFD6BA33); t0 = FF3(t0, t7, t6, t5, t4, t3, t2, t1, X2, 0x6C24CF5C); if (rounds >= 4) { t7 = FF4(t7, t6, t5, t4, t3, t2, t1, t0, X24, 0x7A325381); t6 = FF4(t6, t5, t4, t3, t2, t1, t0, t7, X4, 0x28958677); t5 = FF4(t5, t4, t3, t2, t1, t0, t7, t6, X0, 0x3B8F4898); t4 = FF4(t4, t3, t2, t1, t0, t7, t6, t5, X14, 0x6B4BB9AF); t3 = FF4(t3, t2, t1, t0, t7, t6, t5, t4, X2, 0xC4BFE81B); t2 = FF4(t2, t1, t0, t7, t6, t5, t4, t3, X7, 0x66282193); t1 = FF4(t1, t0, t7, t6, t5, t4, t3, t2, X28, 0x61D809CC); t0 = FF4(t0, t7, t6, t5, t4, t3, t2, t1, X23, 0xFB21A991); t7 = FF4(t7, t6, t5, t4, t3, t2, t1, t0, X26, 0x487CAC60); t6 = FF4(t6, t5, t4, t3, t2, t1, t0, t7, X6, 0x5DEC8032); t5 = FF4(t5, t4, t3, t2, t1, t0, t7, t6, X30, 0xEF845D5D); t4 = FF4(t4, t3, t2, t1, t0, t7, t6, t5, X20, 0xE98575B1); t3 = FF4(t3, t2, t1, t0, t7, t6, t5, t4, X18, 0xDC262302); t2 = FF4(t2, t1, t0, t7, t6, t5, t4, t3, X25, 0xEB651B88); t1 = FF4(t1, t0, t7, t6, t5, t4, t3, t2, X19, 0x23893E81); t0 = FF4(t0, t7, t6, t5, t4, t3, t2, t1, X3, 0xD396ACC5); t7 = FF4(t7, t6, t5, t4, t3, t2, t1, t0, X22, 0x0F6D6FF3); t6 = FF4(t6, t5, t4, t3, t2, t1, t0, t7, X11, 0x83F44239); t5 = FF4(t5, t4, t3, t2, t1, t0, t7, t6, X31, 0x2E0B4482); t4 = FF4(t4, t3, t2, t1, t0, t7, t6, t5, X21, 0xA4842004); t3 = FF4(t3, t2, t1, t0, t7, t6, t5, t4, X8, 0x69C8F04A); t2 = FF4(t2, t1, t0, t7, t6, t5, t4, t3, X27, 0x9E1F9B5E); t1 = FF4(t1, t0, t7, t6, t5, t4, t3, t2, X12, 0x21C66842); t0 = FF4(t0, t7, t6, t5, t4, t3, t2, t1, X9, 0xF6E96C9A); t7 = FF4(t7, t6, t5, t4, t3, t2, t1, t0, X1, 0x670C9C61); t6 = FF4(t6, t5, t4, t3, t2, t1, t0, t7, X29, 0xABD388F0); t5 = FF4(t5, t4, t3, t2, t1, t0, t7, t6, X5, 0x6A51A0D2); t4 = FF4(t4, t3, t2, t1, t0, t7, t6, t5, X15, 0xD8542F68); t3 = FF4(t3, t2, t1, t0, t7, t6, t5, t4, X17, 0x960FA728); t2 = FF4(t2, t1, t0, t7, t6, t5, t4, t3, X10, 0xAB5133A3); t1 = FF4(t1, t0, t7, t6, t5, t4, t3, t2, X16, 0x6EEF0B6C); t0 = FF4(t0, t7, t6, t5, t4, t3, t2, t1, X13, 0x137A3BE4); if (rounds == 5) { t7 = FF5(t7, t6, t5, t4, t3, t2, t1, t0, X27, 0xBA3BF050); t6 = FF5(t6, t5, t4, t3, t2, t1, t0, t7, X3, 0x7EFB2A98); t5 = FF5(t5, t4, t3, t2, t1, t0, t7, t6, X21, 0xA1F1651D); t4 = FF5(t4, t3, t2, t1, t0, t7, t6, t5, X26, 0x39AF0176); t3 = FF5(t3, t2, t1, t0, t7, t6, t5, t4, X17, 0x66CA593E); t2 = FF5(t2, t1, t0, t7, t6, t5, t4, t3, X11, 0x82430E88); t1 = FF5(t1, t0, t7, t6, t5, t4, t3, t2, X20, 0x8CEE8619); t0 = FF5(t0, t7, t6, t5, t4, t3, t2, t1, X29, 0x456F9FB4); t7 = FF5(t7, t6, t5, t4, t3, t2, t1, t0, X19, 0x7D84A5C3); t6 = FF5(t6, t5, t4, t3, t2, t1, t0, t7, X0, 0x3B8B5EBE); t5 = FF5(t5, t4, t3, t2, t1, t0, t7, t6, X12, 0xE06F75D8); t4 = FF5(t4, t3, t2, t1, t0, t7, t6, t5, X7, 0x85C12073); t3 = FF5(t3, t2, t1, t0, t7, t6, t5, t4, X13, 0x401A449F); t2 = FF5(t2, t1, t0, t7, t6, t5, t4, t3, X8, 0x56C16AA6); t1 = FF5(t1, t0, t7, t6, t5, t4, t3, t2, X31, 0x4ED3AA62); t0 = FF5(t0, t7, t6, t5, t4, t3, t2, t1, X10, 0x363F7706); t7 = FF5(t7, t6, t5, t4, t3, t2, t1, t0, X5, 0x1BFEDF72); t6 = FF5(t6, t5, t4, t3, t2, t1, t0, t7, X9, 0x429B023D); t5 = FF5(t5, t4, t3, t2, t1, t0, t7, t6, X14, 0x37D0D724); t4 = FF5(t4, t3, t2, t1, t0, t7, t6, t5, X30, 0xD00A1248); t3 = FF5(t3, t2, t1, t0, t7, t6, t5, t4, X18, 0xDB0FEAD3); t2 = FF5(t2, t1, t0, t7, t6, t5, t4, t3, X6, 0x49F1C09B); t1 = FF5(t1, t0, t7, t6, t5, t4, t3, t2, X28, 0x075372C9); t0 = FF5(t0, t7, t6, t5, t4, t3, t2, t1, X24, 0x80991B7B); t7 = FF5(t7, t6, t5, t4, t3, t2, t1, t0, X2, 0x25D479D8); t6 = FF5(t6, t5, t4, t3, t2, t1, t0, t7, X23, 0xF6E8DEF7); t5 = FF5(t5, t4, t3, t2, t1, t0, t7, t6, X16, 0xE3FE501A); t4 = FF5(t4, t3, t2, t1, t0, t7, t6, t5, X22, 0xB6794C3B); t3 = FF5(t3, t2, t1, t0, t7, t6, t5, t4, X4, 0x976CE0BD); t2 = FF5(t2, t1, t0, t7, t6, t5, t4, t3, X1, 0x04C006BA); t1 = FF5(t1, t0, t7, t6, t5, t4, t3, t2, X25, 0xC1A94FB6); t0 = FF5(t0, t7, t6, t5, t4, t3, t2, t1, X15, 0x409F60C4); } } h7 += t7; h6 += t6; h5 += t5; h4 += t4; h3 += t3; h2 += t2; h1 += t1; h0 += t0; } protected byte[] padBuffer() { // pad out to 118 mod 128. other 10 bytes have special use. int n = (int) (count % BLOCK_SIZE); int padding = (n < 118) ? (118 - n) : (246 - n); byte[] result = new byte[padding + 10]; result[0] = (byte) 0x01; // save the version number (LSB 3), the number of rounds (3 bits in the // middle), the fingerprint length (MSB 2 bits and next byte) and the // number of bits in the unpadded message. int bl = hashSize * 8; result[padding++] = (byte) (((bl & 0x03) << 6) | ((rounds & 0x07) << 3) | (HAVAL_VERSION & 0x07)); result[padding++] = (byte) (bl >>> 2); // save number of bits, casting the long to an array of 8 bytes long bits = count << 3; result[padding++] = (byte) bits; result[padding++] = (byte) (bits >>> 8); result[padding++] = (byte) (bits >>> 16); result[padding++] = (byte) (bits >>> 24); result[padding++] = (byte) (bits >>> 32); result[padding++] = (byte) (bits >>> 40); result[padding++] = (byte) (bits >>> 48); result[padding] = (byte) (bits >>> 56); return result; } protected byte[] getResult() { tailorDigestBits(); // tailor context for the designated output size // cast enough top context values into an array of hashSize bytes byte[] result = new byte[hashSize]; if (hashSize >= HAVAL_256_BIT) { result[31] = (byte) (h7 >>> 24); result[30] = (byte) (h7 >>> 16); result[29] = (byte) (h7 >>> 8); result[28] = (byte) h7; } if (hashSize >= HAVAL_224_BIT) { result[27] = (byte) (h6 >>> 24); result[26] = (byte) (h6 >>> 16); result[25] = (byte) (h6 >>> 8); result[24] = (byte) h6; } if (hashSize >= HAVAL_192_BIT) { result[23] = (byte) (h5 >>> 24); result[22] = (byte) (h5 >>> 16); result[21] = (byte) (h5 >>> 8); result[20] = (byte) h5; } if (hashSize >= HAVAL_160_BIT) { result[19] = (byte) (h4 >>> 24); result[18] = (byte) (h4 >>> 16); result[17] = (byte) (h4 >>> 8); result[16] = (byte) h4; } result[15] = (byte) (h3 >>> 24); result[14] = (byte) (h3 >>> 16); result[13] = (byte) (h3 >>> 8); result[12] = (byte) h3; result[11] = (byte) (h2 >>> 24); result[10] = (byte) (h2 >>> 16); result[9] = (byte) (h2 >>> 8); result[8] = (byte) h2; result[7] = (byte) (h1 >>> 24); result[6] = (byte) (h1 >>> 16); result[5] = (byte) (h1 >>> 8); result[4] = (byte) h1; result[3] = (byte) (h0 >>> 24); result[2] = (byte) (h0 >>> 16); result[1] = (byte) (h0 >>> 8); result[0] = (byte) h0; return result; } protected void resetContext() { h0 = 0x243F6A88; h1 = 0x85A308D3; h2 = 0x13198A2E; h3 = 0x03707344; h4 = 0xA4093822; h5 = 0x299F31D0; h6 = 0x082EFA98; h7 = 0xEC4E6C89; } public boolean selfTest() { if (valid == null) { valid = new Boolean(DIGEST0.equals(Util.toString(new Haval().digest()))); } return valid.booleanValue(); } // helper methods ---------------------------------------------------------- /** Tailors the last output. */ private void tailorDigestBits() { int t; switch (hashSize) { case HAVAL_128_BIT: t = (h7 & 0x000000FF) | (h6 & 0xFF000000) | (h5 & 0x00FF0000) | (h4 & 0x0000FF00); h0 += t >>> 8 | t << 24; t = (h7 & 0x0000FF00) | (h6 & 0x000000FF) | (h5 & 0xFF000000) | (h4 & 0x00FF0000); h1 += t >>> 16 | t << 16; t = (h7 & 0x00FF0000) | (h6 & 0x0000FF00) | (h5 & 0x000000FF) | (h4 & 0xFF000000); h2 += t >>> 24 | t << 8; t = (h7 & 0xFF000000) | (h6 & 0x00FF0000) | (h5 & 0x0000FF00) | (h4 & 0x000000FF); h3 += t; break; case HAVAL_160_BIT: t = (h7 & 0x3F) | (h6 & (0x7F << 25)) | (h5 & (0x3F << 19)); h0 += t >>> 19 | t << 13; t = (h7 & (0x3F << 6)) | (h6 & 0x3F) | (h5 & (0x7F << 25)); h1 += t >>> 25 | t << 7; t = (h7 & (0x7F << 12)) | (h6 & (0x3F << 6)) | (h5 & 0x3F); h2 += t; t = (h7 & (0x3F << 19)) | (h6 & (0x7F << 12)) | (h5 & (0x3F << 6)); h3 += (t >>> 6); t = (h7 & (0x7F << 25)) | (h6 & (0x3F << 19)) | (h5 & (0x7F << 12)); h4 += (t >>> 12); break; case HAVAL_192_BIT: t = (h7 & 0x1F) | (h6 & (0x3F << 26)); h0 += t >>> 26 | t << 6; t = (h7 & (0x1F << 5)) | (h6 & 0x1F); h1 += t; t = (h7 & (0x3F << 10)) | (h6 & (0x1F << 5)); h2 += (t >>> 5); t = (h7 & (0x1F << 16)) | (h6 & (0x3F << 10)); h3 += (t >>> 10); t = (h7 & (0x1F << 21)) | (h6 & (0x1F << 16)); h4 += (t >>> 16); t = (h7 & (0x3F << 26)) | (h6 & (0x1F << 21)); h5 += (t >>> 21); break; case HAVAL_224_BIT: h0 += ((h7 >>> 27) & 0x1F); h1 += ((h7 >>> 22) & 0x1F); h2 += ((h7 >>> 18) & 0x0F); h3 += ((h7 >>> 13) & 0x1F); h4 += ((h7 >>> 9) & 0x0F); h5 += ((h7 >>> 4) & 0x1F); h6 += (h7 & 0x0F); } } /** * Permutations phi_{i,j}, i=3,4,5, j=1,...,i. * * rounds = 3: 6 5 4 3 2 1 0 * | | | | | | | (replaced by) * phi_{3,1}: 1 0 3 5 6 2 4 * phi_{3,2}: 4 2 1 0 5 3 6 * phi_{3,3}: 6 1 2 3 4 5 0 * * rounds = 4: 6 5 4 3 2 1 0 * | | | | | | | (replaced by) * phi_{4,1}: 2 6 1 4 5 3 0 * phi_{4,2}: 3 5 2 0 1 6 4 * phi_{4,3}: 1 4 3 6 0 2 5 * phi_{4,4}: 6 4 0 5 2 1 3 * * rounds = 5: 6 5 4 3 2 1 0 * | | | | | | | (replaced by) * phi_{5,1}: 3 4 1 0 5 2 6 * phi_{5,2}: 6 2 1 0 3 4 5 * phi_{5,3}: 2 6 0 4 3 1 5 * phi_{5,4}: 1 5 3 2 0 4 6 * phi_{5,5}: 2 5 0 6 4 3 1 */ private int FF1(int x7, int x6, int x5, int x4, int x3, int x2, int x1, int x0, int w) { int t; switch (rounds) { case 3: t = f1(x1, x0, x3, x5, x6, x2, x4); break; case 4: t = f1(x2, x6, x1, x4, x5, x3, x0); break; default: t = f1(x3, x4, x1, x0, x5, x2, x6); } return (t >>> 7 | t << 25) + (x7 >>> 11 | x7 << 21) + w; } private int FF2(int x7, int x6, int x5, int x4, int x3, int x2, int x1, int x0, int w, int c) { int t; switch (rounds) { case 3: t = f2(x4, x2, x1, x0, x5, x3, x6); break; case 4: t = f2(x3, x5, x2, x0, x1, x6, x4); break; default: t = f2(x6, x2, x1, x0, x3, x4, x5); } return (t >>> 7 | t << 25) + (x7 >>> 11 | x7 << 21) + w + c; } private int FF3(int x7, int x6, int x5, int x4, int x3, int x2, int x1, int x0, int w, int c) { int t; switch (rounds) { case 3: t = f3(x6, x1, x2, x3, x4, x5, x0); break; case 4: t = f3(x1, x4, x3, x6, x0, x2, x5); break; default: t = f3(x2, x6, x0, x4, x3, x1, x5); } return (t >>> 7 | t << 25) + (x7 >>> 11 | x7 << 21) + w + c; } private int FF4(int x7, int x6, int x5, int x4, int x3, int x2, int x1, int x0, int w, int c) { int t; switch (rounds) { case 4: t = f4(x6, x4, x0, x5, x2, x1, x3); break; default: t = f4(x1, x5, x3, x2, x0, x4, x6); } return (t >>> 7 | t << 25) + (x7 >>> 11 | x7 << 21) + w + c; } private int FF5(int x7, int x6, int x5, int x4, int x3, int x2, int x1, int x0, int w, int c) { int t = f5(x2, x5, x0, x6, x4, x3, x1); return (t >>> 7 | t << 25) + (x7 >>> 11 | x7 << 21) + w + c; } private int f1(int x6, int x5, int x4, int x3, int x2, int x1, int x0) { return x1 & (x0 ^ x4) ^ x2 & x5 ^ x3 & x6 ^ x0; } private int f2(int x6, int x5, int x4, int x3, int x2, int x1, int x0) { return x2 & (x1 & ~x3 ^ x4 & x5 ^ x6 ^ x0) ^ x4 & (x1 ^ x5) ^ x3 & x5 ^ x0; } private int f3(int x6, int x5, int x4, int x3, int x2, int x1, int x0) { return x3 & (x1 & x2 ^ x6 ^ x0) ^ x1 & x4 ^ x2 & x5 ^ x0; } private int f4(int x6, int x5, int x4, int x3, int x2, int x1, int x0) { return x4 & (x5 & ~x2 ^ x3 & ~x6 ^ x1 ^ x6 ^ x0) ^ x3 & (x1 & x2 ^ x5 ^ x6) ^ x2 & x6 ^ x0; } private int f5(int x6, int x5, int x4, int x3, int x2, int x1, int x0) { return x0 & (x1 & x2 & x3 ^ ~x5) ^ x1 & x4 ^ x2 & x5 ^ x3 & x6; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?