Blog Archive

Android generate md5 hash example

The MD5 Message-Digest Algorithm is a widely used cryptographic hash function that produces a 128-bit (16-byte) hash value. Specified in RFC 1321, MD5 has been employed in a wide variety of security applications, and is also commonly used to check data integrity. However, it has been shown that MD5 is not collision resistant; as such, MD5 is not suitable for applications like SSL certificates or digital signatures that rely on this property. An MD5 hash is typically expressed as a 32-digit hexadecimal number.
package com.as400samplecode;


public class MD5Hash {

    public static void main(String[] args) {
        String s = "SecretKey20111013000";
        String  res = md5(s);


    private static String md5(String s) { try { 
        // Create MD5 Hash 
        MessageDigest digest ="MD5"); 
        byte messageDigest[] = digest.digest();

         // Create Hex String
         StringBuffer hexString = new StringBuffer();
         for (int i=0; i<messageDigest.length; i++)
             hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        return hexString.toString();

     } catch (NoSuchAlgorithmException e) {
     return "";