×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    25

    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. (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:

    Code:
    1234
    Data

    Code:
    c54601c60001550187360603773500018722060342726f7773696e675f4750525300018707060353757065726d616e2053796e634d4c000187340603687474703a2f2f6d6574726f706f6c69732e636f6d3a383038302f736572766963652f73796e630001c65901873a06032e2f636f6e7461637473000187070603436f6e74616374732044420001872e0603746578742f782d7663617264000101c65901873a06032e2f63616c656e64617200018707060343616c656e6461722044420001872e0603746578742f782d7663616c656e646172000101c65701873106036e616d653400018732060370617373776f7264340001010101
    Output:

    My output:

    Code:
    dffe01657cc99b02394cb7b9708654558d63d4dd
    Output from HashCalc:

    Code:
    49a4f8bac2a1987292a427faf7ad026c316f361f

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    25

    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...)

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    3

    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?

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    25

    Re: Help required: How to generate a MAC (HMAC SHA1) with Java

    Quote Originally Posted by starchyme1 View Post
    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?
    Yes, I did find the solution: there is a subtle mistake in the code. See below:

    http://forums.sun.com/thread.jspa?threadID=5283141


    Pete

Similar Threads

  1. Java 1.3.1 Required
    By tunelabguy in forum Symbian Tools & SDKs
    Replies: 1
    Last Post: 2006-05-01, 23:14
  2. Replies: 3
    Last Post: 2005-02-14, 16:37
  3. How develop Java games on mobile phones?
    By ericzutter in forum Mobile Java General
    Replies: 4
    Last Post: 2003-11-20, 23:14
  4. Java tools for Mac
    By joakim.knudsen in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2003-05-27, 18:00
  5. Java API to generate SMIL
    By melwong in forum General Messaging
    Replies: 1
    Last Post: 2003-03-30, 10:50

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×