Java MD5 String md5(String content)

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

Description

md

License

Apache License

Declaration

public static String md5(String content) 

Method Source Code

//package com.java2s;
/*/*from w w w.  ja  v a  2s . co m*/
 * Copyright 2013 Rui Sun (SteveSunCanada@gmail.com)
 *
 * The Netty Project licenses this file to you 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;
import java.security.NoSuchAlgorithmException;

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

    public static String md5(String content) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");

            byte[] thedigest = md.digest(content.getBytes());
            char chars[] = new char[16 * 2];
            for (int i = 0; i < thedigest.length; i++) {
                chars[i * 2] = hexDigits[thedigest[i] >>> 4 & 0xf];
                chars[i * 2 + 1] = hexDigits[thedigest[i] & 0xf];
            }

            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. md5(InputStream input)
  2. md5(InputStream is)
  3. md5(java.lang.String message)
  4. md5(Object content)
  5. md5(Path path)
  6. md5(String content)
  7. md5(String content)
  8. md5(String d)
  9. md5(String data)