Java MD5 Encode MD5Encode(String inStr)

Here you can find the source of MD5Encode(String inStr)

Description

MD Encode

License

Artistic License

Declaration

public static String MD5Encode(String inStr) throws NoSuchAlgorithmException, UnsupportedEncodingException 

Method Source Code


//package com.java2s;
//License from project: Artistic License 

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {

    public static String MD5Encode(String inStr) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = getMessageDigest("MD5");
        return bytetoString(md.digest(inStr.getBytes("UTF-8")));
    }/*w  w  w .  jav a  2 s . co m*/

    private static MessageDigest getMessageDigest(String digestType) {
        try {
            return MessageDigest.getInstance(digestType);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("No such algorithm [" + digestType + "]");
        }
    }

    private static String bytetoString(byte[] digest) {
        StringBuffer sb = new StringBuffer();
        String tempStr;

        for (int i = 0; i < digest.length; i++) {
            tempStr = (Integer.toHexString(digest[i] & 0xff));
            if (tempStr.length() == 1) {
                sb.append("0" + tempStr);
            } else {
                sb.append(tempStr);
            }
        }
        return sb.toString().toLowerCase();
    }
}

Related

  1. md5Encode(byte[] content)
  2. MD5Encode(final String origin, final String charsetname)
  3. md5Encode(String input, String encoding)
  4. md5Encode(String inStr)
  5. md5Encode(String inStr)
  6. md5Encode(String message)
  7. MD5Encode(String origin)