Java MD5 String MD5(String str)

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

Description

MD

License

Artistic License

Declaration

public static String MD5(String str) 

Method Source Code


//package com.java2s;
//License from project: Artistic License 

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

public class Main {
    public static String MD5(String str) {
        return encrypt(str, "MD5");
    }// ww w  . j  av a2 s .  c  o m

    public static String encrypt(String str, String enc) {
        MessageDigest md = null;
        String result = null;
        byte[] bt = str.getBytes();
        try {
            md = MessageDigest.getInstance(enc);
            md.update(bt);
            result = bytes2Hex(md.digest());
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        return result;
    }

    private static String bytes2Hex(byte[] bts) {
        String des = "";
        String tmp = null;

        for (byte bt : bts) {
            tmp = (Integer.toHexString(bt & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }
}

Related

  1. md5(String src)
  2. md5(String src)
  3. md5(String srcStr)
  4. md5(String str)
  5. md5(String str)
  6. md5(String str)
  7. md5(String str)
  8. md5(String str)
  9. md5(String str)