import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class HmacSHA256Util {
public static byte[] hmacSHA256(byte[] key,byte[] content) throws Exception {
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
hmacSha256.init(new SecretKeySpec(key, 0, key.length, "HmacSHA256"));
byte[] hmacSha256Bytes = hmacSha256.doFinal(content);
return hmacSha256Bytes;
}
public static String byteArrayToHexString(byte[] b) {
StringBuilder hs = new StringBuilder();
String stmp;
for (int n = 0; b!=null && n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1)
hs.append('0');
hs.append(stmp);
}
return hs.toString().toLowerCase();
}
public static String hmacSHA256(String secret, String message) throws Exception {
String hash = "";
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
hmacSha256.init(secret_key);
byte[] bytes = hmacSha256.doFinal(message.getBytes());
hash = byteArrayToHexString(bytes);
return hash;
}
public static void main(String[] args) {
String appSecret = "b06c75b58d1701ff470119a4114f8b45";
String appId = "10000001";
String timestamp = "1529853639000";
String message = appId + timestamp + "<req><orderId>91303183862452</orderId><notifyUrl></notifyUrl><refundTime>2013-03-22 16:25:26</refundTime><refundFee>24.5</refundFee><refundNo>14252414245</refundNo></req>";
System.out.println("加密前 =========>"+message);
String str = "";
try{
str = HmacSHA256Util.hmacSHA256(appSecret,message);
System.out.println("加密后 =========>"+str);
}catch (Exception e){
System.out.println("Error HmacSHA256 ===========>" + e.getMessage());
}
}
}