package com.ibeetl.admin.core.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.math.BigInteger; import java.security.MessageDigest; public class MD5Util { static Logger log = LoggerFactory.getLogger(MD5Util.class); /** * 字符串的MD5 * @param plainText * @return */ public static String encode(String plainText) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(plainText.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0){ i += 256; } if (i < 16){ buf.append("0"); } buf.append(Integer.toHexString(i)); } return buf.toString(); //System.out.println("result: " + buf.toString());// 32位的加密 //System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; } /** * 文件的MD5 * @param filePath * @return */ public static String getFileMD5(String filePath){ String value = null; FileInputStream in = null; try { File file = new File(filePath); in = new FileInputStream(file); MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[8192]; int c; while ((c = in.read(buffer)) != -1) { md5.update(buffer, 0, c); } BigInteger bi = new BigInteger(1, md5.digest()); value = bi.toString(16).toUpperCase(); } catch (Exception e) { log.error("{}", e); } finally { if (null != in) { try { in.close(); } catch (IOException e) { log.error("{}", e); } } } return value; } public static String wxPaySign(String origin, String charsetname) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); if (charsetname == null || "".equals(charsetname)) { resultString = byteArrayToHexString(md.digest(resultString.getBytes())); }else { resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname))); } } catch (Exception exception) { exception.printStackTrace(); } return resultString; } private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.append(byteToHexString(b[i])); return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) { n += 256; } int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; public static void main(String args[]) { String str = "abc"; Long b1 = System.currentTimeMillis(); for(int i =0;i<1;i++){ //MD5.encode(str); System.out.println(MD5Util.encode(str)); //900150983cd24fb0d6963f7d28e17f72 // Md5PasswordEncoder md5 = new Md5PasswordEncoder(); // //md5.setEncodeHashAsBase64(false); // //md5.encodePassword(str, ""); // System.out.println(md5.encodePassword(str, "")); } Long e1 = System.currentTimeMillis(); System.out.println("MD5.encode耗时:"+(e1-b1)); } }