MD5 algorithm RFC 1321 : MD5 « Security « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Security » MD5Screenshots 
MD5 algorithm RFC 1321
 


/***
* jwma Java WebMail
* Copyright (c) 2000-2003 jwma team
*
* jwma is free software; you can distribute and use this source
* under the terms of the BSD-style license received along with
* the distribution.
 ***/

/**
 * This class implements the MD5 algorithm.
 * <p>
 * The specification is available from RFC 1321, and there are numerous
 * implementations out there, this one was tuned specifically for hashing short
 * strings (i.e. passwords).
 * <p>
 * I do not recommend to use this implementation for anything else but that, and
 * if anybody feels that this code is to "close" to something available on the
 * net, please contact me, and I will certainly take steps to clear up the
 * situation.
 */
public final class MD5 {

  /**
   * Returns the MD5 hash digest transformed into a hex
   * representation as <tt>String</tt>.
   *
   @return the MD5 hash of the input as <tt>String</tt>.
   */
  public static final String hash(String str) {
    return MD5.asHex(MD5.digest(str.getBytes()));
  }//hash

  /**
   * Returns the MD5 hash of the input data.
   * <p>
   * Following the the MD5 standard specification, the result
   * is returned least significant byte first, however,
   * many applications use the most significant byte first (more conventional).
   *
   @param msg the <tt>byte[]</tt> to be digested.
   *
   @return the MD5 hash of the data as <tt>String</tt>.
   */
  public static final byte[] digest(byte[] msg) {
    int padsize = (120 (msg.length % 64)) 64;
    int i;
    long l = msg.length * 8;

    //MD5 registers
    int a = 0x67452301, aa,
        b = 0xefcdab89, bb,
        c = 0x98badcfe, cc,
        d = 0x10325476, dd;

    byte[] src = new byte[msg.length + padsize + 8];

    //padding
    try {
      System.arraycopy(msg, 0, src, 0, msg.length);
    catch (Exception ex) {
      //should not happen, but calm's the compiler
    }

    src[msg.length(byte0x80;

    for (i = msg.length + 1; i < msg.length + padsize; i++src[i0;

    //append length
    for (i = src.length - 8; i < src.length; i++) {
      src[i(bytel;
      l >>>= 8;
    }
    int[] x = new int[16];

    //prcess the data 16-word blocks
    for (i = 0; i < src.length; i += 64) {
      //construct block
      for (int j = 0; j < 16; j++) {
        x[j0;
        for (int k = 3; k >= 0; k--) {
          x[j<<= 8;
          x[j+= src[i + j * + k0xff;
        }
      }
      aa = a;
      bb = b;
      cc = c;
      dd = d;
      a = round1(a, b, c, d, 071, x);
      d = round1(d, a, b, c, 1122, x);
      c = round1(c, d, a, b, 2173, x);
      b = round1(b, c, d, a, 3224, x);

      a = round1(a, b, c, d, 475, x);
      d = round1(d, a, b, c, 5126, x);
      c = round1(c, d, a, b, 6177, x);
      b = round1(b, c, d, a, 7228, x);

      a = round1(a, b, c, d, 879, x);
      d = round1(d, a, b, c, 91210, x);
      c = round1(c, d, a, b, 101711, x);
      b = round1(b, c, d, a, 112212, x);

      a = round1(a, b, c, d, 12713, x);
      d = round1(d, a, b, c, 131214, x);
      c = round1(c, d, a, b, 141715, x);
      b = round1(b, c, d, a, 152216, x);

      a = round2(a, b, c, d, 1517, x);
      d = round2(d, a, b, c, 6918, x);
      c = round2(c, d, a, b, 111419, x);
      b = round2(b, c, d, a, 02020, x);

      a = round2(a, b, c, d, 5521, x);
      d = round2(d, a, b, c, 10922, x);
      c = round2(c, d, a, b, 151423, x);
      b = round2(b, c, d, a, 42024, x);

      a = round2(a, b, c, d, 9525, x);
      d = round2(d, a, b, c, 14926, x);
      c = round2(c, d, a, b, 31427, x);
      b = round2(b, c, d, a, 82028, x);

      a = round2(a, b, c, d, 13529, x);
      d = round2(d, a, b, c, 2930, x);
      c = round2(c, d, a, b, 71431, x);
      b = round2(b, c, d, a, 122032, x);

      a = round3(a, b, c, d, 5433, x);
      d = round3(d, a, b, c, 81134, x);
      c = round3(c, d, a, b, 111635, x);
      b = round3(b, c, d, a, 142336, x);

      a = round3(a, b, c, d, 1437, x);
      d = round3(d, a, b, c, 41138, x);
      c = round3(c, d, a, b, 71639, x);
      b = round3(b, c, d, a, 102340, x);

      a = round3(a, b, c, d, 13441, x);
      d = round3(d, a, b, c, 01142, x);
      c = round3(c, d, a, b, 31643, x);
      b = round3(b, c, d, a, 62344, x);

      a = round3(a, b, c, d, 9445, x);
      d = round3(d, a, b, c, 121146, x);
      c = round3(c, d, a, b, 151647, x);
      b = round3(b, c, d, a, 22348, x);

      a = round4(a, b, c, d, 0649, x);
      d = round4(d, a, b, c, 71050, x);
      c = round4(c, d, a, b, 141551, x);
      b = round4(b, c, d, a, 52152, x);

      a = round4(a, b, c, d, 12653, x);
      d = round4(d, a, b, c, 31054, x);
      c = round4(c, d, a, b, 101555, x);
      b = round4(b, c, d, a, 12156, x);

      a = round4(a, b, c, d, 8657, x);
      d = round4(d, a, b, c, 151058, x);
      c = round4(c, d, a, b, 61559, x);
      b = round4(b, c, d, a, 132160, x);

      a = round4(a, b, c, d, 4661, x);
      d = round4(d, a, b, c, 111062, x);
      c = round4(c, d, a, b, 21563, x);
      b = round4(b, c, d, a, 92164, x);

      a += aa;
      b += bb;
      c += cc;
      d += dd;
    }
    byte[] ret = new byte[16];

    for (i = 0; i < 4; i++) {
      ret[i(bytea;
      a >>>= 8;
    }
    for (; i < 8; i++) {
      ret[i(byteb;
      b >>>= 8;
    }
    for (; i < 12; i++) {
      ret[i(bytec;
      c >>>= 8;
    }
    for (; i < 16; i++) {
      ret[i(byted;
      d >>>= 8;
    }
    return ret;
  }//digest


  /** MD5 Transformation routines *********************************************/

  private static final int rot(int x, int s) {
    return x << s | x >>> (32 - s);
  }//rot

  private static final int F(int x, int y, int z) {
    return (x & y(~x & z);
  }//F

  private static final int G(int x, int y, int z) {
    return (x & z(y & ~z);
  }//G

  private static final int H(int x, int y, int z) {
    return x ^ y ^ z;
  }//H

  private static final int I(int x, int y, int z) {
    return y ^ (x | ~z);
  }//I

  private static final int round1(int a, int b, int c,
                                  int d, int k, int s,
                                  int i, int[] x) {
    return b + rot((a + F(b, c, d+ x[k+ T[i - 1]), s);
  }//round1

  private static final int round2(int a, int b, int c,
                                  int d, int k, int s,
                                  int i, int[] x) {
    return b + rot((a + G(b, c, d+ x[k+ T[i - 1]), s);
  }//round2

  private static final int round3(int a, int b, int c,
                                  int d, int k, int s,
                                  int i, int[] x) {
    return b + rot((a + H(b, c, d+ x[k+ T[i - 1]), s);
  }//round3

  private static final int round4(int a, int b, int c,
                                  int d, int k, int s,
                                  int i, int[] x) {
    return b + rot((a + I(b, c, d+ x[k+ T[i - 1]), s);
  }//round4

  private static final int T[] {
    0xd76aa4780xe8c7b7560x242070db0xc1bdceee0xf57c0faf,
    0x4787c62a0xa83046130xfd4695010x698098d80x8b44f7af,
    0xffff5bb10x895cd7be0x6b9011220xfd9871930xa679438e,
    0x49b408210xf61e25620xc040b3400x265e5a510xe9b6c7aa,
    0xd62f105d0x024414530xd8a1e6810xe7d3fbc80x21e1cde6,
    0xc33707d60xf4d50d870x455a14ed0xa9e3e9050xfcefa3f8,
    0x676f02d90x8d2a4c8a0xfffa39420x8771f6810x6d9d6122,
    0xfde5380c0xa4beea440x4bdecfa90xf6bb4b600xbebfbc70,
    0x289b7ec60xeaa127fa0xd4ef30850x04881d050xd9d4d039,
    0xe6db99e50x1fa27cf80xc4ac56650xf42922440x432aff97,
    0xab9423a70xfc93a0390x655b59c30x8f0ccc920xffeff47d,
    0x85845dd10x6fa87e4f0xfe2ce6e00xa30143140x4e0811a1,
    0xf7537e820xbd3af2350x2ad7d2bb0xeb86d391
  };

  /** END MD5 Transformation routines *****************************************/

  /**
   * Returns a <tt>String</tt> containing unsigned hexadecimal
   * numbers as digits.
   * <p>
   * Contains two hex digit characters for each byte from the passed in
   * <tt>byte[]</tt>.
   *
   @param data the array of bytes to be converted into a hex-string.
   @return  the generated hexadecimal representation as
   *          <tt>String</tt>.
   */
  public static final String asHex(byte[] data) {
    //double size, two bytes (hex range) for one byte
    StringBuffer buf = new StringBuffer(data.length * 2);
    for (int i = 0; i < data.length; i++) {
      //don't forget the second hex digit
      if (((intdata[i0xff0x10) {
        buf.append("0");
      }
      buf.append(Long.toString((intdata[i0xff16));
    }
    return buf.toString();
  }//asHex

  /**
   * A main, to allow using this class from the command line.
   */
  public static void main(String[] args) {
    try {
      if (args == null || args.length == 0) {
        System.out.println("Usage: java dtw.webmail.util.MD5 <password>");
      }
      System.out.println("Hash=" + hash(args[0]));
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }//main

}//class MD5

   
  
Related examples in the same category
1. OTP one-time password calculationOTP one-time password calculation
2. Applet to serve as an s/key calculator application wrapper around otp class
3. Creating a Keyed Digest Using MD5
4. MD5 BASE64 checksum for the specified input string.
5. MD5 InputStream
6. Implements MD5 functionality on a stream.
7. Fast implementation of RSA's MD5 hash generator in Java JDK Beta-2 or higher
8. Contains internal state of the MD5 class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.