Java MD5 String md5(String text)

Here you can find the source of md5(String text)

Description

md

License

Open Source License

Declaration

public static String md5(String text) 

Method Source Code

//package com.java2s;
/**/*from w  w w .  j  a  v a  2 s  .c  om*/
 *  Copyright (c) 2011, StringUtil.java TAIHEIOT and/or its affiliates. All rights reserved.
 *
 *  Licensed under the TAIHEIOT License, Version 1.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.security.MessageDigest;

public class Main {

    public static String md5(String text) {
        if (isNullOrEmpty(text))
            return "";
        StringBuffer hexString = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(text.getBytes());
            byte[] digest = md.digest();
            for (int i = 0; i < digest.length; i++) {
                text = Integer.toHexString(0xFF & digest[i]);
                if (text.length() < 2) {
                    text = "0" + text;
                }
                hexString.append(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hexString.toString();
    }

    public static boolean isNullOrEmpty(String string) {
        if (string == null)
            return true;
        else if (string.trim().isEmpty())
            return true;
        return false;
    }
}

Related

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