public class MD5Util { public static String getMD5(String str) { try { // Create MD5 Hash MessageDigest md5 = java.security.MessageDigest.getInstance("MD5"); md5.update(str.getBytes()); byte messageDigest[] = md5.digest(); String resultArray = byteArrayToHex(messageDigest); return resultArray; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } public static String byteArrayToHex(byte[] byteArray) { char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' }; char[] resultCharArray =new char[byteArray.length * 2]; int index = 0; for (byte b : byteArray) { resultCharArray[index++] = hexDigits[b>>>4 & 0xf]; resultCharArray[index++] = hexDigits[b & 0xf]; } return new String(resultCharArray); } }
相关文章