我正在使用来自http://tools.ietf.org/html/rfc4231
的测试向量测试我的Hmac但是在测试用例3“使用大于64字节的密钥和数据的组合长度进行测试(= SHA-224和SHA-256的块大小)。”我得到的摘要与正确的摘要不同。
byte[] key = hexify("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
byte[] data = hexify("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
byte[] correct = hexify("773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe");
// Create digest
SecretKey macKey = new SecretKeySpec(key, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(macKey);
byte[] digest = mac.doFinal(data);
知道为什么他们会变得不同吗?我错过了什么?我对此很陌生。
// Hex encoded
a5418172bb54bf71f3ec28d1c9f34c48da17007eac4d0ca9e2f8ab54b91603e8
773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe
答案 0 :(得分:3)
我无法重现您的问题。对我来说,以下SSCCE工作得很好:
public static void main(String[] args) throws Exception {
byte[] key = hexify("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
byte[] data = hexify("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
byte[] correct = hexify("773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe");
// Create digest
SecretKey macKey = new SecretKeySpec(key, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(macKey);
byte[] digest = mac.doFinal(data);
System.out.println(Arrays.equals(correct, digest));
}
private static byte[] hexify(String string) {
return DatatypeConverter.parseHexBinary(string);
}
打印:true
显示或比较代码中可能出现错误?