Java MD5 Sum md5sum(String message)

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

Description

Generates an MD5 sum for the given string.

License

Open Source License

Parameter

Parameter Description
message string to digested

Return

MD5 sum corresponding to the given text

Declaration

public static String md5sum(String message) 

Method Source Code


//package com.java2s;
/*//from   w  w  w .j  av  a2  s  .  c om
 * This file is part of cBioPortal.
 *
 * cBioPortal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static final String encodeAlgorithm = "MD5";
    public static final String characterEncoding = "UTF-8";
    public static final int md5Base = 16;

    /**
    * Generates an MD5 sum for the given string.
    * 
    * @param message   string to digested
    * @return         MD5 sum corresponding to the given text
    */
    public static String md5sum(String message) {
        MessageDigest md5;
        byte[] md5sumBytes;
        String sum = null;

        try {
            md5 = MessageDigest.getInstance(encodeAlgorithm);
            md5sumBytes = md5.digest(message.getBytes(characterEncoding));
            sum = (new BigInteger(1, md5sumBytes)).toString(md5Base);
        } catch (NoSuchAlgorithmException e) {
            sum = null;
        } catch (UnsupportedEncodingException e) {
            sum = null;
        }

        return sum;
    }
}

Related

  1. md5sum(InputStream file)
  2. md5sum(InputStream in)
  3. md5sum(String data)
  4. md5sum(String input)
  5. md5sum(String inString)
  6. md5Sum(String msg)
  7. md5sum(String str)
  8. md5sum(String string)
  9. md5sum(String url)