package com.demo.lib.common.util.security; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import android.util.Log; public class MD5Utils { private static final String TAG = "MD5Util"; public static String getMD532(String key) { String cacheKey = ""; try { final MessageDigest mDigest = MessageDigest.getInstance("MD5"); mDigest.update(key.getBytes()); cacheKey = bytesToHexString(mDigest.digest()); } catch (NoSuchAlgorithmException e) { Log.e(TAG, e.getMessage()); } return cacheKey; } private static String bytesToHexString(byte[] bytes) { // http://stackoverflow.com/questions/332079 StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(0xFF & bytes[i]); if (hex.length() == 1) { sb.append('0'); } sb.append(hex); } return sb.toString(); } }