手动实现md5加密

手动实现md5加密

public static String md5(String source) {
		
		if(source == null || source.isEmpty()) {
			
			//字符串不合法
			throw new RuntimeException("不能传入空字符串");
		}
		
		String algorithm = "md5";
		
		try {
			
			MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
			
			// 获取source对应的字符数组
			byte[] input = source.getBytes();
			
			// 进行加密
			byte[] output = messageDigest.digest(input);
			
			// 创建biginteger
			int signum = 1;
			BigInteger bigInteger = new BigInteger(signum, output);
			
			// 将biginteger转换为16进制字符串
			int radix = 16;
			String encoded = bigInteger.toString(radix).toUpperCase();
			
			return encoded;
			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
原文地址:https://www.cnblogs.com/katoMegumi/p/14074081.html