Help required: How to generate a MAC (HMAC SHA1) with Java
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. ([url]http://www.slavasoft.com/hashcalc/index.htm[/url])
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
[B]Java class:[/B]
[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;
}
}[/CODE]
[B]
Input / 'arguments':[/B]
Key:
[CODE]1234[/CODE]
Data
[CODE]c54601c60001550187360603773500018722060342726f7773696e675f4750525300018707060353757065726d616e2053796e634d4c000187340603687474703a2f2f6d6574726f706f6c69732e636f6d3a383038302f736572766963652f73796e630001c65901873a06032e2f636f6e7461637473000187070603436f6e74616374732044420001872e0603746578742f782d7663617264000101c65901873a06032e2f63616c656e64617200018707060343616c656e6461722044420001872e0603746578742f782d7663616c656e646172000101c65701873106036e616d653400018732060370617373776f7264340001010101[/CODE]
[B]Output:[/B]
My output:
[CODE]dffe01657cc99b02394cb7b9708654558d63d4dd[/CODE]
Output from HashCalc:
[CODE]49a4f8bac2a1987292a427faf7ad026c316f361f[/CODE]
Re: Help required: How to generate a MAC (HMAC SHA1) with Java
OK - I just figured out why my result differs from that of the HashCalc Tool.
When I entered the 'data' in HashCalc, I specified that the 'data' format is 'Hex'.
If I specify in HashCalc that the 'data' format is 'Text', it comes back with the same result as my code.
Do I need to somehow submit the 'data' to the MAC Signing process as a Hex 'binary stream'? Or can I pass it in as an ASCII string of Hex characters?
Is there a document somewhere with test data and results? (The S40 Series document does not appear to mention what USERPIN they used to generate their MAC...)
Re: Help required: How to generate a MAC (HMAC SHA1) with Java
Hi pcimring. Did u find a solution for this? When using "text" in HashCalc all the hash functions match, however, using "Hex" produces the alternate seemingly correct values.
Now I am not sure how to manipulate the string passed to the encoding functions. I doubt the encoding functions are wrong.
So how do I tell these encoding functions the string passed is in hex....like in hashCalc?...or must another conversion take place before the final hex conversion is done for the WSP headers?
Re: Help required: How to generate a MAC (HMAC SHA1) with Java
[QUOTE=starchyme1;443690]Hi pcimring. Did u find a solution for this? When using "text" in HashCalc all the hash functions match, however, using "Hex" produces the alternate seemingly correct values.
Now I am not sure how to manipulate the string passed to the encoding functions. I doubt the encoding functions are wrong.
So how do I tell these encoding functions the string passed is in hex....like in hashCalc?...or must another conversion take place before the final hex conversion is done for the WSP headers?[/QUOTE]
Yes, I did find the solution: there is a subtle mistake in the code. See below:
[URL="http://forums.sun.com/thread.jspa?threadID=5283141"]
http://forums.sun.com/thread.jspa?threadID=5283141[/URL]
Pete