<span style="font-family:Comic Sans MS;font-size:12px;">package com.sica.hmac;
import com.google.common.base.Strings;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
/**
* Created by xiang.li on 2015/2/27.
*/
public class HMAC {
/**
* 定義加密方式
* MAC算法可選以下多種算法
* <pre>
* HmacMD5
* HmacSHA1
* HmacSHA256
* HmacSHA384
* HmacSHA512
* </pre>
*/
private final static String KEY_MAC = "HmacMD5";
/**
* 全局?jǐn)?shù)組
*/
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
/**
* 構(gòu)造函數(shù)
*/
public HMAC() {
}
/**
* BASE64 加密
* @param key 需要加密的字節(jié)數(shù)組
* @return 字符串
* @throws Exception
*/
public static String encryptBase64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* BASE64 解密
* @param key 需要解密的字符串
* @return 字節(jié)數(shù)組
* @throws Exception
*/
public static byte[] decryptBase64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
/**
* 初始化HMAC密鑰
* @return
*/
public static String init() {
SecretKey key;
String str = "";
try {
KeyGenerator generator = KeyGenerator.getInstance(KEY_MAC);
key = generator.generateKey();
str = encryptBase64(key.getEncoded());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
/**
* HMAC加密
* @param data 需要加密的字節(jié)數(shù)組
* @param key 密鑰
* @return 字節(jié)數(shù)組
*/
public static byte[] encryptHMAC(byte[] data, String key) {
SecretKey secretKey;
byte[] bytes = null;
try {
secretKey = new SecretKeySpec(decryptBase64(key), KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
bytes = mac.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
return bytes;
}
/**
* HMAC加密
* @param data 需要加密的字符串
* @param key 密鑰
* @return 字符串
*/
public static String encryptHMAC(String data, String key) {
if (Strings.isNullOrEmpty(data)) {
return null;
}
byte[] bytes = encryptHMAC(data.getBytes(), key);
return byteArrayToHexString(bytes);
}
/**
* 將一個(gè)字節(jié)轉(zhuǎn)化成十六進(jìn)制形式的字符串
* @param b 字節(jié)數(shù)組
* @return 字符串
*/
private static String byteToHexString(byte b) {
int ret = b;
//System.out.println("ret = " + ret);
if (ret < 0) {
ret += 256;
}
int m = ret / 16;
int n = ret % 16;
return hexDigits[m] + hexDigits[n];
}
/**
* 轉(zhuǎn)換字節(jié)數(shù)組為十六進(jìn)制字符串
* @param bytes 字節(jié)數(shù)組
* @return 十六進(jìn)制字符串
*/
private static String byteArrayToHexString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
sb.append(byteToHexString(bytes[i]));
}
return sb.toString();
}
/**
* 測(cè)試方法
* @param args
*/
public static void main(String[] args) throws Exception {
String key = HMAC.init();
System.out.println("Mac密鑰:\n" + key);
String word = "123";
System.out.println(encryptHMAC(word, key));
}
}</span>