Java MD5 Hash md5Hash(String input)

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

Description

md Hash

License

Open Source License

Declaration

public static String md5Hash(String input) 

Method Source Code

//package com.java2s;
/*//w ww. jav  a 2  s . c  om
 * Copyright (c) 2016. Sten Martinez
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

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

public class Main {
    private static MessageDigest _md5Digest = null;

    public static String md5Hash(String input) {
        if (_md5Digest == null) {
            try {
                _md5Digest = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                // this will not fail.
                e.printStackTrace();
                return null;
            }
        }

        byte[] digest = _md5Digest.digest(input.getBytes());

        StringBuilder sb = new StringBuilder();

        for (byte b : digest) {
            sb.append(String.format("%02x", b));
        }

        return sb.toString();
    }
}

Related

  1. md5Hash(byte[] key)
  2. md5Hash(String algorithm, String input)
  3. md5Hash(String buf)
  4. md5Hash(String content)
  5. md5Hash(String in)
  6. md5hash(String key)
  7. md5hash(String pass)
  8. md5Hash(String source)
  9. md5Hash(String text)