使用java自带加密算法实现文本的md5加密算法详解大数据

本篇使用java自带的MessageDigest实现对文本的md5加密算法,具体代码如下:

 /**   
 [email protected]: 将字符串转化为MD5 
 */  
package cn.yicha.novel.util;   
 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
   
public class ParseMD5 { 
 
	/** 
	 * @param str 
	 * @return 
	 * @Date: 2013-9-6   
	 * @Author: lulei   
	 * @Description:  32位小写MD5 
	 */ 
	public static String parseStrToMd5L32(String str){ 
		String reStr = null; 
		try { 
			MessageDigest md5 = MessageDigest.getInstance("MD5"); 
			byte[] bytes = md5.digest(str.getBytes()); 
			StringBuffer stringBuffer = new StringBuffer(); 
			for (byte b : bytes){ 
				int bt = b&0xff; 
				if (bt < 16){ 
					stringBuffer.append(0); 
				}  
				stringBuffer.append(Integer.toHexString(bt)); 
			} 
			reStr = stringBuffer.toString(); 
		} catch (NoSuchAlgorithmException e) { 
			e.printStackTrace(); 
		} 
		return reStr; 
	} 
	 
	/** 
	 * @param str 
	 * @return 
	 * @Date: 2013-9-6   
	 * @Author: lulei   
	 * @Description: 32位大写MD5 
	 */ 
	public static String parseStrToMd5U32(String str){ 
		String reStr = parseStrToMd5L32(str); 
		if (reStr != null){ 
			reStr = reStr.toUpperCase(); 
		} 
		return reStr; 
	} 
	 
	/** 
	 * @param str 
	 * @return 
	 * @Date: 2013-9-6   
	 * @Author: lulei   
	 * @Description: 16位小写MD5 
	 */ 
	public static String parseStrToMd5U16(String str){ 
		String reStr = parseStrToMd5L32(str); 
		if (reStr != null){ 
			reStr = reStr.toUpperCase().substring(8, 24); 
		} 
		return reStr; 
	} 
	 
	/** 
	 * @param str 
	 * @return 
	 * @Date: 2013-9-6   
	 * @Author: lulei   
	 * @Description: 16位大写MD5 
	 */ 
	public static String parseStrToMd5L16(String str){ 
		String reStr = parseStrToMd5L32(str); 
		if (reStr != null){ 
			reStr = reStr.substring(8, 24); 
		} 
		return reStr; 
	} 
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/bigdata/8958.html

(0)
上一篇 2021年7月19日 09:02
下一篇 2021年7月19日 09:02

相关推荐

发表回复

登录后才能评论