Java MD5 String md5(java.lang.String message)

Here you can find the source of md5(java.lang.String message)

Description

Creates an MD5 digest from the message.

License

Apache License

Parameter

Parameter Description
message not null

Return

MD5 digest, not null

Declaration

public static String md5(java.lang.String message) 

Method Source Code

//package com.java2s;
/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF 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 {
    /**/* w  w w  .  j  a v a  2  s.c  om*/
     * Creates an MD5 digest from the message.
     * Note that this implementation is unsalted.
     * @param message not null
     * @return MD5 digest, not null
     */
    public static String md5(java.lang.String message) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            StringBuilder sb = new StringBuilder();
            byte buf[] = message.getBytes();
            byte[] md5 = md.digest(buf);
            //System.out.println(message);
            for (byte aMd5 : md5) {
                String tmpStr = "0" + Integer.toHexString((0xff & aMd5));
                sb.append(tmpStr.substring(tmpStr.length() - 2));
            }
            return sb.toString();

        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }
}

Related

  1. md5(final String string)
  2. md5(final String string)
  3. md5(final String text)
  4. md5(InputStream input)
  5. md5(InputStream is)
  6. md5(Object content)
  7. md5(Path path)
  8. md5(String content)
  9. md5(String content)