Java??4???????????????
?????loading ???????[ 2017/4/27 11:26:10 ] ?????????? Java
????4.HMAC
????HMAC(Hash Message Authentication Code???????????????????????Hash???????Э?顣???????????????????????ù????????????????????????????????????????????????????????????????????????????????????С??С????飬??MAC???????????????У???????????????????????????????м?????????
????java??????
package com.cn.???????;
/*
HMAC
HMAC(Hash Message Authentication Code???????????????????????Hash???????Э?顣
???????????????????????ù???????????????????????????????????????????????????????????????
?????????????????????С??С????飬
??MAC???????????????У???????????????????????????????м?????????*/
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import com.cn.comm.Tools;
/**
* ???????????
*/
public abstract class HMAC {
public static final String KEY_MAC = "HmacMD5";
/**
* ?????HMAC???
*
* @return
* @throws Exception
*/
public static String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
SecretKey secretKey = keyGenerator.generateKey();
return BASE64.encryptBASE64(secretKey.getEncoded());
}
/**
* HMAC???? ?????????
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static String encryptHMAC(byte[] data?? String key) throws Exception {
SecretKey secretKey = new SecretKeySpec(BASE64.decryptBASE64(key)?? KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return new String(mac.doFinal(data));
}
public static String getResult1(String inputStr)
{
String path=Tools.getClassPath();
String fileSource=path+"/file/HMAC_key.txt";
System.out.println("=======???????????:"+inputStr);
String result=null;
try {
byte[] inputData = inputStr.getBytes();
String key = HMAC.initMacKey(); /*???????*/
System.out.println("Mac???:===" + key);
/*?????д???*/
Tools.WriteMyFile(fileSource??key);
result= HMAC.encryptHMAC(inputData?? key);
System.out.println("HMAC?????:===" + result);
} catch (Exception e) {e.printStackTrace();}
return result.toString();
}
public static String getResult2(String inputStr)
{
System.out.println("=======???????????:"+inputStr);
String path=Tools.getClassPath();
String fileSource=path+"/file/HMAC_key.txt";
String key=null;;
try {
/*???????????ж??*/
key=Tools.ReadMyFile(fileSource);
System.out.println("getResult2???:===" + key);
} catch (Exception e1) {
e1.printStackTrace();}
String result=null;
try {
byte[] inputData = inputStr.getBytes();
/*????????м???*/
result= HMAC.encryptHMAC(inputData?? key);
System.out.println("HMAC?????:===" + result);
} catch (Exception e) {e.printStackTrace();}
return result.toString();
}
public static void main(String args[])
{
try {
String inputStr = "??????";
/*??????????????????м?????????μ????????????*/
getResult1(inputStr);
getResult2(inputStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
Java???????????Щ???????????????Java????????????????Java?б???Map????????Java Web???????????????Java??????????????д?????Java????????7???????????????????????(java .net ?????)???Java??????????Python??????Java webdriver??λ????????′????е?????Java??д??????????????????Java???????????????JavaScript????????????Java?????????????????? Java???????10??????????????Java?м????????????????java???????ü???????????м???????????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????