Generating a MAC using a pre-shared key
1 posts in topic
Flat View  Flat View

Posted By:   Bob_McLaren
Posted On:   Wednesday, March 15, 2006 10:58 AM

I have an application that requires that I validate some input information from a third party using an HMAC hash with a specified 16-byte (32 character) key.
In my searching, I have found plenty of utility classes for doing MD5 hashing, MAC generation, etc.., but all utilities that use secret keys seem to depend on a random key generator, rather than simply accepting a static key.

Can you point me in the right direction?

Re: Generating a MAC using a pre-shared key

Posted By:   Bob_McLaren  
Posted On:   Monday, March 20, 2006 11:03 AM

In case anyone else is searching for this answer, I finally found it. Here is the code I used.

byte[] sKeyBytes=com.sun.org.apache.xerces.internal.impl.dv.util.HexBin.decode("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
javax.crypto.spec.SecretKeySpec skey = new javax.crypto.spec.SecretKeySpec(sKeyBytes,"HmacMD5");
javax.crypto.Mac mac = javax.crypto.Mac.getInstance(skey.getAlgorithm());
String plaintext="Hi There";
byte[] textbytes=plaintext.getBytes("UTF-8");
byte[] results=mac.doFinal(textbytes);
String strResults=com.sun.org.apache.xerces.internal.impl.dv.util.HexBin.encode(results);
System.out.println("HMAC:"+ strResults);
About | Sitemap | Contact