package com.lzx.lock.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * Created by xian on 2017/2/17. */ public class StringUtil { public static String toMD5(String source) { if (null == source || "".equals(source)) return null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(source.getBytes()); return toHex(digest.digest()); } catch (NoSuchAlgorithmException e) { } return null; } public static String toHex(byte[] buf) { if (buf == null) return ""; StringBuffer result = new StringBuffer(2 * buf.length); for (int i = 0; i < buf.length; i++) { appendHex(result, buf[i]); } return result.toString(); } private final static String HEX = "0123456789ABCDEF"; private static void appendHex(StringBuffer sb, byte b) { sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f)); } }