1 26 package com.Ostermiller.util; 27 28 import java.io.*; 29 30 54 public class MD5 { 55 56 61 public MD5 () { 62 reset(); 63 } 64 65 73 public static void main (String [] args){ 74 if (args.length == 0){ 75 System.err.println("Please specify a file."); 76 } else { 77 for (int i=0; i<args.length; i++){ 78 try { 79 System.out.println(MD5.getHashString(new File(args[i])) + " " + args[i]); 80 } catch (IOException x){ 81 System.err.println(x.getMessage()); 82 } 83 } 84 } 85 } 86 87 94 public byte[] getHash() { 95 if (!finalState.valid) { 96 finalState.copy(workingState); 97 long bitCount = finalState.bitCount; 98 int leftOver = (int) (((bitCount >>> 3)) & 0x3f); 100 int padlen = (leftOver < 56) ? (56 - leftOver) : (120 - leftOver); 102 update(finalState, padding, 0, padlen); 104 update(finalState, encode(bitCount), 0, 8); 106 finalState.valid = true; 107 } 108 return encode(finalState.state, 16); 110 } 111 112 119 public String getHashString(){ 120 return toHex(this.getHash()); 121 } 122 123 131 public static byte[] getHash(byte[] b){ 132 MD5 md5 = new MD5(); 133 md5.update(b); 134 return md5.getHash(); 135 } 136 137 145 public static String getHashString(byte[] b){ 146 MD5 md5 = new MD5(); 147 md5.update(b); 148 return md5.getHashString(); 149 } 150 151 160 public static byte[] getHash(InputStream in) throws IOException { 161 MD5 md5 = new MD5(); 162 byte[] buffer = new byte[1024]; 163 int read; 164 while ((read = in.read(buffer)) != -1){ 165 md5.update(buffer, read); 166 } 167 return md5.getHash(); 168 } 169 170 179 public static String getHashString(InputStream in) throws IOException { 180 MD5 md5 = new MD5(); 181 byte[] buffer = new byte[1024]; 182 int read; 183 while ((read = in.read(buffer)) != -1){ 184 md5.update(buffer, read); 185 } 186 return md5.getHashString(); 187 } 188 189 198 public static byte[] getHash(File f) throws IOException { 199 return getHash(new FileInputStream(f)); 200 } 201 202 211 public static String getHashString(File f) throws IOException { 212 return getHashString(new FileInputStream(f)); 213 } 214 215 225 public static byte[] getHash(String s){ 226 MD5 md5 = new MD5(); 227 md5.update(s); 228 return md5.getHash(); 229 } 230 231 241 public static String getHashString(String s){ 242 MD5 md5 = new MD5(); 243 md5.update(s); 244 return md5.getHashString(); 245 } 246 247 248 258 public static byte[] getHash(String s, String enc) throws UnsupportedEncodingException { 259 MD5 md5 = new MD5(); 260 md5.update(s, enc); 261 return md5.getHash(); 262 } 263 264 274 public static String getHashString(String s, String enc) throws UnsupportedEncodingException { 275 MD5 md5 = new MD5(); 276 md5.update(s, enc); 277 return md5.getHashString(); 278 } 279 280 281 286 public void reset() { 287 workingState.reset(); 288 finalState.valid = false; 289 } 290 291 298 public String toString(){ 299 return getHashString(); 300 } 301 302 319 private void update (MD5State state, byte buffer[], int offset, int length) { 320 321 finalState.valid = false; 322 323 if ((length + offset) > buffer.length){ 325 length = buffer.length - offset; 326 } 327 328 int index = (int) (state.bitCount >>> 3) & 0x3f; 332 333 state.bitCount += length << 3; 335 336 int partlen = 64 - index; 337 338 int i = 0; 339 if (length >= partlen) { 340 System.arraycopy(buffer, offset, state.buffer, index, partlen); 341 transform(state, decode(state.buffer, 64, 0)); 342 for (i = partlen; (i + 63) < length; i+= 64){ 343 transform(state, decode(buffer, 64, i)); 344 } 345 index = 0; 346 } 347 348 if (i < length) { 350 for (int start = i; i < length; i++) { 351 state.buffer[index + i - start] = buffer[i + offset]; 352 } 353 } 354 } 355 356 368 public void update (byte buffer[], int offset, int length) { 369 update(workingState, buffer, offset, length); 370 } 371 372 383 public void update (byte buffer[], int length) { 384 update(buffer, 0, length); 385 } 386 387 394 public void update (byte buffer[]) { 395 update(buffer, 0, buffer.length); 396 } 397 398 405 public void update (byte b) { 406 byte buffer[] = new byte[1]; 407 buffer[0] = b; 408 update(buffer, 1); 409 } 410 411 420 private void update (MD5State state, long l) { 421 update( 422 state, 423 new byte[] { 424 (byte)((l >>> 0) & 0xff), 425 (byte)((l >>> 8) & 0xff), 426 (byte)((l >>> 16) & 0xff), 427 (byte)((l >>> 24) & 0xff), 428 (byte)((l >>> 32) & 0xff), 429 (byte)((l >>> 40) & 0xff), 430 (byte)((l >>> 48) & 0xff), 431 (byte)((l >>> 56) & 0xff), 432 }, 433 0, 434 8 435 ); 436 } 437 438 447 public void update (String s) { 448 update(s.getBytes()); 449 } 450 451 460 public void update (String s, String enc) throws UnsupportedEncodingException { 461 update(s.getBytes(enc)); 462 } 463 464 470 private MD5State workingState = new MD5State(); 471 472 479 private MD5State finalState = new MD5State(); 480 481 486 private int[] decodeBuffer = new int[16]; 487 488 494 private static final byte padding[] = { 495 (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 496 0, 0, 0, 0, 0, 0, 0, 0, 497 0, 0, 0, 0, 0, 0, 0, 0, 498 0, 0, 0, 0, 0, 0, 0, 0, 499 0, 0, 0, 0, 0, 0, 0, 0, 500 0, 0, 0, 0, 0, 0, 0, 0, 501 0, 0, 0, 0, 0, 0, 0, 0, 502 0, 0, 0, 0, 0, 0, 0, 0, 503 }; 504 505 511 private class MD5State { 512 513 518 public boolean valid = true; 519 520 525 public void reset(){ 526 state[0] = 0x67452301; 527 state[1] = 0xefcdab89; 528 state[2] = 0x98badcfe; 529 state[3] = 0x10325476; 530 531 bitCount = 0; 532 } 533 534 539 public int state[] = new int[4]; 540 541 546 public long bitCount; 547 548 553 public byte buffer[] = new byte[64]; 554 555 public MD5State() { 556 reset(); 557 } 558 559 566 public void copy(MD5State from) { 567 System.arraycopy(from.buffer, 0, this.buffer, 0, this.buffer.length); 568 System.arraycopy(from.state, 0, this.state, 0, this.state.length); 569 this.valid = from.valid; 570 this.bitCount = from.bitCount; 571 } 572 573 public String toString(){ 574 return state[0] + " " + state[1] + " " + state[2] + " " + state[3]; 575 } 576 } 577 578 579 588 private static String toHex(byte hash[]){ 589 StringBuffer buf = new StringBuffer (hash.length * 2); 590 for (int i=0; i<hash.length; i++){ 591 int intVal = hash[i] & 0xff; 592 if (intVal < 0x10){ 593 buf.append("0"); 596 } 597 buf.append(Integer.toHexString(intVal)); 598 } 599 return buf.toString(); 600 } 601 602 private static int FF (int a, int b, int c, int d, int x, int s, int ac) { 603 a += ((b & c) | (~b & d)); 604 a += x; 605 a += ac; 606 a = (a << s) | (a >>> (32 - s)); 608 return a + b; 609 } 610 611 private static int GG (int a, int b, int c, int d, int x, int s, int ac) { 612 a += ((b & d) | (c & ~d)); 613 a += x; 614 a += ac; 615 a = (a << s) | (a >>> (32 - s)); 617 return a + b; 618 } 619 620 private static int HH (int a, int b, int c, int d, int x, int s, int ac) { 621 a += (b ^ c ^ d); 622 a += x; 623 a += ac; 624 a = (a << s) | (a >>> (32 - s)); 626 return a + b; 627 } 628 629 private static int II (int a, int b, int c, int d, int x, int s, int ac) { 630 a += (c ^ (b | ~d)); 631 a += x; 632 a += ac; 633 a = (a << s) | (a >>> (32 - s)); 635 return a + b; 636 } 637 638 private static byte[] encode(long l){ 639 byte[] out = new byte[8]; 640 out[0] = (byte) (l & 0xff); 641 out[1] = (byte) ((l >>> 8) & 0xff); 642 out[2] = (byte) ((l >>> 16) & 0xff); 643 out[3] = (byte) ((l >>> 24) & 0xff); 644 out[4] = (byte) ((l >>> 32) & 0xff); 645 out[5] = (byte) ((l >>> 40) & 0xff); 646 out[6] = (byte) ((l >>> 48) & 0xff); 647 out[7] = (byte) ((l >>> 56) & 0xff); 648 return out; 649 } 650 651 private static byte[] encode(int input[], int len){ 652 byte[] out = new byte[len]; 653 int i, j; 654 for (i = j = 0; j < len; i++, j += 4) { 655 out[j] = (byte) (input[i] & 0xff); 656 out[j + 1] = (byte) ((input[i] >>> 8) & 0xff); 657 out[j + 2] = (byte) ((input[i] >>> 16) & 0xff); 658 out[j + 3] = (byte) ((input[i] >>> 24) & 0xff); 659 } 660 return out; 661 } 662 663 private int[] decode(byte buffer[], int len, int offset){ 664 int i, j; 665 for (i = j = 0; j < len; i++, j += 4) { 666 decodeBuffer[i] = ( 667 (int) (buffer[j + offset] & 0xff)) | 668 (((int) (buffer[j + 1 + offset] & 0xff)) << 8) | 669 (((int) (buffer[j + 2 + offset] & 0xff)) << 16) | 670 (((int) (buffer[j + 3 + offset] & 0xff)) << 24 671 ); 672 } 673 return decodeBuffer; 674 } 675 676 private static void transform(MD5State state, int[] x){ 677 int a = state.state[0]; 678 int b = state.state[1]; 679 int c = state.state[2]; 680 int d = state.state[3]; 681 682 683 a = FF (a, b, c, d, x[ 0], 7, 0xd76aa478); 684 d = FF (d, a, b, c, x[ 1], 12, 0xe8c7b756); 685 c = FF (c, d, a, b, x[ 2], 17, 0x242070db); 686 b = FF (b, c, d, a, x[ 3], 22, 0xc1bdceee); 687 a = FF (a, b, c, d, x[ 4], 7, 0xf57c0faf); 688 d = FF (d, a, b, c, x[ 5], 12, 0x4787c62a); 689 c = FF (c, d, a, b, x[ 6], 17, 0xa8304613); 690 b = FF (b, c, d, a, x[ 7], 22, 0xfd469501); 691 a = FF (a, b, c, d, x[ 8], 7, 0x698098d8); 692 d = FF (d, a, b, c, x[ 9], 12, 0x8b44f7af); 693 c = FF (c, d, a, b, x[10], 17, 0xffff5bb1); 694 b = FF (b, c, d, a, x[11], 22, 0x895cd7be); 695 a = FF (a, b, c, d, x[12], 7, 0x6b901122); 696 d = FF (d, a, b, c, x[13], 12, 0xfd987193); 697 c = FF (c, d, a, b, x[14], 17, 0xa679438e); 698 b = FF (b, c, d, a, x[15], 22, 0x49b40821); 699 700 701 a = GG (a, b, c, d, x[ 1], 5, 0xf61e2562); 702 d = GG (d, a, b, c, x[ 6], 9, 0xc040b340); 703 c = GG (c, d, a, b, x[11], 14, 0x265e5a51); 704 b = GG (b, c, d, a, x[ 0], 20, 0xe9b6c7aa); 705 a = GG (a, b, c, d, x[ 5], 5, 0xd62f105d); 706 d = GG (d, a, b, c, x[10], 9, 0x02441453); 707 c = GG (c, d, a, b, x[15], 14, 0xd8a1e681); 708 b = GG (b, c, d, a, x[ 4], 20, 0xe7d3fbc8); 709 a = GG (a, b, c, d, x[ 9], 5, 0x21e1cde6); 710 d = GG (d, a, b, c, x[14], 9, 0xc33707d6); 711 c = GG (c, d, a, b, x[ 3], 14, 0xf4d50d87); 712 b = GG (b, c, d, a, x[ 8], 20, 0x455a14ed); 713 a = GG (a, b, c, d, x[13], 5, 0xa9e3e905); 714 d = GG (d, a, b, c, x[ 2], 9, 0xfcefa3f8); 715 c = GG (c, d, a, b, x[ 7], 14, 0x676f02d9); 716 b = GG (b, c, d, a, x[12], 20, 0x8d2a4c8a); 717 718 719 a = HH (a, b, c, d, x[ 5], 4, 0xfffa3942); 720 d = HH (d, a, b, c, x[ 8], 11, 0x8771f681); 721 c = HH (c, d, a, b, x[11], 16, 0x6d9d6122); 722 b = HH (b, c, d, a, x[14], 23, 0xfde5380c); 723 a = HH (a, b, c, d, x[ 1], 4, 0xa4beea44); 724 d = HH (d, a, b, c, x[ 4], 11, 0x4bdecfa9); 725 c = HH (c, d, a, b, x[ 7], 16, 0xf6bb4b60); 726 b = HH (b, c, d, a, x[10], 23, 0xbebfbc70); 727 a = HH (a, b, c, d, x[13], 4, 0x289b7ec6); 728 d = HH (d, a, b, c, x[ 0], 11, 0xeaa127fa); 729 c = HH (c, d, a, b, x[ 3], 16, 0xd4ef3085); 730 b = HH (b, c, d, a, x[ 6], 23, 0x04881d05); 731 a = HH (a, b, c, d, x[ 9], 4, 0xd9d4d039); 732 d = HH (d, a, b, c, x[12], 11, 0xe6db99e5); 733 c = HH (c, d, a, b, x[15], 16, 0x1fa27cf8); 734 b = HH (b, c, d, a, x[ 2], 23, 0xc4ac5665); 735 736 737 a = II (a, b, c, d, x[ 0], 6, 0xf4292244); 738 d = II (d, a, b, c, x[ 7], 10, 0x432aff97); 739 c = II (c, d, a, b, x[14], 15, 0xab9423a7); 740 b = II (b, c, d, a, x[ 5], 21, 0xfc93a039); 741 a = II (a, b, c, d, x[12], 6, 0x655b59c3); 742 d = II (d, a, b, c, x[ 3], 10, 0x8f0ccc92); 743 c = II (c, d, a, b, x[10], 15, 0xffeff47d); 744 b = II (b, c, d, a, x[ 1], 21, 0x85845dd1); 745 a = II (a, b, c, d, x[ 8], 6, 0x6fa87e4f); 746 d = II (d, a, b, c, x[15], 10, 0xfe2ce6e0); 747 c = II (c, d, a, b, x[ 6], 15, 0xa3014314); 748 b = II (b, c, d, a, x[13], 21, 0x4e0811a1); 749 a = II (a, b, c, d, x[ 4], 6, 0xf7537e82); 750 d = II (d, a, b, c, x[11], 10, 0xbd3af235); 751 c = II (c, d, a, b, x[ 2], 15, 0x2ad7d2bb); 752 b = II (b, c, d, a, x[ 9], 21, 0xeb86d391); 753 754 state.state[0] += a; 755 state.state[1] += b; 756 state.state[2] += c; 757 state.state[3] += d; 758 } 759 } 760 | Popular Tags |