Java MD5 String md5(Object content)

Here you can find the source of md5(Object content)

Description

md

License

Apache License

Declaration

public static String md5(Object content) 

Method Source Code

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

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

public class Main {
    private static final String CARTSET_UTF_8 = "UTF-8";
    private static final String MD5_NAME = "MD5";

    public static String md5(Object content) {
        String keys = null;//from   ww w.j av a 2s.co m
        if (content == null) {
            return null;
        }
        try {
            MessageDigest md = MessageDigest.getInstance(MD5_NAME);
            byte[] bPass = String.valueOf(content).getBytes(CARTSET_UTF_8);
            md.update(bPass);
            keys = bytesToHexString(md.digest());
        } catch (NoSuchAlgorithmException aex) {
            System.out.println(aex);
        } catch (java.io.UnsupportedEncodingException uex) {
            System.out.println(uex);
        }
        return keys.toLowerCase();
    }

    private static String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
}

Related

  1. md5(final String string)
  2. md5(final String text)
  3. md5(InputStream input)
  4. md5(InputStream is)
  5. md5(java.lang.String message)
  6. md5(Path path)
  7. md5(String content)
  8. md5(String content)
  9. md5(String content)