Hi
I am attempting to generate a MAC (HMAC SHA1) with Java code - based on 'USERPIN'.
However, when I run the code on a given Hex String, it returns a different value to that of HashCalc. (http://www.slavasoft.com/hashcalc/index.htm)
Can anyone provide me with a Java code snippet that works and has been tested?
I have included my code and the results below.
Thanks
Pete
Java class:
Code:
import java.security.SignatureException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
public class GenerateMac {
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
public static String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException {
/**
*@param data
* The data to be signed. (The wbxml as a string of Hex digits.)
*@param key
* The signing key. (E.g. USERPIN of '1234')
*/
String result;
try {
// Get an hmac_sha1 key from the raw key bytes
byte[] keyBytes = key.getBytes();
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1_ALGORITHM);
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(data.getBytes());
// Convert raw bytes to Hex
byte[] hexBytes = new Hex().encode(rawHmac);
// Covert array of Hex bytes to a String
result = new String(hexBytes, "ISO-8859-1");
System.out.println("MAC : " + result);
}
catch (Exception e) {
throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
}
return result;
}
}
Input / 'arguments':
Key:
Data
Code:
c54601c60001550187360603773500018722060342726f7773696e675f4750525300018707060353757065726d616e2053796e634d4c000187340603687474703a2f2f6d6574726f706f6c69732e636f6d3a383038302f736572766963652f73796e630001c65901873a06032e2f636f6e7461637473000187070603436f6e74616374732044420001872e0603746578742f782d7663617264000101c65901873a06032e2f63616c656e64617200018707060343616c656e6461722044420001872e0603746578742f782d7663616c656e646172000101c65701873106036e616d653400018732060370617373776f7264340001010101
Output:
My output:
Code:
dffe01657cc99b02394cb7b9708654558d63d4dd
Output from HashCalc:
Code:
49a4f8bac2a1987292a427faf7ad026c316f361f