1 26 package org.claros.chat.utility; 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 439 440 449 public void update (String s) { 450 update(s.getBytes()); 451 } 452 453 462 public void update (String s, String enc) throws UnsupportedEncodingException { 463 update(s.getBytes(enc)); 464 } 465 466 472 private MD5State workingState = new MD5State(); 473 474 481 private MD5State finalState = new MD5State(); 482 483 488 private int[] decodeBuffer = new int[16]; 489 490 496 private static final byte padding[] = { 497 (byte) 0x80, 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 0, 0, 0, 0, 0, 0, 0, 0, 504 0, 0, 0, 0, 0, 0, 0, 0, 505 }; 506 507 513 private class MD5State { 514 515 520 public boolean valid = true; 521 522 527 public void reset(){ 528 state[0] = 0x67452301; 529 state[1] = 0xefcdab89; 530 state[2] = 0x98badcfe; 531 state[3] = 0x10325476; 532 533 bitCount = 0; 534 } 535 536 541 public int state[] = new int[4]; 542 543 548 public long bitCount; 549 550 555 public byte buffer[] = new byte[64]; 556 557 public MD5State() { 558 reset(); 559 } 560 561 568 public void copy(MD5State from) { 569 System.arraycopy(from.buffer, 0, this.buffer, 0, this.buffer.length); 570 System.arraycopy(from.state, 0, this.state, 0, this.state.length); 571 this.valid = from.valid; 572 this.bitCount = from.bitCount; 573 } 574 575 public String toString(){ 576 return state[0] + " " + state[1] + " " + state[2] + " " + state[3]; 577 } 578 } 579 580 581 590 private static String toHex(byte hash[]){ 591 String buf = ""; 592 for (int i=0; i<hash.length; i++){ 593 int intVal = hash[i] & 0xff; 594 if (intVal < 0x10){ 595 buf += "0"; 598 } 599 buf += (Integer.toHexString(intVal)); 600 } 601 return buf.toString(); 602 } 603 604 private static int FF (int a, int b, int c, int d, int x, int s, int ac) { 605 a += ((b & c) | (~b & d)); 606 a += x; 607 a += ac; 608 a = (a << s) | (a >>> (32 - s)); 610 return a + b; 611 } 612 613 private static int GG (int a, int b, int c, int d, int x, int s, int ac) { 614 a += ((b & d) | (c & ~d)); 615 a += x; 616 a += ac; 617 a = (a << s) | (a >>> (32 - s)); 619 return a + b; 620 } 621 622 private static int HH (int a, int b, int c, int d, int x, int s, int ac) { 623 a += (b ^ c ^ d); 624 a += x; 625 a += ac; 626 a = (a << s) | (a >>> (32 - s)); 628 return a + b; 629 } 630 631 private static int II (int a, int b, int c, int d, int x, int s, int ac) { 632 a += (c ^ (b | ~d)); 633 a += x; 634 a += ac; 635 a = (a << s) | (a >>> (32 - s)); 637 return a + b; 638 } 639 640 private static byte[] encode(long l){ 641 byte[] out = new byte[8]; 642 out[0] = (byte) (l & 0xff); 643 out[1] = (byte) ((l >>> 8) & 0xff); 644 out[2] = (byte) ((l >>> 16) & 0xff); 645 out[3] = (byte) ((l >>> 24) & 0xff); 646 out[4] = (byte) ((l >>> 32) & 0xff); 647 out[5] = (byte) ((l >>> 40) & 0xff); 648 out[6] = (byte) ((l >>> 48) & 0xff); 649 out[7] = (byte) ((l >>> 56) & 0xff); 650 return out; 651 } 652 653 private static byte[] encode(int input[], int len){ 654 byte[] out = new byte[len]; 655 int i, j; 656 for (i = j = 0; j < len; i++, j += 4) { 657 out[j] = (byte) (input[i] & 0xff); 658 out[j + 1] = (byte) ((input[i] >>> 8) & 0xff); 659 out[j + 2] = (byte) ((input[i] >>> 16) & 0xff); 660 out[j + 3] = (byte) ((input[i] >>> 24) & 0xff); 661 } 662 return out; 663 } 664 665 private int[] decode(byte buffer[], int len, int offset){ 666 int i, j; 667 for (i = j = 0; j < len; i++, j += 4) { 668 decodeBuffer[i] = ( 669 (int) (buffer[j + offset] & 0xff)) | 670 (((int) (buffer[j + 1 + offset] & 0xff)) << 8) | 671 (((int) (buffer[j + 2 + offset] & 0xff)) << 16) | 672 (((int) (buffer[j + 3 + offset] & 0xff)) << 24 673 ); 674 } 675 return decodeBuffer; 676 } 677 678 private static void transform(MD5State state, int[] x){ 679 int a = state.state[0]; 680 int b = state.state[1]; 681 int c = state.state[2]; 682 int d = state.state[3]; 683 684 685 a = FF (a, b, c, d, x[ 0], 7, 0xd76aa478); 686 d = FF (d, a, b, c, x[ 1], 12, 0xe8c7b756); 687 c = FF (c, d, a, b, x[ 2], 17, 0x242070db); 688 b = FF (b, c, d, a, x[ 3], 22, 0xc1bdceee); 689 a = FF (a, b, c, d, x[ 4], 7, 0xf57c0faf); 690 d = FF (d, a, b, c, x[ 5], 12, 0x4787c62a); 691 c = FF (c, d, a, b, x[ 6], 17, 0xa8304613); 692 b = FF (b, c, d, a, x[ 7], 22, 0xfd469501); 693 a = FF (a, b, c, d, x[ 8], 7, 0x698098d8); 694 d = FF (d, a, b, c, x[ 9], 12, 0x8b44f7af); 695 c = FF (c, d, a, b, x[10], 17, 0xffff5bb1); 696 b = FF (b, c, d, a, x[11], 22, 0x895cd7be); 697 a = FF (a, b, c, d, x[12], 7, 0x6b901122); 698 d = FF (d, a, b, c, x[13], 12, 0xfd987193); 699 c = FF (c, d, a, b, x[14], 17, 0xa679438e); 700 b = FF (b, c, d, a, x[15], 22, 0x49b40821); 701 702 703 a = GG (a, b, c, d, x[ 1], 5, 0xf61e2562); 704 d = GG (d, a, b, c, x[ 6], 9, 0xc040b340); 705 c = GG (c, d, a, b, x[11], 14, 0x265e5a51); 706 b = GG (b, c, d, a, x[ 0], 20, 0xe9b6c7aa); 707 a = GG (a, b, c, d, x[ 5], 5, 0xd62f105d); 708 d = GG (d, a, b, c, x[10], 9, 0x02441453); 709 c = GG (c, d, a, b, x[15], 14, 0xd8a1e681); 710 b = GG (b, c, d, a, x[ 4], 20, 0xe7d3fbc8); 711 a = GG (a, b, c, d, x[ 9], 5, 0x21e1cde6); 712 d = GG (d, a, b, c, x[14], 9, 0xc33707d6); 713 c = GG (c, d, a, b, x[ 3], 14, 0xf4d50d87); 714 b = GG (b, c, d, a, x[ 8], 20, 0x455a14ed); 715 a = GG (a, b, c, d, x[13], 5, 0xa9e3e905); 716 d = GG (d, a, b, c, x[ 2], 9, 0xfcefa3f8); 717 c = GG (c, d, a, b, x[ 7], 14, 0x676f02d9); 718 b = GG (b, c, d, a, x[12], 20, 0x8d2a4c8a); 719 720 721 a = HH (a, b, c, d, x[ 5], 4, 0xfffa3942); 722 d = HH (d, a, b, c, x[ 8], 11, 0x8771f681); 723 c = HH (c, d, a, b, x[11], 16, 0x6d9d6122); 724 b = HH (b, c, d, a, x[14], 23, 0xfde5380c); 725 a = HH (a, b, c, d, x[ 1], 4, 0xa4beea44); 726 d = HH (d, a, b, c, x[ 4], 11, 0x4bdecfa9); 727 c = HH (c, d, a, b, x[ 7], 16, 0xf6bb4b60); 728 b = HH (b, c, d, a, x[10], 23, 0xbebfbc70); 729 a = HH (a, b, c, d, x[13], 4, 0x289b7ec6); 730 d = HH (d, a, b, c, x[ 0], 11, 0xeaa127fa); 731 c = HH (c, d, a, b, x[ 3], 16, 0xd4ef3085); 732 b = HH (b, c, d, a, x[ 6], 23, 0x04881d05); 733 a = HH (a, b, c, d, x[ 9], 4, 0xd9d4d039); 734 d = HH (d, a, b, c, x[12], 11, 0xe6db99e5); 735 c = HH (c, d, a, b, x[15], 16, 0x1fa27cf8); 736 b = HH (b, c, d, a, x[ 2], 23, 0xc4ac5665); 737 738 739 a = II (a, b, c, d, x[ 0], 6, 0xf4292244); 740 d = II (d, a, b, c, x[ 7], 10, 0x432aff97); 741 c = II (c, d, a, b, x[14], 15, 0xab9423a7); 742 b = II (b, c, d, a, x[ 5], 21, 0xfc93a039); 743 a = II (a, b, c, d, x[12], 6, 0x655b59c3); 744 d = II (d, a, b, c, x[ 3], 10, 0x8f0ccc92); 745 c = II (c, d, a, b, x[10], 15, 0xffeff47d); 746 b = II (b, c, d, a, x[ 1], 21, 0x85845dd1); 747 a = II (a, b, c, d, x[ 8], 6, 0x6fa87e4f); 748 d = II (d, a, b, c, x[15], 10, 0xfe2ce6e0); 749 c = II (c, d, a, b, x[ 6], 15, 0xa3014314); 750 b = II (b, c, d, a, x[13], 21, 0x4e0811a1); 751 a = II (a, b, c, d, x[ 4], 6, 0xf7537e82); 752 d = II (d, a, b, c, x[11], 10, 0xbd3af235); 753 c = II (c, d, a, b, x[ 2], 15, 0x2ad7d2bb); 754 b = II (b, c, d, a, x[ 9], 21, 0xeb86d391); 755 756 state.state[0] += a; 757 state.state[1] += b; 758 state.state[2] += c; 759 state.state[3] += d; 760 } 761 } | Popular Tags |