⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 haval.java

📁 另一个使用java编写的加密通用算法包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        for (int i = 0; i < BLOCK_LENGTH; i++)            buffer[i] = 0;    }    /** Continues a HAVAL message digest using the input byte. */    protected void engineUpdate (byte input) {        // compute number of bytes still unhashed; i.e. present in buffer        int i = (int)(count % BLOCK_LENGTH);        count++;                                    // update number of bytes        buffer[i] = input;        if (i == BLOCK_LENGTH - 1)              transform(buffer, 0);    }    /**     * Hashes a byte array from a given offset for a specified length.     * to be used in conjunction with engineReset() and finish().     *     * @param  input    byte array from which data is to be hashed.     * @param  offset   start index of bytes to hash in input.     * @param  len      number of bytes to hash.     */    protected void engineUpdate (byte[] input, int offset, int len) {        // make sure we don't exceed input's allocated size/length.        if (offset < 0 || len < 0 || (long)offset + len > input.length)            throw new ArrayIndexOutOfBoundsException();        // compute number of bytes still unhashed; ie. present in buffer        int bufferNdx = (int)(count % BLOCK_LENGTH);        count += len;                               // update number of bytes        int partLen = BLOCK_LENGTH - bufferNdx;        int i = 0;        // hash as many 128-byte blocks as possible        if (len >= partLen) {            System.arraycopy(input, offset, buffer, bufferNdx, partLen);                transform(buffer, 0);            for (i = partLen; i + BLOCK_LENGTH - 1 < len; i+= BLOCK_LENGTH)                transform(input, offset + i);            bufferNdx = 0;        }        // buffer remaining input        if (i < len)            System.arraycopy(input, offset + i, buffer, bufferNdx, len - i);    }    /**     * Completes the hash computation by performing final operations such     * as padding. At the return of this engineDigest, the MD engine is     * reset.     *     * @return the array of bytes for the resulting hash value.     */    protected byte[] engineDigest() {        // pad out to 118 mod 128. Other 10 bytes have special use.        int bufferNdx = (int)(count % BLOCK_LENGTH);        int padLen = bufferNdx < 118 ? 118 - bufferNdx : 246 - bufferNdx;                byte[] tail = new byte[padLen + 10];        tail[0] = 0x01;        // save the version number (LSB 3), the number of passes (3 bits in the        // middle), the fingerprint length (MSB 2 bits and next byte) and the        // number of bits in the unpadded message.        tail[padLen] = (byte)(((bitLength & 0x3) << 6) | ((passes & 0x7) << 3) |                                (VERSION & 0x7));        tail[padLen + 1] = (byte)(bitLength >>> 2);        // convert count (long) into an array of 8 bytes        for (int i = 0; i < 8; i++)            tail[padLen + 2 + i] = (byte)((count * 8) >>> (8 * i));                    engineUpdate(tail, 0, tail.length);        // tailor the context for the designated bitLength output        tailorDigestBits();        // cast enough top HAVAL context (array of 8 ints) values into        // an array of (bitLength / 8) bytes.        byte[] result = new byte[bitLength / 8];        for (int i = 0; i < bitLength / 32; i++)            for (int j = 0; j < 4; j++)                result[i * 4 + j] = (byte)(context[i] >>> (8 * j));        engineReset();        return result;    }    /** <b>SPI</b>: Returns the digest length in bytes. */    protected int engineGetDigestLength() { return bitLength / 8; }    public void setParameter(String param, Object value)    throws NoSuchParameterException, InvalidParameterException,           InvalidParameterTypeException {        engineSetParameter(param, value);    }    public Object getParameter(String param)    throws NoSuchParameterException, InvalidParameterException {        return engineGetParameter(param);    }    protected void engineSetParameter(String param, Object value)    throws NoSuchParameterException, InvalidParameterException,           InvalidParameterTypeException {        if (param.equalsIgnoreCase("passes")) {            if (value instanceof Integer)                setPasses(((Integer) value).intValue());            else                throw new InvalidParameterTypeException("passes.HAVAL");        } else if (param.equalsIgnoreCase("bitLength")) {            if (value instanceof Integer)                setBitLength(((Integer) value).intValue());            else                throw new InvalidParameterTypeException("bitLength.HAVAL");        } else            throw new NoSuchParameterException(param + ".HAVAL");    }    protected Object engineGetParameter(String param)    throws NoSuchParameterException, InvalidParameterException {        if (param.equalsIgnoreCase("passes"))            return new Integer(passes);        else if (param.equalsIgnoreCase("bitLength"))            return new Integer(bitLength);        else            throw new NoSuchParameterException(param + ".HAVAL");    }// Own methods//...........................................................................    /**     * Sets the number of passes for this HAVAL object, resetting all     * internal variables.     */    public void setPasses(int p) {        if (p >= 3 && p <= 5) {            passes = p;            engineReset();        } else            throw new InvalidParameterException();    }    /**     * Sets the output length of this HAVAL object in bits, resetting all     * internal variables.     */    public void setBitLength(int len) {        if ((len % 32) == 0 && len >= 128 && len <= 256) {            bitLength = len;            engineReset();        } else            throw new InvalidParameterException();    }    /**     * Sets the output length of this HAVAL object in bytes, resetting all     * internal variables.     */    public void setDigestLength(int len) {        setBitLength(len * 8);    }    /** Hashes a 128-byte block starting at offset in b. */    private void transform (byte[] block, int offset) {        // if allowed to use native library then now's the time        if (native_ok) {            // should never happen, but this would be a security bug so check it anyway:            if (context.length != CONTEXT_LENGTH || offset < 0 ||                (long)offset + BLOCK_LENGTH > block.length) {                throw new InternalError(getAlgorithm() +                    ": context.length != " + CONTEXT_LENGTH + " || offset < 0 || " +                    "(long)offset + " + BLOCK_LENGTH + " > block.length");            }            linkStatus.check(native_hash(context, block, offset, passes));            return;        }        // encodes 128 bytes from input block into an array of 32 integers.        for (int i = 0; i < 32; i++)            X[i] = (block[offset++] & 0xFF)       |                   (block[offset++] & 0xFF) <<  8 |                   (block[offset++] & 0xFF) << 16 |                   (block[offset++] & 0xFF) << 24;        int t0 = context[0], t1 = context[1], t2 = context[2], t3 = context[3],            t4 = context[4], t5 = context[5], t6 = context[6], t7 = context[7];        // Pass 1        t7 = FF_1(t7, t6, t5, t4, t3, t2, t1, t0, X[ 0]);        t6 = FF_1(t6, t5, t4, t3, t2, t1, t0, t7, X[ 1]);        t5 = FF_1(t5, t4, t3, t2, t1, t0, t7, t6, X[ 2]);        t4 = FF_1(t4, t3, t2, t1, t0, t7, t6, t5, X[ 3]);        t3 = FF_1(t3, t2, t1, t0, t7, t6, t5, t4, X[ 4]);        t2 = FF_1(t2, t1, t0, t7, t6, t5, t4, t3, X[ 5]);        t1 = FF_1(t1, t0, t7, t6, t5, t4, t3, t2, X[ 6]);        t0 = FF_1(t0, t7, t6, t5, t4, t3, t2, t1, X[ 7]);        t7 = FF_1(t7, t6, t5, t4, t3, t2, t1, t0, X[ 8]);        t6 = FF_1(t6, t5, t4, t3, t2, t1, t0, t7, X[ 9]);        t5 = FF_1(t5, t4, t3, t2, t1, t0, t7, t6, X[10]);        t4 = FF_1(t4, t3, t2, t1, t0, t7, t6, t5, X[11]);        t3 = FF_1(t3, t2, t1, t0, t7, t6, t5, t4, X[12]);        t2 = FF_1(t2, t1, t0, t7, t6, t5, t4, t3, X[13]);        t1 = FF_1(t1, t0, t7, t6, t5, t4, t3, t2, X[14]);        t0 = FF_1(t0, t7, t6, t5, t4, t3, t2, t1, X[15]);        t7 = FF_1(t7, t6, t5, t4, t3, t2, t1, t0, X[16]);        t6 = FF_1(t6, t5, t4, t3, t2, t1, t0, t7, X[17]);        t5 = FF_1(t5, t4, t3, t2, t1, t0, t7, t6, X[18]);        t4 = FF_1(t4, t3, t2, t1, t0, t7, t6, t5, X[19]);        t3 = FF_1(t3, t2, t1, t0, t7, t6, t5, t4, X[20]);        t2 = FF_1(t2, t1, t0, t7, t6, t5, t4, t3, X[21]);        t1 = FF_1(t1, t0, t7, t6, t5, t4, t3, t2, X[22]);        t0 = FF_1(t0, t7, t6, t5, t4, t3, t2, t1, X[23]);        t7 = FF_1(t7, t6, t5, t4, t3, t2, t1, t0, X[24]);        t6 = FF_1(t6, t5, t4, t3, t2, t1, t0, t7, X[25]);        t5 = FF_1(t5, t4, t3, t2, t1, t0, t7, t6, X[26]);        t4 = FF_1(t4, t3, t2, t1, t0, t7, t6, t5, X[27]);        t3 = FF_1(t3, t2, t1, t0, t7, t6, t5, t4, X[28]);        t2 = FF_1(t2, t1, t0, t7, t6, t5, t4, t3, X[29]);        t1 = FF_1(t1, t0, t7, t6, t5, t4, t3, t2, X[30]);        t0 = FF_1(t0, t7, t6, t5, t4, t3, t2, t1, X[31]);        // Pass 2        t7 = FF_2(t7, t6, t5, t4, t3, t2, t1, t0, X[ 5], 0x452821E6);        t6 = FF_2(t6, t5, t4, t3, t2, t1, t0, t7, X[14], 0x38D01377);        t5 = FF_2(t5, t4, t3, t2, t1, t0, t7, t6, X[26], 0xBE5466CF);        t4 = FF_2(t4, t3, t2, t1, t0, t7, t6, t5, X[18], 0x34E90C6C);        t3 = FF_2(t3, t2, t1, t0, t7, t6, t5, t4, X[11], 0xC0AC29B7);        t2 = FF_2(t2, t1, t0, t7, t6, t5, t4, t3, X[28], 0xC97C50DD);        t1 = FF_2(t1, t0, t7, t6, t5, t4, t3, t2, X[ 7], 0x3F84D5B5);        t0 = FF_2(t0, t7, t6, t5, t4, t3, t2, t1, X[16], 0xB5470917);        t7 = FF_2(t7, t6, t5, t4, t3, t2, t1, t0, X[ 0], 0x9216D5D9);        t6 = FF_2(t6, t5, t4, t3, t2, t1, t0, t7, X[23], 0x8979FB1B);        t5 = FF_2(t5, t4, t3, t2, t1, t0, t7, t6, X[20], 0xD1310BA6);        t4 = FF_2(t4, t3, t2, t1, t0, t7, t6, t5, X[22], 0x98DFB5AC);        t3 = FF_2(t3, t2, t1, t0, t7, t6, t5, t4, X[ 1], 0x2FFD72DB);        t2 = FF_2(t2, t1, t0, t7, t6, t5, t4, t3, X[10], 0xD01ADFB7);        t1 = FF_2(t1, t0, t7, t6, t5, t4, t3, t2, X[ 4], 0xB8E1AFED);        t0 = FF_2(t0, t7, t6, t5, t4, t3, t2, t1, X[ 8], 0x6A267E96);        t7 = FF_2(t7, t6, t5, t4, t3, t2, t1, t0, X[30], 0xBA7C9045);        t6 = FF_2(t6, t5, t4, t3, t2, t1, t0, t7, X[ 3], 0xF12C7F99);        t5 = FF_2(t5, t4, t3, t2, t1, t0, t7, t6, X[21], 0x24A19947);        t4 = FF_2(t4, t3, t2, t1, t0, t7, t6, t5, X[ 9], 0xB3916CF7);        t3 = FF_2(t3, t2, t1, t0, t7, t6, t5, t4, X[17], 0x0801F2E2);        t2 = FF_2(t2, t1, t0, t7, t6, t5, t4, t3, X[24], 0x858EFC16);        t1 = FF_2(t1, t0, t7, t6, t5, t4, t3, t2, X[29], 0x636920D8);        t0 = FF_2(t0, t7, t6, t5, t4, t3, t2, t1, X[ 6], 0x71574E69);        t7 = FF_2(t7, t6, t5, t4, t3, t2, t1, t0, X[19], 0xA458FEA3);        t6 = FF_2(t6, t5, t4, t3, t2, t1, t0, t7, X[12], 0xF4933D7E);        t5 = FF_2(t5, t4, t3, t2, t1, t0, t7, t6, X[15], 0x0D95748F);        t4 = FF_2(t4, t3, t2, t1, t0, t7, t6, t5, X[13], 0x728EB658);        t3 = FF_2(t3, t2, t1, t0, t7, t6, t5, t4, X[ 2], 0x718BCD58);        t2 = FF_2(t2, t1, t0, t7, t6, t5, t4, t3, X[25], 0x82154AEE);        t1 = FF_2(t1, t0, t7, t6, t5, t4, t3, t2, X[31], 0x7B54A41D);        t0 = FF_2(t0, t7, t6, t5, t4, t3, t2, t1, X[27], 0xC25A59B5);        // Pass 3        t7 = FF_3(t7, t6, t5, t4, t3, t2, t1, t0, X[19], 0x9C30D539);        t6 = FF_3(t6, t5, t4, t3, t2, t1, t0, t7, X[ 9], 0x2AF26013);        t5 = FF_3(t5, t4, t3, t2, t1, t0, t7, t6, X[ 4], 0xC5D1B023);        t4 = FF_3(t4, t3, t2, t1, t0, t7, t6, t5, X[20], 0x286085F0);        t3 = FF_3(t3, t2, t1, t0, t7, t6, t5, t4, X[28], 0xCA417918);        t2 = FF_3(t2, t1, t0, t7, t6, t5, t4, t3, X[17], 0xB8DB38EF);        t1 = FF_3(t1, t0, t7, t6, t5, t4, t3, t2, X[ 8], 0x8E79DCB0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -