Java MD5 String MD5(String str)

Here you can find the source of MD5(String str)

Description

MD

License

Open Source License

Declaration

public static String MD5(String str) 

Method Source Code


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

public class Main {
    public static String MD5(String str) {
        return MD5(str.getBytes());
    }/*from ww  w  .  j  a  v a 2 s  . c o m*/

    /**
     * MD5  encrypt method
     * @param msg original data
     * @return encrypt data
     */
    public static String MD5(byte[] msg) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(msg);

            byte[] digest = md.digest();
            StringBuffer buf = new StringBuffer(digest.length * 2);

            for (int i = 0; i < digest.length; i++) {
                int intVal = digest[i] & 0xff;

                if (intVal < 0x10) {
                    buf.append("0");
                }

                buf.append(Integer.toHexString(intVal));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException ne) {
            return null;
        }
    }
}

Related

  1. md5(String str)
  2. md5(String str)
  3. md5(String str)
  4. md5(String str)
  5. MD5(String str)
  6. md5(String str)
  7. md5(String str)
  8. md5(String str)
  9. MD5(String str)