JDK自带MD5加密算法

其实jdk就自带了md5加密算法,直接调用很方便

public static String md5(String source) {
		
	StringBuffer sb = new StringBuffer(32);
		
	try {
		MessageDigest md 	= MessageDigest.getInstance("MD5");
		byte[] array 		= md.digest(source.getBytes("utf-8"));
			
		for (int i = 0; i < array.length; i++) {
			sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3));
		}
	} catch (Exception e) {
		logger.error("Can not encode the string '" + source + "' to MD5!", e);
		return null;
	}
		
	return sb.toString();
}


你可能感兴趣的:(java学习)