尚筹网:编写密码md5加密的工具包

代码

所在工程:atcrowdfunding-admin-3-common
全类名:com.atguigu.crowd.funding.util.CrowdFundingUtils

package com.atguigu.crowd.funding.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Map;

public class CrowdFundingUtils {
	
	/**
	 * 判断Map是否有效
	 * @param map 待验证的Map
	 * @return true表示有效,false表示无效
	 */
	public static <K,V> boolean mapEffective(Map<K, V> map) {
		
		return map != null && map.size() > 0;
	}
	
	/**
	 * 判断集合是否有效
	 * @param collection 待验证集合
	 * @return true表示有效,false表示无效
	 */
	public static <E> boolean collectionEffective(Collection<E> collection) {
		
		return collection != null && collection.size() > 0;
	}
	
	/**
	 * 判断字符串是否有效
	 * @param source 待验证字符串
	 * @return true表示有效,false表示无效
	 */
	public static boolean stringEffective(String source) {
		
		return source != null && source.length() > 0;
	}
	
	/**
	 * MD5加密工具方法
	 * @param source 明文
	 * @return	密文
	 */
	public static String md5(String source) {
		
		// 判断传入的明文字符串是否有效
		if(!stringEffective(source)) {
			
			// 如果检测到明文字符串无效,抛出异常通知方法的调用者
			throw new RuntimeException("明文不是有效字符串,请核对后再操作!");
		}
		
		// 声明StringBuilder待用
		StringBuilder builder = new StringBuilder();
		
		// 准备字符数组
		char[] characters = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
		
		// 指定加密算法
		String algorithm = "MD5";
		
		try {
			
			// 执行加密操作的核心对象
			MessageDigest digest = MessageDigest.getInstance(algorithm);
			
			// 将要加密的明文转换成字节数组形式
			byte[] inputBytes = source.getBytes();
			
			// 执行加密
			byte[] outputBytes = digest.digest(inputBytes);
			
			// 遍历outputBytes
			for (int i = 0; i < outputBytes.length; i++) {
				
				// 获取当前字节数值
				byte b = outputBytes[i];
				
				// 获取低四位值
				int lowValue = b & 15;
				
				// 右移四位和15做与运算得到高四位值
				int highValue = (b >> 4) & 15;
				
				// 以高四位、低四位的值为下标从字符数组中获取对应字符
				char highCharacter = characters[highValue];
				char lowCharacter = characters[lowValue];
				
				// 拼接
				builder.append(highCharacter).append(lowCharacter);
			}
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return builder.toString();
	}

}

测试

所在工程:atcrowdfunding-admin-3-common
所在文件:/atcrowdfunding-admin-3-common/src/test/java/com/atguigu/crowd/funding/utilTest/CrowdFundingUtilsTest.java

package com.atguigu.crowd.funding.utilTest;

import org.junit.Test;

import com.atguigu.crowd.funding.util.CrowdFundingUtils;

public class CrowdFundingUtilsTest {

	@Test
	public void testCrowdFundingUtilMd5() {
		// E10ADC3949BA59ABBE56E057F20F883E
		System.out.println(CrowdFundingUtils.md5("123456"));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值