Java MD5 String md5(String plainText)

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

Description

md

License

Apache License

Declaration

public static String md5(String plainText) 

Method Source Code

//package com.java2s;
/*/* w  ww  .j a  va 2s  .c  o m*/
 *  Copyright 2015 the original author or authors. 
 *  @https://github.com/scouter-project/scouter
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  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 plainText) {
        String md5Text = null;

        if (plainText != null) {
            try {
                byte[] byteArray = plainText.getBytes();
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.update(byteArray);

                byte[] md5Bytes = md5.digest();
                StringBuffer buf = new StringBuffer();

                for (int i = 0; i < md5Bytes.length; i++) {
                    if ((md5Bytes[i] & 0xff) < 0x10) {
                        buf.append("0");
                    }
                    buf.append(Long.toString(md5Bytes[i] & 0xff, 16));
                }

                md5Text = buf.toString();
            } catch (Throwable t) {
                return plainText;
            }
        }

        return md5Text;
    }
}

Related

  1. md5(String plain)
  2. Md5(String plainText)
  3. md5(String plainText)
  4. Md5(String plainText)
  5. md5(String plainText)
  6. MD5(String pwd)
  7. md5(String raw)
  8. md5(String s)
  9. md5(String s)