Java MD5 String md5(String input)

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

Description

Create an MD5 Hash of an input String.

License

Open Source License

Parameter

Parameter Description
input String to Hash

Return

Hash (Hex-Encoded)

Declaration

public static String md5(String input) 

Method Source Code

//package com.java2s;
/*// w  w w.  ja v a  2  s.com
 * regain/Thumbnailer - A file search engine providing plenty of formats (Plugin)
 * Copyright (C) 2011  Come_IN Computerclubs (University of Siegen)
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: Come_IN-Team <come_in-team@listserv.uni-siegen.de>
 */

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**
     * Create an MD5 Hash of an input String.
     * Uses the MD5 Algorithm of MessageDigest.
     * @param input   String to Hash
     * @return   Hash (Hex-Encoded)
     */
    public static String md5(String input) {
        StringBuilder res = new StringBuilder();
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(input.getBytes());
            byte[] md5 = algorithm.digest();
            String tmp = "";
            for (int i = 0; i < md5.length; i++) {
                tmp = (Integer.toHexString(0xFF & md5[i]));
                if (tmp.length() == 1) {
                    res.append("0").append(tmp);
                } else {
                    res.append(tmp);
                }
            }
        } catch (NoSuchAlgorithmException ex) {
            return "";
        }
        return res.toString();
    }
}

Related

  1. md5(String data)
  2. md5(String data)
  3. md5(String data)
  4. md5(String encryptStr)
  5. MD5(String input)
  6. md5(String input)
  7. md5(String input)
  8. md5(String input)
  9. md5(String input)