String key = "ThisIsAKeyYouMakeIt"; //自己写一个key字符串(解码的时候也要用)
byte[] callURLByte = null;
try {
callURLByte = callURL.getBytes("UTF8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Mac mac;
SecretKey secretKey = new SecretKeySpec(key.getBytes(), "HmacMD5"); //传入的key需要编程byte数组
mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
byte[] result = mac.doFinal(callURLByte); //得到的结果同样是byte数组
StringBuilder sign = new StringBuilder();
// Integer.toHexString是得到16进制数
for (byte b : result) {
if (Integer.toHexString(0xFF & b).length() == 1) {
sign.append("0" + Integer.toHexString(0xFF & b)); //为MD5加密是32位的16进制,和0xFF做与运算,是为了让高24为补0
} else {
sign.append(Integer.toHexString(0xFF & b));
}
}
Hmac-md5加密
最新推荐文章于 2024-05-04 07:33:30 发布