HMAC-MD5算法
:K是密钥(OperatorSecret),长度可为64字节,若小于该长度,在密钥后面用“0”补齐。
2)HMAC-MD5流程
1) 在SigSecret)0来创建一个长为64字节的字符串(str);
2) (str)与ipad(0x36)做异或运算,形成结果字符串(istr);
3)将消息内容data附加到第二步的结果字符串(istr)的末尾;
4)做md5运算于第三步生成的数据流(istr);
5) (str)与opad(0x5c)做异或运算,形成结果字符串(ostr);
6)再将第四步的结果(istr)附加到第五步的结果字符串(ostr)的末尾;
7)做md5运算于第六步生成的数据流(ostr),输出最终结果(out)。
import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * @Title: HMacMD5Util * @Description: HMacMD5 * @author chy * @date 2018/6/11 15:32 */ public class HMacMD5Util { /** * 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(); } /** * datakeyhmac-md5rfc2104 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浠h〃hashMD5Ktext ipad为0x36opad为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 byte[length]; // Key bytes of 64 bytes length /* * If key's length is longer than 64,then use hash to digest it and use * the result as actual key. 64 */ if (key.length > length) { actualKey = md5(key); } for (int i = 0; i < actualKey.length; i++) { keyArr[i] = actualKey[i]; } /* * append zeros to K 640x0064 */ if (actualKey.length < length) { for (int i = actualKey.length; i < keyArr.length; i++) keyArr[i] = 0x00; } /* * calc K XOR ipad ipad */ byte[] kIpadXorResult = new byte[length]; for (int i = 0; i < length; i++) { kIpadXorResult[i] = (byte) (keyArr[i] ^ ipad[i]); } /* * append "text" to the end of "K XOR ipad" K XOR ipad */ byte[] firstAppendResult = new byte[kIpadXorResult.length + data.length]; for (int i = 0; i < kIpadXorResult.length; i++) { firstAppendResult[i] = kIpadXorResult[i]; } for (int i = 0; i < data.length; i++) { firstAppendResult[i + keyArr.length] = data[i]; } /* * calc H(K XOR ipad, text) */ byte[] firstHashResult = md5(firstAppendResult); /* * calc K XOR opad opad */ byte[] kOpadXorResult = new byte[length]; for (int i = 0; i < length; i++) { kOpadXorResult[i] = (byte) (keyArr[i] ^ opad[i]); } /* * append "H(K XOR ipad, text)" to the end of "K XOR opad" H(K XOR * ipad, text)K XOR opad */ byte[] secondAppendResult = new byte[kOpadXorResult.length + firstHashResult.length]; for (int i = 0; i < kOpadXorResult.length; i++) { secondAppendResult[i] = kOpadXorResult[i]; } for (int i = 0; i < firstHashResult.length; i++) { secondAppendResult[i + keyArr.length] = firstHashResult[i]; } /* * H(K XOR opad, H(K XOR ipad, text)) */ byte[] hmacMd5Bytes = md5(secondAppendResult); return hmacMd5Bytes; } /** * 16 * @param src * @return */ public static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException { String key="1234567890abcdef"; String src="123456789il7B0BSEjFdzpyKzfOFpvg/Se1CP802RItKYFPfSLRxJ3jf0bVl9hvYOEktPAYW2nd7S8MBcyHYyacHKbISq5iTmDzG+ivnR+SZJv3USNTYVMz9rCQVSxd0cLlqsJauko79NnwQJbzDTyLooYoIwz75qBOH2/xOMirpeEqRJrF/EQjWekJmGk9RtboXePu2rka+Xm51syBPhiXJAq0GfbfaFu9tNqs/e2Vjja/ltE1M0lqvxfXQ6da6HrThsm5id4ClZFIi0acRfrsPLRixS/IQYtksxghvJwbqOsbIsITail9Ayy4tKcogeEZiOO+4Ed264NSKmk7l3wKwJLAFjCFogBx8GE3OBz4pqcAn/ydA=201607291424000001"; byte[] macmd5 = HMacMD5Util.getHmacMd5Bytes(key.getBytes(), src.getBytes()); System.out.println(HMacMD5Util.bytesToHexString(macmd5)); } }
文章来源: HMAC-MD5 签名算法