Java MD5 digestMd5(String plain)

Here you can find the source of digestMd5(String plain)

Description

digest Md

License

Open Source License

Parameter

Parameter Description
plain plain text

Exception

Parameter Description
RuntimeException if MD5 algorithm does not exist

Return

MD5 hash (in hex)

Declaration

public static final String digestMd5(String plain) 

Method Source Code

//package com.java2s;
/**//from   www.  j  av  a  2  s .co m
 * Heatmap Framework - Core
 *
 * Copyright (C) 2013   Martin Becker
 *                   becker@informatik.uni-wuerzburg.de
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 */

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

public class Main {
    /**
     * @param plain plain text
     * 
     * @return MD5 hash (in hex)
     * 
     * @throws RuntimeException if MD5 algorithm does not exist
     */
    public static final String digestMd5(String plain) {
        try {
            return digest(plain, "MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param plain plain text
     * @param algorithm digest algorithm
     * 
     * @return digest in hex according to the given algorithm
     * 
     * @throws NoSuchAlgorithmException if the digest algorithm does not exist
     */
    public static String digest(String plain, String algorithm) throws NoSuchAlgorithmException {

        MessageDigest messageDigest;
        messageDigest = MessageDigest.getInstance(algorithm);

        byte[] bytes = toByteArray(plain);
        byte[] digestedBytes = messageDigest.digest(bytes);

        String hex = toHexString(digestedBytes);

        return hex;
    }

    /**
     * @param plain plain text
     * 
     * @return byte representation of the text using UTF-8 character set
     * 
     * @throws RuntimeException if encoding UTF-8 is not supported
     */
    public static final byte[] toByteArray(String plain) {
        try {
            return plain.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param bytes byte array
     * 
     * @return hex representation
     */
    public static final String toHexString(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : bytes) {

            String part = Integer.toHexString((int) (b & 0xff));

            if (part.length() < 2) {
                stringBuffer.append(0);
            }

            stringBuffer.append(part);
        }
        return stringBuffer.toString();
    }
}

Related

  1. digestMD5(byte[] data)
  2. digestMd5(final String value)
  3. digestMD5(String buffer, String key)
  4. digestMD5(String data)
  5. digestMD5(String login, String pass)
  6. digestMD5(String text)
  7. encryptmd5(String str)
  8. generateMD5ByContent(String content)
  9. getFileMD5(String filePath)