Java MD5 Sum md5sum(String str)

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

Description

mdsum

License

Open Source License

Declaration

public static String md5sum(String str) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static String md5sum(String str) {
        try {/*w ww.  j  a v  a2 s  . c om*/
            return md5sum(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException uee) {
            return null;
        }
    }

    private static String md5sum(byte[] bytes) {
        String ret = null;
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] byteDigest = md.digest(bytes);
            ret = byteToString(byteDigest);
        } catch (NoSuchAlgorithmException nsae) {
            nsae.printStackTrace();
        }
        return ret;
    }

    private static String byteToString(byte[] digest) {
        String tmpStr = "";
        StringBuffer strBuf = new StringBuffer(40);
        for (int i = 0; i < digest.length; i++) {
            tmpStr = (Integer.toHexString(digest[i] & 0xff));
            if (tmpStr.length() == 1) {
                strBuf.append("0" + tmpStr);
            } else {
                strBuf.append(tmpStr);
            }
        }
        return strBuf.toString();
    }
}

Related

  1. md5sum(String data)
  2. md5sum(String input)
  3. md5sum(String inString)
  4. md5sum(String message)
  5. md5Sum(String msg)
  6. md5sum(String string)
  7. md5sum(String url)
  8. md5Summary(String str)