package com.tu.Security;

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

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

/**
 * MD系列消息摘要算法
 * 
 * 算法:
 * MD2  实现方:JDK
 * MD4  实现方:BC
 * MD5  实现方:JDK
 * 
 * @author comtu
 */
public class Md {
	public static String str;
	public static final String MD5_TYPE_16 = "t_16";
	public static final String MD5_TYPE_32 = "T_32";

	public static String md5s(String plainText, String type) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();

			if (type.equals(MD5_TYPE_16)) {
				str = str.substring(8, 24);
			}

			return str;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String agrs[]) {
		System.out.println("MD5_TYPE_16:" + Md.md5s("comtu", MD5_TYPE_16));
		System.out.println("MD5_TYPE_32:" + Md.md5s("comtu", MD5_TYPE_32));

		jdkMD2("comtu");
		jdkMD5("comtu");
		bcMD4("comtu");
		bcMD5("comtu");
		ccMD2("comtu");
		ccMD5("comtu");

	}

	public static void jdkMD2(String msg) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD2");
			byte[] md2Bytes = md.digest(msg.getBytes());
			System.out.println("JDK MD2:" + org.apache.commons.codec.binary.Hex.encodeHexString(md2Bytes));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void jdkMD5(String msg) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] md5Bytes = md.digest(msg.getBytes());
			System.out.println("JDK MD5:" + org.apache.commons.codec.binary.Hex.encodeHexString(md5Bytes));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// BouncyCastle
	public static void bcMD4(String msg) {
		try {
			Security.addProvider(new BouncyCastleProvider());// 为JDK设置算法
			MessageDigest md = MessageDigest.getInstance("MD4");
			byte[] md4Bytes = md.digest(msg.getBytes());
			System.out.println("JDK MD5:" + org.apache.commons.codec.binary.Hex.encodeHexString(md4Bytes));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		Digest digest = new MD4Digest();
		digest.update(msg.getBytes(), 0, msg.getBytes().length);
		byte[] md4Byte = new byte[digest.getDigestSize()];
		digest.doFinal(md4Byte, 0);
		System.out.println("BC MD4: " + org.bouncycastle.util.encoders.Hex.toHexString(md4Byte));
	}

	// BouncyCastle
	public static void bcMD5(String msg) {
		Digest digest = new MD5Digest();
		digest.update(msg.getBytes(), 0, msg.getBytes().length);
		byte[] md5Byte = new byte[digest.getDigestSize()];
		digest.doFinal(md5Byte, 0);
		System.out.println("BC MD5: " + org.bouncycastle.util.encoders.Hex.toHexString(md5Byte));
	}
	
	//commons codec
	public static void ccMD5(String msg){
		System.out.println("CC MD5:"+DigestUtils.md5Hex(msg.getBytes()));
	}
	//commons codec
	public static void ccMD2(String msg){
		System.out.println("CC MD2:"+DigestUtils.md2Hex(msg.getBytes()));
	}

}








































