/**
 * 
 */
package com.manage.encrypt;

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

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA224Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.util.DigestUtils;

/**
 * <p>Title:MD_SHA_MAC</p>
 * <p>Description: 消息摘要</p>
 * <p>Company: </p> 
 * @author hu
 * @date
 */
public class MD_SHA_MAC {
	
	private static String src="security";
	
	public static void jdkMD5(){
		try {
			MessageDigest md=MessageDigest.getInstance("MD5");
			byte[] md5bytes=md.digest(src.getBytes());
			System.out.println("JDK md5:"+Hex.encodeHexString(md5bytes));			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}		
	}
	
	
	public static void jdkMD2(){
		try {
			MessageDigest md=MessageDigest.getInstance("MD2");
			byte[] md2bytes=md.digest(src.getBytes());
			System.out.println("JDK md2:"+Hex.encodeHexString(md2bytes));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
	}
	
	public static void jdkSHA1(){
		try {
			MessageDigest md=MessageDigest.getInstance("SHA");
			md.update(src.getBytes());
			System.out.println("jdk sha-1:"+Hex.encodeHexString(md.digest()));
			
		} catch (NoSuchAlgorithmException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	public static void bcMD4(){			
		try {
			//properties文件
			Security.addProvider(new BouncyCastleProvider());
			MessageDigest md=MessageDigest.getInstance("MD4");
			byte[] md4bytes=md.digest(src.getBytes());
			System.out.println("bc md4:"+Hex.encodeHexString(md4bytes));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		/*Digest digest=new MD4Digest();
		digest.update(src.getBytes(), 0, src.length());
		byte[] md4Bytes=new byte[digest.getDigestSize()];
		digest.doFinal(md4Bytes, 0);
		System.out.println("BC md4:"+Hex.encodeHexString(md4Bytes));	*/
		
	}
	
	public static void bcMD5(){	
		Digest digest=new MD5Digest();
		digest.update(src.getBytes(), 0, src.length());
		byte[] md5Bytes=new byte[digest.getDigestSize()];
		digest.doFinal(md5Bytes, 0);
		System.out.println("BC md5:"+Hex.encodeHexString(md5Bytes));	
		
	}
	
	public static void bcSHA1(){
		Digest digest=new SHA1Digest();
		digest.update(src.getBytes(),0,src.getBytes().length);
		byte[] sha1Bytes=new byte[digest.getDigestSize()];
		//参数（输入哪，偏移量0）
		digest.doFinal(sha1Bytes, 0);
		System.out.println("bc sha-1:"+Hex.encodeHexString(sha1Bytes));
	
	}
	
	public static void bcSHA224(){
		Digest digest=new SHA224Digest();
		digest.update(src.getBytes(),0,src.getBytes().length);
		byte[] sha224Bytes=new byte[digest.getDigestSize()];
		//参数（输入哪，偏移量0）
		digest.doFinal(sha224Bytes, 0);
		System.out.println("bc sha-224:"+Hex.encodeHexString(sha224Bytes));
	
	}
	
	public static void bcSHA224_2(){		
		try {
			Security.addProvider(new BouncyCastleProvider());
			MessageDigest md = MessageDigest.getInstance("SHA224");
			byte[] md224bytes=md.digest(src.getBytes());
			System.out.println("bc sha-224:"+Hex.encodeHexString(md224bytes));
		} catch (NoSuchAlgorithmException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
	
	public static void ccMD5(){			
		System.out.println("cc md5:"+DigestUtils.md5DigestAsHex(src.getBytes()));	
	}	
	
	public static void ccSHA1(){		
		//System.out.println("cc sha-1:"+DigestUtils.sha1Hex(src.g));	
 
    }
	
	
	//MAC
	public static void jdkHmacMD5(){
		try {
			//初始化KeyGenerator
			KeyGenerator keyGenerator=KeyGenerator.getInstance("HmacMD5");
			//产生密钥
			SecretKey secreKey=keyGenerator.generateKey();
			//获取密钥，自动生成
			//byte[] key=secreKey.getEncoded();			
			//获取密钥
			byte[] key=Hex.decodeHex(new char[]{'c','c','c','c','c','c','c','c','c','c'});
			
			//还原密钥
			SecretKey restoreSecretKey=new SecretKeySpec(key,"HmacMD5");
		    //实例化MAC
			Mac mac=Mac.getInstance(restoreSecretKey.getAlgorithm());
			//初始化MAC
			mac.init(restoreSecretKey);
			//执行消息摘要
			byte[] hmacMD5Bytes=mac.doFinal(src.getBytes());
			//System.out.println("jdk HmacMD5:"+Hex.encodeHexString(hmacMD5Bytes));
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}
	
	//MAC
		public static String bcHmacMD5(String src){
			HMac hmac=new HMac(new MD5Digest());
			hmac.init(new KeyParameter(org.bouncycastle.util.encoders.Hex.decode("cccccccccc")));
			hmac.update(src.getBytes(),0,src.getBytes().length);
		    //结果
			byte[] hmacMD5Bytes=new byte[hmac.getMacSize()];
			hmac.doFinal(hmacMD5Bytes, 0);
			//System.out.println("bc HmacMD5:"+Hex.encodeHexString(hmacMD5Bytes));
			return Hex.encodeHexString(hmacMD5Bytes);
		}
		
		
		
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		jdkMD5();
		jdkMD2();
		bcMD4();
		bcMD5();
	    ccMD5();
	    jdkSHA1();
	    bcSHA1();
	    bcSHA224();
	    bcSHA224_2();
	    jdkHmacMD5();
	    String ss=bcHmacMD5(src);
	    System.out.println(ss);
	}

}
