Java MD5 Encode md5Encode(String source)

Here you can find the source of md5Encode(String source)

Description

Get MD5 Encoding

License

Open Source License

Parameter

Parameter Description
source String

Return

String

Declaration

public static String md5Encode(String source) 

Method Source Code


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

public class Main {
    /**//from  w ww  . j a v a  2 s. c om
     * Get MD5 Encoding
     *
     * @param source {@link String}
     * @return String
     */
    public static String md5Encode(String source) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(source.getBytes("UTF-8"));
            return getHexString(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Convert byte array to hex string
     *
     * @param bytes byte
     * @return String
     */
    public static String getHexString(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            String hex = Integer.toHexString(0x00FF & b);
            if (hex.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(hex);
        }
        return stringBuffer.toString();
    }
}

Related

  1. md5Encode(String inStr)
  2. md5Encode(String message)
  3. MD5Encode(String origin)
  4. MD5Encode(String origin)
  5. MD5Encode(String s)
  6. Md5Encode(String source)
  7. MD5Encode(String sourceString)
  8. md5Encode(String src)
  9. MD5Encode(String str)