title: HMAC encryption result different from JUnit test and Spring
body: <p>I have some strange issue with my function. I'm trying to get a HMAC encrypted data from a secret key. 
  I worked on a unit test to try my algorithm, and it worked fine.</p>
  
  <p>Here is the code, with hard datas to make it easier to test :</p>
  
  <pre><code>public static String hmac(String parameters, String key) {
      String result = "";
      parameters = "PBX_SITE=1999888&amp;PBX_RANG=32&amp;PBX_IDENTIFIANT=2&amp;PBX_TOTAL=1000&amp;PBX_DEVISE=978&amp;PBX_CMD=TEST TEST&amp;PBX_PORTEUR=test@test.com&amp;PBX_RETOUR=Mt:M;Ref:R;Auto:A;Erreur:E;Id:U&amp;PBX_HASH=SHA512&amp;PBX_TIME=2011-02-28T11:01:50+01:00&amp;PBX_TYPEPAIEMENT=CARTE&amp;PBX_TYPECARTE=CB&amp;PBX_AUTOSEULE=O";
      key = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
  
      try{
          final String HMAC_SHA512 = "HmacSHA512";
          Mac sha512_HMAC = Mac.getInstance(HMAC_SHA512); 
  
          //pack
          String input = key.length() % 2 == 0 ? key : key  + "0";
          StringBuilder output = new StringBuilder();
          for (int i = 0; i &lt; input.length(); i+=2) {
              String str = input.substring(i, i+2);
              output.append((char)Integer.parseInt(str, 16));
          }
          key = output.toString();
  
          SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA512);
          sha512_HMAC.init(keySpec);
  
          byte [] mac_data = sha512_HMAC.doFinal(parameters.getBytes());
  
          //toHex
          result = String.format("%040x", new BigInteger(1, mac_data)).toUpperCase();
          System.out.println(result);
  
      } catch (NoSuchAlgorithmException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (InvalidKeyException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } finally{
          System.out.println("Done");
      }
  
      return result;
  }
  </code></pre>
  
  <p>When i call this function with my Unit Test, the result is fine : </p>
  
  <pre><code>String hmac2 = AboWebUtils.hmac(parameters, key);
  </code></pre>
  
  <p>9D47FFC1779013D1D26F92EE1C858AD7ECE5333106D4D61892BC683F640B061A72124F72A5B7BCD445767E347F9BA8ADE9A08A36791669E32E99C17975BD2FC2</p>
  
  <p>When i call this method from y web app (Spring framework), i got a different result (wrong) :</p>
  
  <pre><code>AboWebUtils.hmac(parameters, key);
  </code></pre>
  
  <p>14BC65DEC5AF3CEED21F024D6A5665F6C8BDF90F956F8D327E0382A3A7F09ACEF557A2C5CE72F32D0CA2B3FBBE344CE5D2798526817E5647210D663AEA9BB899</p>
  
  <p>I don't get why the results are differents. I am calling the same method, the parameters are even overriden for test purpose. I should get the same result. JUnit &amp; Spring dont call the method with the same way or something like that?</p>
  
  <p>Thanks for your help :)</p>