📄 ripemd160.java
字号:
//........................................................................... /** Return a copy of this MD object. */ public Object clone() { return new RIPEMD160(this); }// JCE methods//........................................................................... /** * Resets this object disregarding any temporary data present at the * time of the invocation of this call. */ protected void engineReset () { // magic RIPEMD160 initialisation constants context[0] = 0x67452301; context[1] = 0xEFCDAB89; context[2] = 0x98BADCFE; context[3] = 0x10325476; context[4] = 0xC3D2E1F0; count = 0L; for (int i = 0; i < BLOCK_LENGTH; i++) buffer[i] = 0; } /** Continue a RIPEMD160 message digest using the input byte. */ protected void engineUpdate (byte input) { // compute number of bytes still unhashed; ie. 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); } /** * RIPEMD160 block update operation. * <p> * Continue a RIPEMD160 message digest operation, by filling the buffer, * transform(ing) data in 512-bit message block(s), updating the variables * context and count, and leaving (buffering) the remaining bytes in buffer * for the next update or finish. * * @param input input block * @param offset start of meaningful bytes in input * @param len count of bytes in input block to consider */ public 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; 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); } /** * Complete 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 output (in bytes) to 56 mod 64 int bufferNdx = (int)(count % BLOCK_LENGTH); int padLen = (bufferNdx < 56) ? (56 - bufferNdx) : (120 - bufferNdx); // padding is alwas binary 1 followed by binary 0s byte[] tail = new byte[padLen + 8]; tail[0] = (byte)0x80; // save number of bits, casting the long to an array of 8 bytes for (int i = 0; i < 8; i++) tail[padLen + i] = (byte)((count * 8) >>> (8 * i)); // append length before final transform engineUpdate(tail, 0, tail.length); byte[] result = new byte[20]; // cast this RIPEMD160's context (array of 5 ints) into an array of 20 bytes. for (int i = 0; i < 5; i++) for (int j = 0; j < 4; j++) result[i * 4 + j] = (byte)((context[i] >>> (8 * j)) & 0xFF); engineReset(); return result; } /** <b>SPI</b>: Returns the digest length in bytes. */ protected int engineGetDigestLength() { return 20; }// Own methods//........................................................................... /** * RIPEMD160 basic transformation. * <p> * Transforms context based on 512 bits from input block starting from * the offset'th byte. */ 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)); return; } int A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, T, s, i; // encode 64 bytes from input block into an array of 16 unsigned // integers. for (i = 0; i < 16; i++) X[i] = (block[offset++] & 0xFF) | (block[offset++] & 0xFF) << 8 | (block[offset++] & 0xFF) << 16 | (block[offset++] & 0xFF) << 24; A = Ap = context[0]; B = Bp = context[1]; C = Cp = context[2]; D = Dp = context[3]; E = Ep = context[4]; // rounds 0...15 for (i = 0; i < 16; i++) { s = S[i]; T = A + (B ^ C ^ D) + X[i]; A = E; E = D; D = C << 10 | C >>> 22; C = B; B = (T << s | T >>> (32 - s)) + A; s = Sp[i]; T = Ap + (Bp ^ (Cp | ~Dp)) + X[Rp[i]] + 0x50A28BE6; Ap = Ep; Ep = Dp; Dp = Cp << 10 | Cp >>> 22; Cp = Bp; Bp = (T << s | T >>> (32 - s)) + Ap; } // rounds 16...31 for (i = 16; i < 32; i++) { s = S[i]; T = A + ((B & C) | (~B & D)) + X[R[i]] + 0x5A827999; A = E; E = D; D = C << 10 | C >>> 22; C = B; B = (T << s | T >>> (32 - s)) + A; s = Sp[i]; T = Ap + ((Bp & Dp) | (Cp & ~Dp)) + X[Rp[i]] + 0x5C4DD124; Ap = Ep; Ep = Dp; Dp = Cp << 10 | Cp >>> 22; Cp = Bp; Bp = (T << s | T >>> (32 - s)) + Ap; } // rounds 32...47 for (i = 32; i < 48; i++) { s = S[i]; T = A + ((B | ~C) ^ D) + X[R[i]] + 0x6ED9EBA1; A = E; E = D; D = C << 10 | C >>> 22; C = B; B = (T << s | T >>> (32 - s)) + A; s = Sp[i]; T = Ap + ((Bp | ~Cp) ^ Dp) + X[Rp[i]] + 0x6D703EF3; Ap = Ep; Ep = Dp; Dp = Cp << 10 | Cp >>> 22; Cp = Bp; Bp = (T << s | T >>> (32 - s)) + Ap; } // rounds 48...63 for (i = 48; i < 64; i++) { s = S[i]; T = A + ((B & D) | (C & ~D)) + X[R[i]] + 0x8F1BBCDC; A = E; E = D; D = C << 10 | C >>> 22; C = B; B = (T << s | T >>> (32 - s)) + A; s = Sp[i]; T = Ap + ((Bp & Cp) | (~Bp & Dp)) + X[Rp[i]] + 0x7A6D76E9; Ap = Ep; Ep = Dp; Dp = Cp << 10 | Cp >>> 22; Cp = Bp; Bp = (T << s | T >>> (32 - s)) + Ap; } // rounds 64...79 for (i = 64; i < 80; i++) { s = S[i]; T = A + (B ^ (C | ~D)) + X[R[i]] + 0xA953FD4E; A = E; E = D; D = C << 10 | C >>> 22; C = B; B = (T << s | T >>> (32 - s)) + A; s = Sp[i]; T = Ap + (Bp ^ Cp ^ Dp) + X[Rp[i]]; Ap = Ep; Ep = Dp; Dp = Cp << 10 | Cp >>> 22; Cp = Bp; Bp = (T << s | T >>> (32 - s)) + Ap; } T = context[1] + C + Dp; context[1] = context[2] + D + Ep; context[2] = context[3] + E + Ap; context[3] = context[4] + A + Bp; context[4] = context[0] + B + Cp; context[0] = T; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -