HMacMD5 签名生成方式
一.工具类
原文地址: https://blog.csdn.net/chy2z/article/details/80828770>
package cn.gdmcmc.iovs.hamcmd5.hamacmd5;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HMacMD5 {
/**
* 计算参数的md5信息
*
* @param str
* 待处理的字节数组
* @return md5摘要信息
* @throws NoSuchAlgorithmException
*/
private static byte[] md5(byte[] str) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str);
return md.digest();
}
/**
* 将待加密数据data,通过密钥key,使用hmac-md5算法进行加密,然后返回加密结果。 参照rfc2104 HMAC算法介绍实现。
* @param key
* 密钥
* @param data
* 待加密数据
* @return 加密结果
* @throws NoSuchAlgorithmException
*/
public static byte[] getHmacMd5Bytes(byte[] key, byte[] data)
throws NoSuchAlgorithmException {
/*
* HmacMd5 calculation formula: H(K XOR opad, H(K XOR ipad, text))
* HmacMd5 计算公式:H(K XOR opad, H(K XOR ipad, text))
* H代表hash算法,本类中使用MD5算法,K代表密钥,text代表要加密的数据 ipad为0x36,opad为0x5C。
*/
int length = 64;
byte[] ipad = new byte[length];
byte[] opad = new byte[length];
for (int i = 0; i < 64; i++) {
ipad[i] = 0x36;
opad[i] = 0x5C;
}
byte[] actualKey = key; // Actual key.
byte[] keyArr = new