Java MD5 String md5(String s)

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

Description

md

License

Open Source License

Declaration

public static String md5(String s) 

Method Source Code


//package com.java2s;
/*/*w ww. j  ava 2  s  .  c  o  m*/
 * utils-java8
 * Copyright (C) 2014 Raffaele Ragni
 *
 * 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/>.
 */

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

public class Main {
    public static final String DIGEST_MD5 = "MD5";

    public static String md5(String s) {
        return digest(s, DIGEST_MD5);
    }

    public static String digest(String source, String algorythm) {
        if (source == null || source.length() == 0) {
            return source;
        }

        try {
            StringBuilder result = new StringBuilder();
            MessageDigest md = MessageDigest.getInstance(algorythm);
            byte[] bytes = md.digest(source.getBytes("UTF-8"));
            for (byte b : bytes) {
                String bb = "0" + Integer.toHexString(b);
                result.append(bb.substring(bb.length() - 2));
            }
            return result.toString();
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            return null;
        }
    }
}

Related

  1. Md5(String plainText)
  2. md5(String plainText)
  3. md5(String plainText)
  4. MD5(String pwd)
  5. md5(String raw)
  6. md5(String s)
  7. MD5(String s)
  8. md5(String s)
  9. md5(String s)