Java MD5 Encode md5Encode(String message)

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

Description

md Encode

License

Apache License

Declaration

public static String md5Encode(String message) 

Method Source Code

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

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static final boolean toUpperCase = true;

    public static String md5Encode(String message) {
        return encode("MD5", message);
    }//from   w  w w  . j av  a 2  s.c  o  m

    private static String encode(String code, String message) {
        return encode(code, message.getBytes());
    }

    private static String encode(String code, byte[] message) {
        MessageDigest md;
        String encode = null;
        try {
            md = MessageDigest.getInstance(code);
            encode = byteArrayToHexString(md.digest(message));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return encode;
    }

    private static String byteArrayToHexString(byte[] digest) {
        StringBuffer md5StrBuff = new StringBuffer();

        for (int i = 0; i < digest.length; i++) {
            if (Integer.toHexString(0xFF & digest[i]).length() == 1) {
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & digest[i]));
            } else {
                String hex = Integer.toHexString(0xFF & digest[i]);
                if (toUpperCase) {
                    hex = hex.toUpperCase();
                }
                md5StrBuff.append(hex);
            }
        }
        return md5StrBuff.toString();
    }
}

Related

  1. MD5Encode(final String origin, final String charsetname)
  2. md5Encode(String input, String encoding)
  3. md5Encode(String inStr)
  4. md5Encode(String inStr)
  5. MD5Encode(String inStr)
  6. MD5Encode(String origin)
  7. MD5Encode(String origin)
  8. MD5Encode(String s)
  9. Md5Encode(String source)