使用MD算法获取消息摘要

使用MD算法获取消息摘要

使用md2、md4、md5算法封装了一个获取消息摘要的工具类,如下:

package com.cd.springbootdemo.util;

import java.security.MessageDigest;
import java.security.Security;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

/**
 * 
 * @desc 使用MD算法获取消息摘要
 */
public class MdCoder {

	/**
	 * MD2消息摘要
	 * @param data 待做摘要处理的数据
	 * return byte[] 消息摘要 
	 * @throws Exception
	 */
	public static byte[] encodeMD2(byte[] data) throws Exception {
		MessageDigest md=MessageDigest.getInstance("MD2");
		return md.digest(data);
	}
	
	/**
	 * MD2消息摘要
	 * @param data 待做摘要处理的数据
	 * return byte[] 消息摘要 
	 * @throws Exception
	 */
	public static String encodeMD2Hex(byte[] data) throws Exception {
		//执行消息摘要
		byte[] b=encodeMD2(data);
		//做十六进制编码处理
		return new String(Hex.encode(b));
	}
	
	/**
	 * MD4消息摘要
	 * @param data 待做摘要处理的数据
	 * return byte[] 消息摘要 
	 * @throws Exception
	 */
	public static byte[] encodeMD4(byte[] data) throws Exception {
		//加入BouncyCastleProvider支持
		Security.addProvider(new BouncyCastleProvider());
		MessageDigest md=MessageDigest.getInstance("MD4");
		return md.digest(data);
	}
	
	/**
	 * MD4消息摘要
	 * @param data 待做摘要处理的数据
	 * return byte[] 消息摘要 
	 * @throws Exception
	 */
	public static String encodeMD4Hex(byte[] data) throws Exception {
		//执行消息摘要
		byte[] b=encodeMD4(data);
		//做十六进制编码处理
		return new String(Hex.encode(b));
	}
	
	
	/**
	 * MD5消息摘要
	 * @param data 待做摘要处理的数据
	 * return byte[] 消息摘要 
	 * @throws Exception
	 */
	public static byte[] encodeMD5(byte[] data) throws Exception {
		MessageDigest md=MessageDigest.getInstance("MD5");
		return md.digest(data);
	}
	
	/**
	 * MD5消息摘要
	 * @param data 待做摘要处理的数据
	 * return byte[] 消息摘要 
	 * @throws Exception
	 */
	public static String encodeMD5Hex(byte[] data) throws Exception {
		//执行消息摘要
		byte[] b=encodeMD5(data);
		//做十六进制编码处理
		return new String(Hex.encode(b));
	}

	public static void main(String[] args) throws Exception {
		
		String str="MDHex消息摘要";
		String data1=MdCoder.encodeMD2Hex(str.getBytes());
		String data2=MdCoder.encodeMD2Hex(str.getBytes());
		System.out.println("原文:\t"+str);
		System.out.println("MD2Hex-1:\t"+data1);
		System.out.println("MD2Hex-2:\t"+data2);
		
		str="MDHex消息摘要";
		data1=MdCoder.encodeMD4Hex(str.getBytes());
		data2=MdCoder.encodeMD4Hex(str.getBytes());
		System.out.println("\n原文:\t"+str);
		System.out.println("MD4Hex-1:\t"+data1);
		System.out.println("MD4Hex-2:\t"+data2);
		
		str="MDHex消息摘要";
		data1=MdCoder.encodeMD5Hex(str.getBytes());
		data2=MdCoder.encodeMD5Hex(str.getBytes());
		System.out.println("\n原文:\t"+str);
		System.out.println("MD5Hex-1:\t"+data1);
		System.out.println("MD5Hex-2:\t"+data2);
	}
}

测试效果:
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pillar~

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值