Java MD5 Encode MD5Encode(final String origin, final String charsetname)

Here you can find the source of MD5Encode(final String origin, final String charsetname)

Description

MD Encode

License

Apache License

Declaration

public static String MD5Encode(final String origin, final String charsetname) 

Method Source Code


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

import java.security.MessageDigest;

public class Main {
    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c",
            "d", "e", "f" };

    public static String MD5Encode(final String origin, final String charsetname) {
        String resultString = null;
        try {/*from   ww w  .j  a v a 2 s. com*/
            resultString = new String(origin);
            final MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
        } catch (final Exception exception) {
        }
        return resultString;
    }

    public static String MD5Encode(final String origin) {
        return MD5Encode(origin, "UTF-8");
    }

    private static String byteArrayToHexString(final byte b[]) {
        final StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++)
            resultSb.append(byteToHexString(b[i]));

        return resultSb.toString();
    }

    private static String byteToHexString(final byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        final int d1 = n / 16;
        final int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }
}

Related

  1. md5Encode(byte[] content)
  2. md5Encode(String input, String encoding)
  3. md5Encode(String inStr)
  4. md5Encode(String inStr)
  5. MD5Encode(String inStr)