Note that there are some explanatory texts on larger screens.

plurals
  1. POHMAC SHA1 Signature in Java
    text
    copied!<p>I am trying to interface with a TransUnion web service and I need to provide a HMAC-SHA1 signature to access it.</p> <p>This example is in the TransUnion documentation:<br/> Input of <code>SampleIntegrationOwner2008‐11‐18T19:14:40.293Z</code> with security key <code>xBy/2CLudnBJOxOtDhDRnsDYq9HTuDVr2uCs3FMzoxXEA/Od9tOuwSC70+mIfpjeG68ZGm/PrxFf/s/CzwxF4Q==</code> creates output of <code>/UhwvT/kY9HxiXaOjpIc/BarBkc=</code>.</p> <p>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?</p> <p>Here is the code I am currently working with:</p> <pre><code>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; } </code></pre> <p>Here is my unit test code:</p> <pre><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="); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload