/// <summary> Updates hash with the bytebuffer given (using at maximum length bytes /// from that buffer) /// /// </summary> /// <param name="stat">Which state is updated /// </param> /// <param name="buffer">Array of bytes to be hashed /// </param> /// <param name="offset">Offset to buffer array /// </param> /// <param name="length">Use at maximum `length' bytes (absolute maximum is /// buffer.length) /// </param> private void update(MD5State stat, sbyte[] buffer, int offset, int length) { int index, partlen, i, start; finals = null; /* Length can be told to be shorter, but not inter */ if ((length - offset) > buffer.Length) { length = buffer.Length - offset; } /* compute number of bytes mod 64 */ index = (int)(stat.count & 0x3f); stat.count += length; partlen = 64 - index; if (length >= partlen) { // update state (using only Java) to reflect input int[] decode_buf = new int[16]; if (partlen == 64) { partlen = 0; } else { for (i = 0; i < partlen; i++) { stat.buffer[i + index] = buffer[i + offset]; } transform(stat, stat.buffer, 0, decode_buf); } for (i = partlen; (i + 63) < length; i += 64) { transform(stat, buffer, i + offset, decode_buf); } index = 0; } else { i = 0; } /* buffer remaining input */ if (i < length) { start = i; for (; i < length; i++) { stat.buffer[index + i - start] = buffer[i + offset]; } } }
/// <summary>Create this State as a copy of another state </summary> public MD5State(MD5State from) : this() { int i; for (i = 0; i < buffer.Length; i++) { this.buffer[i] = from.buffer[i]; } for (i = 0; i < state.Length; i++) { this.state[i] = from.state[i]; } this.count = from.count; }
/// <summary> Returns array of bytes (16 bytes) representing hash as of the current /// state of this object. Note: getting a hash does not invalidate the hash /// object, it only creates a copy of the real state which is finalized. /// /// </summary> /// <returns> Array of 16 bytes, the hash of all updated bytes /// </returns> //UPGRADE_NOTE: Synchronized keyword was removed from method 'doFinal'. Lock expression was added. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1027'" public sbyte[] doFinal() { lock (this) { sbyte[] bits; int index, padlen; MD5State fin; if (finals == null) { fin = new MD5State(state); int[] count_ints = new int[] { (int)(fin.count << 3), (int)(fin.count >> 29) }; bits = encode(count_ints, 8); index = (int)(fin.count & 0x3f); padlen = (index < 56)?(56 - index):(120 - index); update(fin, padding, 0, padlen); update(fin, bits, 0, 8); /* Update() sets finals to null */ finals = fin; } return(encode(finals.state, 16)); } }
public MD5(sbyte[] data) { state = new MD5State(); finals = null; update(data); }
private void transform(MD5State state, sbyte[] buffer, int shift, int[] decode_buf) { int a = state.state[0], b = state.state[1], c = state.state[2], d = state.state[3]; int[] x = decode_buf; decode(buffer, shift, decode_buf); /* Round 1 */ a += ((b & c) | (~b & d)) + x[0] + 0xd76aa478; /* 1 */ a = ((a << 7) | (SupportClass.URShift(a, 25))) + b; d += ((a & b) | (~a & c)) + x[1] + 0xe8c7b756; /* 2 */ d = ((d << 12) | (SupportClass.URShift(d, 20))) + a; c += ((d & a) | (~d & b)) + x[2] + 0x242070db; /* 3 */ c = ((c << 17) | (SupportClass.URShift(c, 15))) + d; b += ((c & d) | (~c & a)) + x[3] + 0xc1bdceee; /* 4 */ b = ((b << 22) | (SupportClass.URShift(b, 10))) + c; a += ((b & c) | (~b & d)) + x[4] + 0xf57c0faf; /* 5 */ a = ((a << 7) | (SupportClass.URShift(a, 25))) + b; d += ((a & b) | (~a & c)) + x[5] + 0x4787c62a; /* 6 */ d = ((d << 12) | (SupportClass.URShift(d, 20))) + a; c += ((d & a) | (~d & b)) + x[6] + 0xa8304613; /* 7 */ c = ((c << 17) | (SupportClass.URShift(c, 15))) + d; b += ((c & d) | (~c & a)) + x[7] + 0xfd469501; /* 8 */ b = ((b << 22) | (SupportClass.URShift(b, 10))) + c; a += ((b & c) | (~b & d)) + x[8] + 0x698098d8; /* 9 */ a = ((a << 7) | (SupportClass.URShift(a, 25))) + b; d += ((a & b) | (~a & c)) + x[9] + 0x8b44f7af; /* 10 */ d = ((d << 12) | (SupportClass.URShift(d, 20))) + a; c += ((d & a) | (~d & b)) + x[10] + 0xffff5bb1; /* 11 */ c = ((c << 17) | (SupportClass.URShift(c, 15))) + d; b += ((c & d) | (~c & a)) + x[11] + 0x895cd7be; /* 12 */ b = ((b << 22) | (SupportClass.URShift(b, 10))) + c; a += ((b & c) | (~b & d)) + x[12] + 0x6b901122; /* 13 */ a = ((a << 7) | (SupportClass.URShift(a, 25))) + b; d += ((a & b) | (~a & c)) + x[13] + 0xfd987193; /* 14 */ d = ((d << 12) | (SupportClass.URShift(d, 20))) + a; c += ((d & a) | (~d & b)) + x[14] + 0xa679438e; /* 15 */ c = ((c << 17) | (SupportClass.URShift(c, 15))) + d; b += ((c & d) | (~c & a)) + x[15] + 0x49b40821; /* 16 */ b = ((b << 22) | (SupportClass.URShift(b, 10))) + c; /* Round 2 */ a += ((b & d) | (c & ~d)) + x[1] + 0xf61e2562; /* 17 */ a = ((a << 5) | (SupportClass.URShift(a, 27))) + b; d += ((a & c) | (b & ~c)) + x[6] + 0xc040b340; /* 18 */ d = ((d << 9) | (SupportClass.URShift(d, 23))) + a; c += ((d & b) | (a & ~b)) + x[11] + 0x265e5a51; /* 19 */ c = ((c << 14) | (SupportClass.URShift(c, 18))) + d; b += ((c & a) | (d & ~a)) + x[0] + 0xe9b6c7aa; /* 20 */ b = ((b << 20) | (SupportClass.URShift(b, 12))) + c; a += ((b & d) | (c & ~d)) + x[5] + 0xd62f105d; /* 21 */ a = ((a << 5) | (SupportClass.URShift(a, 27))) + b; d += ((a & c) | (b & ~c)) + x[10] + 0x02441453; /* 22 */ d = ((d << 9) | (SupportClass.URShift(d, 23))) + a; c += ((d & b) | (a & ~b)) + x[15] + 0xd8a1e681; /* 23 */ c = ((c << 14) | (SupportClass.URShift(c, 18))) + d; b += ((c & a) | (d & ~a)) + x[4] + 0xe7d3fbc8; /* 24 */ b = ((b << 20) | (SupportClass.URShift(b, 12))) + c; a += ((b & d) | (c & ~d)) + x[9] + 0x21e1cde6; /* 25 */ a = ((a << 5) | (SupportClass.URShift(a, 27))) + b; d += ((a & c) | (b & ~c)) + x[14] + 0xc33707d6; /* 26 */ d = ((d << 9) | (SupportClass.URShift(d, 23))) + a; c += ((d & b) | (a & ~b)) + x[3] + 0xf4d50d87; /* 27 */ c = ((c << 14) | (SupportClass.URShift(c, 18))) + d; b += ((c & a) | (d & ~a)) + x[8] + 0x455a14ed; /* 28 */ b = ((b << 20) | (SupportClass.URShift(b, 12))) + c; a += ((b & d) | (c & ~d)) + x[13] + 0xa9e3e905; /* 29 */ a = ((a << 5) | (SupportClass.URShift(a, 27))) + b; d += ((a & c) | (b & ~c)) + x[2] + 0xfcefa3f8; /* 30 */ d = ((d << 9) | (SupportClass.URShift(d, 23))) + a; c += ((d & b) | (a & ~b)) + x[7] + 0x676f02d9; /* 31 */ c = ((c << 14) | (SupportClass.URShift(c, 18))) + d; b += ((c & a) | (d & ~a)) + x[12] + 0x8d2a4c8a; /* 32 */ b = ((b << 20) | (SupportClass.URShift(b, 12))) + c; /* Round 3 */ a += (b ^ c ^ d) + x[5] + 0xfffa3942; /* 33 */ a = ((a << 4) | (SupportClass.URShift(a, 28))) + b; d += (a ^ b ^ c) + x[8] + 0x8771f681; /* 34 */ d = ((d << 11) | (SupportClass.URShift(d, 21))) + a; c += (d ^ a ^ b) + x[11] + 0x6d9d6122; /* 35 */ c = ((c << 16) | (SupportClass.URShift(c, 16))) + d; b += (c ^ d ^ a) + x[14] + 0xfde5380c; /* 36 */ b = ((b << 23) | (SupportClass.URShift(b, 9))) + c; a += (b ^ c ^ d) + x[1] + 0xa4beea44; /* 37 */ a = ((a << 4) | (SupportClass.URShift(a, 28))) + b; d += (a ^ b ^ c) + x[4] + 0x4bdecfa9; /* 38 */ d = ((d << 11) | (SupportClass.URShift(d, 21))) + a; c += (d ^ a ^ b) + x[7] + 0xf6bb4b60; /* 39 */ c = ((c << 16) | (SupportClass.URShift(c, 16))) + d; b += (c ^ d ^ a) + x[10] + 0xbebfbc70; /* 40 */ b = ((b << 23) | (SupportClass.URShift(b, 9))) + c; a += (b ^ c ^ d) + x[13] + 0x289b7ec6; /* 41 */ a = ((a << 4) | (SupportClass.URShift(a, 28))) + b; d += (a ^ b ^ c) + x[0] + 0xeaa127fa; /* 42 */ d = ((d << 11) | (SupportClass.URShift(d, 21))) + a; c += (d ^ a ^ b) + x[3] + 0xd4ef3085; /* 43 */ c = ((c << 16) | (SupportClass.URShift(c, 16))) + d; b += (c ^ d ^ a) + x[6] + 0x04881d05; /* 44 */ b = ((b << 23) | (SupportClass.URShift(b, 9))) + c; a += (b ^ c ^ d) + x[9] + 0xd9d4d039; /* 33 */ a = ((a << 4) | (SupportClass.URShift(a, 28))) + b; d += (a ^ b ^ c) + x[12] + 0xe6db99e5; /* 34 */ d = ((d << 11) | (SupportClass.URShift(d, 21))) + a; c += (d ^ a ^ b) + x[15] + 0x1fa27cf8; /* 35 */ c = ((c << 16) | (SupportClass.URShift(c, 16))) + d; b += (c ^ d ^ a) + x[2] + 0xc4ac5665; /* 36 */ b = ((b << 23) | (SupportClass.URShift(b, 9))) + c; /* Round 4 */ a += (c ^ (b | ~d)) + x[0] + 0xf4292244; /* 49 */ a = ((a << 6) | (SupportClass.URShift(a, 26))) + b; d += (b ^ (a | ~c)) + x[7] + 0x432aff97; /* 50 */ d = ((d << 10) | (SupportClass.URShift(d, 22))) + a; c += (a ^ (d | ~b)) + x[14] + 0xab9423a7; /* 51 */ c = ((c << 15) | (SupportClass.URShift(c, 17))) + d; b += (d ^ (c | ~a)) + x[5] + 0xfc93a039; /* 52 */ b = ((b << 21) | (SupportClass.URShift(b, 11))) + c; a += (c ^ (b | ~d)) + x[12] + 0x655b59c3; /* 53 */ a = ((a << 6) | (SupportClass.URShift(a, 26))) + b; d += (b ^ (a | ~c)) + x[3] + 0x8f0ccc92; /* 54 */ d = ((d << 10) | (SupportClass.URShift(d, 22))) + a; c += (a ^ (d | ~b)) + x[10] + 0xffeff47d; /* 55 */ c = ((c << 15) | (SupportClass.URShift(c, 17))) + d; b += (d ^ (c | ~a)) + x[1] + 0x85845dd1; /* 56 */ b = ((b << 21) | (SupportClass.URShift(b, 11))) + c; a += (c ^ (b | ~d)) + x[8] + 0x6fa87e4f; /* 57 */ a = ((a << 6) | (SupportClass.URShift(a, 26))) + b; d += (b ^ (a | ~c)) + x[15] + 0xfe2ce6e0; /* 58 */ d = ((d << 10) | (SupportClass.URShift(d, 22))) + a; c += (a ^ (d | ~b)) + x[6] + 0xa3014314; /* 59 */ c = ((c << 15) | (SupportClass.URShift(c, 17))) + d; b += (d ^ (c | ~a)) + x[13] + 0x4e0811a1; /* 60 */ b = ((b << 21) | (SupportClass.URShift(b, 11))) + c; a += (c ^ (b | ~d)) + x[4] + 0xf7537e82; /* 61 */ a = ((a << 6) | (SupportClass.URShift(a, 26))) + b; d += (b ^ (a | ~c)) + x[11] + 0xbd3af235; /* 62 */ d = ((d << 10) | (SupportClass.URShift(d, 22))) + a; c += (a ^ (d | ~b)) + x[2] + 0x2ad7d2bb; /* 63 */ c = ((c << 15) | (SupportClass.URShift(c, 17))) + d; b += (d ^ (c | ~a)) + x[9] + 0xeb86d391; /* 64 */ b = ((b << 21) | (SupportClass.URShift(b, 11))) + c; state.state[0] += a; state.state[1] += b; state.state[2] += c; state.state[3] += d; //ctsims - Dec 1. 2008 //This call doesn't play well with others, and doesn't have a reason to be here. //It makes serialization super slow, so I took it out. //System.gc(); }