MD5/SHA-1/SHA-256
/** * MD5 */ public static final String MD5 = "MD5"; /** * SHA-1 */ public static final String SHA1 = "SHA-1"; /** * SHA-256 */ public static final String SHA256 = "SHA-256"; /** * * 对字符串根据摘要名进行摘要。可支持MD5,SHA-1,SHA-256,默认SHA-256 * * @param encName 摘要名 * @param strSrc 摘要目标字符串 * @return 摘要结果字符串 */ public static String encrypt(String encName, final String strSrc) { MessageDigest md = null; String strDes = null; byte[] bt = strSrc.getBytes(); try { if (encName == null || encName.equals("")) { encName = "SHA-256"; } md = MessageDigest.getInstance(encName); md.update(bt); // to HexString strDes = bytes2Hex(md.digest()); } catch (NoSuchAlgorithmException e) { return null; } return strDes; } /** * 将byte数组转为16进制表示的字符串 * * @param bts 源数组 * @return 16进制表示的字符串 */ public static String bytes2Hex(byte[] bts) { String des = ""; String tmp = null; for (int i = 0; i < bts.length; i++) { tmp = Integer.toHexString(bts[i] & 0xFF); if (tmp.length() == 1) { des += "0"; } des += tmp; } return des; }?