Java基礎加密之HMAC加密

MAC算法結合了MD5和SHA算法的優點,並加入密鑰的支持,是一種更爲安全的消息摘要算法。java


MAC(Message Authentication Code,消息認證碼算法)是含有密鑰的散列函數算法,兼容了MD和SHA算法的特性,並在此基礎上加入了密鑰。日次,咱們也常把MAC稱爲HMAC(keyed-Hash Message Authentication Code)。算法


MAC算法主要集合了MD和SHA兩大系列消息摘要算法。MD系列的算法有HmacMD二、HmacMD四、HmacMD5三種算法;SHA系列的算法有HmacSHA一、HmacSHA22四、HmacSHA25六、HmacSHA384.HmacSHA512五種算法。安全


通過MAC算法獲得的摘要值也能夠使用十六進制編碼表示,其摘要值長度與參與實現的摘要值長度相同。例如,HmacSHA1算法獲得的摘要長度就是SHA1算法獲得的摘要長度,都是160位二進制碼,換算成十六進制編碼爲40位。ide


wKioL1d2D4OBkwzaAADpjp-U7Ts483.png

package com.soap.util;

import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;

/**
 * Hash Message Authentication Code,散列消息鑑別碼 基於密鑰的Hash算法的認證協議。
 * 
 * @author Roger
 */
public class HMACUtil {

	/**
	 * 構建HmacMD5密鑰
	 * 
	 * @return 
	 * @throws Exception 
	 */
	public static byte[] initHmacMD5Key() throws Exception {
		// 初始化HmacMD5摘要算法的密鑰產生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacMD5");
		// 產生密鑰
		SecretKey secretKey = generator.generateKey();
		// 得到密鑰
		byte[] key = secretKey.getEncoded();
		return key;
	}
	

	/**
	 * 生成32位HmacMD5加密數據
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encodeHmacMD5(byte[] data, byte[] key)
			throws Exception {
		// 還原密鑰
		SecretKey secretKey = new SecretKeySpec(key, "HmacMD5");
		// 實例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 執行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 轉爲十六進制的字符串
	}

	/**
	 * 構建HmacSHA1密鑰
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] initHmacSHAKey() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密鑰產生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA1");
		// 產生密鑰
		SecretKey secretKey = generator.generateKey();
		// 得到密鑰
		byte[] key = secretKey.getEncoded();
		return key;
	}

	/**
	 * 生成40位HmacSHA1加密數據
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encodeHmacSHA(byte[] data, byte[] key)
			throws Exception {
		// 還原密鑰
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA1");
		// 實例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 執行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 轉爲十六進制的字符串
	}

	/**
	 * 構建HmacSHA256密鑰
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] initHmacSHA256Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密鑰產生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA256");
		// 產生密鑰
		SecretKey secretKey = generator.generateKey();
		// 得到密鑰
		byte[] key = secretKey.getEncoded();
		return key;
	}

	/**
	 * 生成64位HmacSHA256加密數據
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encodeHmacSHA256(byte[] data, byte[] key)
			throws Exception {
		// 還原密鑰
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA256");
		// 實例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 執行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 轉爲十六進制的字符串
	}

	/**
	 * 構建HmacSHA384密鑰
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] initHmacSHA384Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密鑰產生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA384");
		// 產生密鑰
		SecretKey secretKey = generator.generateKey();
		// 得到密鑰
		byte[] key = secretKey.getEncoded();
		return key;
	}

	/**
	 * 生成96位HmacSHA384加密數據
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encodeHmacSHA384(byte[] data, byte[] key)
			throws Exception {
		// 還原密鑰
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA384");
		// 實例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 執行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 轉爲十六進制的字符串
	}

	/**
	 * 構建HmacSHA512密鑰
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] initHmacSHA512Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密鑰產生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA512");
		// 產生密鑰
		SecretKey secretKey = generator.generateKey();
		// 得到密鑰
		byte[] key = secretKey.getEncoded();
		return key;
	}

	/**
	 * 生成128位HmacSHA512加密信息
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encodeHmacSHA512(byte[] data, byte[] key)
			throws Exception {
		// 還原密鑰
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");
		// 實例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 執行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 轉爲十六進制的字符串
	}

	public static void main(String[] args) throws Exception {
		String testString = "asdasd";

		byte[] keyHmacMD5 = initHmacMD5Key();
		System.out.println(encodeHmacMD5(testString.getBytes(), keyHmacMD5));
		System.out.println(encodeHmacMD5(testString.getBytes(), keyHmacMD5)
				.length());

		byte[] keyHmacSHA1 = initHmacSHAKey();
		System.out.println(encodeHmacSHA(testString.getBytes(), keyHmacSHA1));
		System.out.println(encodeHmacSHA(testString.getBytes(), keyHmacSHA1)
				.length());

		byte[] keyHmacSHA256 = initHmacSHA256Key();
		System.out.println(encodeHmacSHA256(testString.getBytes(),
				keyHmacSHA256));
		System.out.println(encodeHmacSHA256(testString.getBytes(),
				keyHmacSHA256).length());

		byte[] keyHmacSHA384 = initHmacSHA384Key();
		System.out.println(encodeHmacSHA384(testString.getBytes(),
				keyHmacSHA384));
		System.out.println(encodeHmacSHA384(testString.getBytes(),
				keyHmacSHA384).length());

		byte[] keyHmacSHA512 = initHmacSHA512Key();
		System.out.println(encodeHmacSHA512(testString.getBytes(),
				keyHmacSHA512));
		System.out.println(encodeHmacSHA512(testString.getBytes(),
				keyHmacSHA512).length());
	}
}
相關文章
相關標籤/搜索