Java MD5/SHA/MAC消息摘要算法

消息摘要算法

MD(Message Digest)

SHA(Secure Hash Algorithm)

MAC(Message Authentication Code)

其他如:RipeMD, Tiger, Whirlpool, GOST3411等,均由Bouncy Castle实现。

消息摘要算法–MD(Message Digest)

作用:验证数据的完整性

是数字签名核心算法

MD5

MD家族(128位摘要信息)-MD2,MD4

算法 摘要长度 实现方法
MD2 128 JDK
MD4 128 Bouncy Castle
MD5 128 JDK

代码实现

package md.test;

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

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD2Digest;
import org.bouncycastle.crypto.digests.MD4Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * Message Digest
 * @author Capricorncd
 * https://github.com/capricorncd
 *
 */
public class TestMessageDigest {
   

	private static final String MD2 = "MD2";
	private static final String MD4 = "MD4";
	private static final String MD5 = "MD5";

	public static void main(String[] args) {
   
		String src = "I love the world.";

		jdkMD(src, MD2);
		jdkMD(src, MD4);
		jdkMD(src, MD5);

		bcMD(src, MD2, new MD2Digest());
		bcMD(src, MD4, new MD4Digest());
		bcMD(src, MD5, new MD5Digest());

		bouncyCastleMD(src, MD2);
		bouncyCastleMD(src, MD4);
		bouncyCastleMD(src, MD5);

		ccMD(src, MD2);
		ccMD(src, MD4);
		ccMD(src, MD5);
	}

	/**
	 * JDK MD
	 * @param src source
	 * @param mdType MD2,MD5
	 * @return String result
	 */
	public static String jdkMD(String src, String mdType) {
   
		String result = null;
		try {
   
			MessageDigest md = MessageDigest.getInstance(mdType);
			byte[] mdBytes = md.digest(src.getBytes());
			// toHex
			result = Hex.encodeHexString(mdBytes);
		} catch (NoSuchAlgorithmException e) {
   
			// e.printStackTrace();
			result = e.getMessage();
		}
		println(mdType + ":", result, "by JDK");
		return result;
	}

	/**
	 * bouncy castle MD
	 * @param src source
	 * @param mdType message digest type
	 * @param digest org.bouncycastle.crypto.Digest
	 * @return String result
	 */
	public static<T extends Digest> String bcMD(String src, String mdType, T digest) {
   
		byte[] srcBytes = src.getBytes();
		digest.update(srcBytes, 0, srcBytes.length);
		byte[] resBytes = new byte[digest.getDigestSize()];
		digest.doFinal(resBytes, 0);
		String result = org.bouncycastle.util.encoders.Hex.toHexString(resBytes);
		println(mdType + ":", result, "by Bouncycastle");
		return result;
	}

	/**
	 * bouncy castle provider MD
	 * @param src source
	 * @param mdType message digest type
	 * @return String result
	 */
	public static String bouncyCastleMD(String src, String mdType) {
   
		String result = null;
		Security.addProvider(new BouncyCastleProvider()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值