- Timestamp:
- 01/06/10 19:40:17 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LogicMail/src/org/logicprobe/LogicMail/util/MD5.java
r90 r601 135 135 */ 136 136 int state[]; 137 137 138 138 /** 139 139 * 64-bit character count 140 140 */ 141 141 long count; 142 142 143 143 /** 144 144 * 64-byte buffer (512 bits) for storing to-be-hashed characters 145 145 */ 146 146 byte buffer[]; 147 147 148 148 public MD5State() { 149 149 buffer = new byte[64]; 150 150 count = 0; 151 151 state = new int[4]; 152 152 153 153 state[0] = 0x67452301; 154 154 state[1] = 0xefcdab89; … … 156 156 state[3] = 0x10325476; 157 157 } 158 158 159 159 /** Create this State as a copy of another state */ 160 160 public MD5State(MD5State from) { … … 168 168 } 169 169 }; 170 170 171 171 private MD5State state; 172 172 private MD5State finals; 173 173 174 174 private static final byte padding[] = 175 {(byte)0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,178 0, 0, 0, 0, 0, 0, 0, 0 };179 175 {(byte)0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 178 0, 0, 0, 0, 0, 0, 0, 0 }; 179 180 180 /* 181 181 * len += shift; for (int i = 0; shift < len; i++, shift += 4) { out[i] = … … 185 185 */ 186 186 187 187 188 188 private final void decode( final byte buffer[], final int shift, final int[] out) { 189 189 out[0] = (buffer[shift] & 0xff) | ((buffer[shift + 1] & 0xff) << 8) … … 249 249 | (buffer[shift + 63] << 24); 250 250 } 251 251 252 252 private final void transform(MD5State state, byte buffer[], int shift, int[] decode_buf) { 253 253 int a = state.state[0], b = state.state[1], c = state.state[2], d = state.state[3], x[] = decode_buf; 254 254 255 255 decode(buffer, shift, decode_buf); 256 256 257 257 /* Round 1 */ 258 258 a += ((b & c) | (~b & d)) + x[0] + 0xd76aa478; /* 1 */ … … 264 264 b += ((c & d) | (~c & a)) + x[3] + 0xc1bdceee; /* 4 */ 265 265 b = ((b << 22) | (b >>> 10)) + c; 266 266 267 267 a += ((b & c) | (~b & d)) + x[4] + 0xf57c0faf; /* 5 */ 268 268 a = ((a << 7) | (a >>> 25)) + b; … … 273 273 b += ((c & d) | (~c & a)) + x[7] + 0xfd469501; /* 8 */ 274 274 b = ((b << 22) | (b >>> 10)) + c; 275 275 276 276 a += ((b & c) | (~b & d)) + x[8] + 0x698098d8; /* 9 */ 277 277 a = ((a << 7) | (a >>> 25)) + b; … … 282 282 b += ((c & d) | (~c & a)) + x[11] + 0x895cd7be; /* 12 */ 283 283 b = ((b << 22) | (b >>> 10)) + c; 284 284 285 285 a += ((b & c) | (~b & d)) + x[12] + 0x6b901122; /* 13 */ 286 286 a = ((a << 7) | (a >>> 25)) + b; … … 291 291 b += ((c & d) | (~c & a)) + x[15] + 0x49b40821; /* 16 */ 292 292 b = ((b << 22) | (b >>> 10)) + c; 293 293 294 294 /* Round 2 */ 295 295 a += ((b & d) | (c & ~d)) + x[1] + 0xf61e2562; /* 17 */ … … 301 301 b += ((c & a) | (d & ~a)) + x[0] + 0xe9b6c7aa; /* 20 */ 302 302 b = ((b << 20) | (b >>> 12)) + c; 303 303 304 304 a += ((b & d) | (c & ~d)) + x[5] + 0xd62f105d; /* 21 */ 305 305 a = ((a << 5) | (a >>> 27)) + b; … … 310 310 b += ((c & a) | (d & ~a)) + x[4] + 0xe7d3fbc8; /* 24 */ 311 311 b = ((b << 20) | (b >>> 12)) + c; 312 312 313 313 a += ((b & d) | (c & ~d)) + x[9] + 0x21e1cde6; /* 25 */ 314 314 a = ((a << 5) | (a >>> 27)) + b; … … 319 319 b += ((c & a) | (d & ~a)) + x[8] + 0x455a14ed; /* 28 */ 320 320 b = ((b << 20) | (b >>> 12)) + c; 321 321 322 322 a += ((b & d) | (c & ~d)) + x[13] + 0xa9e3e905; /* 29 */ 323 323 a = ((a << 5) | (a >>> 27)) + b; … … 328 328 b += ((c & a) | (d & ~a)) + x[12] + 0x8d2a4c8a; /* 32 */ 329 329 b = ((b << 20) | (b >>> 12)) + c; 330 330 331 331 /* Round 3 */ 332 332 a += (b ^ c ^ d) + x[5] + 0xfffa3942; /* 33 */ … … 338 338 b += (c ^ d ^ a) + x[14] + 0xfde5380c; /* 36 */ 339 339 b = ((b << 23) | (b >>> 9)) + c; 340 340 341 341 a += (b ^ c ^ d) + x[1] + 0xa4beea44; /* 37 */ 342 342 a = ((a << 4) | (a >>> 28)) + b; … … 347 347 b += (c ^ d ^ a) + x[10] + 0xbebfbc70; /* 40 */ 348 348 b = ((b << 23) | (b >>> 9)) + c; 349 349 350 350 a += (b ^ c ^ d) + x[13] + 0x289b7ec6; /* 41 */ 351 351 a = ((a << 4) | (a >>> 28)) + b; … … 356 356 b += (c ^ d ^ a) + x[6] + 0x04881d05; /* 44 */ 357 357 b = ((b << 23) | (b >>> 9)) + c; 358 358 359 359 a += (b ^ c ^ d) + x[9] + 0xd9d4d039; /* 33 */ 360 360 a = ((a << 4) | (a >>> 28)) + b; … … 365 365 b += (c ^ d ^ a) + x[2] + 0xc4ac5665; /* 36 */ 366 366 b = ((b << 23) | (b >>> 9)) + c; 367 367 368 368 /* Round 4 */ 369 369 a += (c ^ (b | ~d)) + x[0] + 0xf4292244; /* 49 */ … … 375 375 b += (d ^ (c | ~a)) + x[5] + 0xfc93a039; /* 52 */ 376 376 b = ((b << 21) | (b >>> 11)) + c; 377 377 378 378 a += (c ^ (b | ~d)) + x[12] + 0x655b59c3; /* 53 */ 379 379 a = ((a << 6) | (a >>> 26)) + b; … … 384 384 b += (d ^ (c | ~a)) + x[1] + 0x85845dd1; /* 56 */ 385 385 b = ((b << 21) | (b >>> 11)) + c; 386 386 387 387 a += (c ^ (b | ~d)) + x[8] + 0x6fa87e4f; /* 57 */ 388 388 a = ((a << 6) | (a >>> 26)) + b; … … 393 393 b += (d ^ (c | ~a)) + x[13] + 0x4e0811a1; /* 60 */ 394 394 b = ((b << 21) | (b >>> 11)) + c; 395 395 396 396 a += (c ^ (b | ~d)) + x[4] + 0xf7537e82; /* 61 */ 397 397 a = ((a << 6) | (a >>> 26)) + b; … … 402 402 b += (d ^ (c | ~a)) + x[9] + 0xeb86d391; /* 64 */ 403 403 b = ((b << 21) | (b >>> 11)) + c; 404 404 405 405 state.state[0] += a; 406 406 state.state[1] += b; … … 408 408 state.state[3] += d; 409 409 } 410 410 411 411 /** 412 412 * Updates hash with the bytebuffer given (using at maximum length bytes … … 430 430 length = buffer.length - offset; 431 431 } 432 432 433 433 /* compute number of bytes mod 64 */ 434 434 435 435 index = (int) (stat.count & 0x3f); 436 436 stat.count += length; 437 437 438 438 partlen = 64 - index; 439 439 440 440 if (length >= partlen) { 441 441 // update state (using only Java) to reflect input … … 462 462 } 463 463 } 464 464 465 465 private static final byte[] encode( final int input[], final int len) { 466 466 int i, j; … … 475 475 return out; 476 476 } 477 477 478 478 public MD5() { 479 479 state = new MD5State(); 480 480 finals = null; 481 481 } 482 482 483 483 public void reset() { 484 484 state = new MD5State(); 485 485 finals = null; 486 486 } 487 487 488 488 /** 489 489 * Updates hash with given array of bytes … … 498 498 update( state, buffer,0, buffer.length ); 499 499 } 500 500 501 501 /** 502 502 * Returns array of bytes (16 bytes) representing hash as of the current … … 506 506 * @return Array of 16 bytes, the hash of all updated bytes 507 507 */ 508 public synchronizedfinal byte[] getDigest() {508 public final byte[] getDigest() { 509 509 byte bits[]; 510 510 int index, padlen; … … 521 521 finals = fin; 522 522 } 523 523 524 524 return encode(finals.state, 16); 525 525 }
Note: See TracChangeset
for help on using the changeset viewer.
