Java MD5 String MD5(String message)

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

Description

MD

License

Open Source License

Declaration

public static String MD5(String message) 

Method Source Code


//package com.java2s;
/*//  ww  w  .  j a v  a  2 s .  c o  m
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Peter Baumann / rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 */

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

public class Main {
    public static String MD5(String message) {

        MessageDigest messageDigest = null;

        try {
            messageDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 encryption not available.", e);
        }
        byte[] messageBytes;
        try {
            messageBytes = message.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8 encoding is not supported.", e);
        }
        byte[] hash = messageDigest.digest(messageBytes);

        StringBuilder stringHash = new StringBuilder(2 * hash.length);
        for (byte b : hash) {
            stringHash.append(String.format("%02x", b));
        }

        return stringHash.toString();
    }
}

Related

  1. md5(String key)
  2. md5(String md5)
  3. MD5(String md5)
  4. md5(String message)
  5. md5(String message)
  6. md5(String message)
  7. MD5(String message)
  8. md5(String orgin)
  9. md5(String pass)