Android MD5 Encode md5(String s)

Here you can find the source of md5(String s)

Description

md

Declaration

public static String md5(String s) 

Method Source Code

//package com.java2s;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String md5(String s) {
        /*/* ww w.j a  v a  2  s. co m*/
         * 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 "";
         */
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
            digest.update(s.getBytes(), 0, s.length());
            String hash = new BigInteger(1, digest.digest()).toString(16);
            if (hash.length() % 2 != 0) {
                return "0" + hash;
            }
            return hash;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}

Related

  1. MD5Crypto(String str)
  2. getMD5Str(String str)
  3. MD5(String s)
  4. MD5Crypto(String str)
  5. stringToMD5(String string)
  6. md5sum(String string)