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;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Hash {

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

    }

    private static String md5(String s) { try { 
       
        // Create MD5 Hash 
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); 
        digest.update(s.getBytes()); 
        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) {
         e.printStackTrace();
     }
     return "";

    }
   
   
}