Free, tested & ready to use examples!
AnyExample.com
 
Web anyexample.com
 

Java simple class to compute MD5 hash

abstract 
MD5 is a cryptographic message digest algorithm. MD5 hash considered to be one of the most widely-used used secure hashing functions, producing a 128-bit digest (32 hex numbers) from any data. While Java has built-in cryptographic checksum classes, it's quite uneasy to use them for a simple task -- calculate MD5 hash from string and return 32-byte hexadecimal representation.
compatible 
  • Sun Java 1.4 or higher
  • We use MessageDigest class, first setting it to use MD5 algorithm, than feeding it with source data and getting byte array with hash value. To convert this array to hex-string we use our own method convertToHex.

    Here is AeSimpleMD5 class, which single public static method MD5 returns hexadecimal string representation for MD5 hash of input argument:

    source code: Java
     
    import java.io.UnsupportedEncodingException; 
    import java.security.MessageDigest; 
    import java.security.NoSuchAlgorithmException; 
     
    public class AeSimpleMD5 { 
     
        private static String convertToHex(byte[] data) { 
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < data.length; i++) { 
                int halfbyte = (data[i] >>> 4) & 0x0F;
                int two_halfs = 0;
                do { 
                    if ((0 <= halfbyte) && (halfbyte <= 9)) 
                        buf.append((char) ('0' + halfbyte));
                    else 
                        buf.append((char) ('a' + (halfbyte - 10)));
                    halfbyte = data[i] & 0x0F;
                } while(two_halfs++ < 1);
            } 
            return buf.toString();
        } 
     
        public static String MD5(String text) 
        throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
            MessageDigest md;
            md = MessageDigest.getInstance("MD5");
            byte[] md5hash = new byte[32];
            md.update(text.getBytes("iso-8859-1"), 0, text.length());
            md5hash = md.digest();
            return convertToHex(md5hash);
        } 
    } 
     

    You may use this class directly calling this method

    source code: Java
     
    String MD5_ad1 = AeSimpleMD5.MD5("Java solutions class libraries");
    String MD5_ad2 = AeSimpleMD5.MD5("Java Toolkits");
     

    One more example, allows user to input string from console and prints MD5 hash

    source code: Java
     
    import java.io.BufferedReader; 
    import java.io.IOException; 
    import java.io.InputStreamReader; 
    import java.io.UnsupportedEncodingException; 
    import java.security.NoSuchAlgorithmException; 
     
    public class Ex01 { 
     
        public static void main(String[] args) throws IOException { 
            BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
     
            System.out.println("Enter string:");
            String rawString = userInput.readLine();
     
            try { 
                System.out.println("MD5 hash of string: " + AeSimpleMD5.MD5(rawString));
            } catch (NoSuchAlgorithmException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace();
            } 
        } 
    } 
     
    warning 
  • This class is suitable to calculate hash for small chunks of in-memory string data. To process large external files (like ISO images) you should use MessageDigest class directly
  • There may be difficulties while processing string data in encodings other than iso-8859-1.
  • tested 
  • JDK 1.5
  •  


     
    © AnyExample 2010-2013
    License | Privacy | Contact