问题描述
我正在尝试与TransUnion Web服务进行交互,我需要提供一个HMAC-SHA1签名来访问它。
I am trying to interface with a TransUnion web service and I need to provide a HMAC-SHA1 signature to access it.
此示例位于TransUnion文档中:
输入 SampleIntegrationOwner2008-11-18T19:14:40.293Z
带安全
键 xBy / 2CLudnBJOxOtDhDRnsDYq9HTuDVr2uCs3FMzoxXEA / Od9tOuwSC70 + mIfpjeG68ZGm / PrxFf / s / CzwxF4Q ==
创建 / UhwvT / kY9HxiXaOjpIc / BarBkc =
的输出。
This example is in the TransUnion documentation:
Input of SampleIntegrationOwner2008‐11‐18T19:14:40.293Z
with securitykey xBy/2CLudnBJOxOtDhDRnsDYq9HTuDVr2uCs3FMzoxXEA/Od9tOuwSC70+mIfpjeG68ZGm/PrxFf/s/CzwxF4Q==
creates output of /UhwvT/kY9HxiXaOjpIc/BarBkc=
.
鉴于数据和密钥,我无法在Java中获得相同的结果。我已经尝试了几个在线计算器,但也没有一个返回这个结果。他们的文档中的示例是否不正确,或者我只是没有正确处理这些字符串?
Given that data and key, I cannot get this same result in Java. I have tried several online calculators, and none of them return this result either. Is the example in their documentation incorrect, or am I just not handling these strings correctly?
以下是我目前使用的代码:
Here is the code I am currently working with:
public static String calcShaHash (String data, String key) {
String HMAC_SHA1_ALGORITHM = "HmacSHA1";
String result = null;
try {
Key signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
result = Base64.encodeBase64String(rawHmac);
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
这是我的单位测试代码:
Here is my unit test code:
@Test
public void testCalcShaHash() {
String data = "SampleIntegrationOwner2008-11-18T19:14:40.293Z";
String key = "xBy/2CLudnBJOxOtDhDRnsDYq9HTuDVr2uCs3FMzoxXEA/Od9tOuwSC70+mIfpjeG68ZGm/PrxFf/s/CzwxF4Q==";
String result = Utils.calcShaHash(data, key);
assertEquals(result, "/UhwvT/kY9HxiXaOjpIc/BarBkc=");
}
推荐答案
有一点我注意到连字符不是正常的连字符。如果复制并粘贴它们,则它们不在ASCII字符集中。我可以肯定地说,哈希长度看起来是正确的。有趣的是,即使在输入正确的连字符后,我也无法让你的代码产生正确的答案。但无论如何。它解决了这个问题。 Huzzah!
One thing I noticed is that the hyphens are not normal hyphens. If you copy and paste them, they are not in the ASCII character set. All I can say for sure is that the hash length appears correct. The funny thing is, I couldn't get your code to produce the correct answer, even after putting the correct hyphens in. But no matter. It solved the problem. Huzzah!
这篇关于Java中的HMAC SHA1签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!