How To Use Message Authentication Code (MAC) and Hash MAC (HMAC) in Java Code

Posted on November 8, 2012. Filed under: java/j2EE |


The following is an example of generate HMAC.

 

hmacDemo#mkdir hmacdemo
hmacDemo#cd hmacdemo/
hmacDemo#cp ~/Downloads/commons-codec-1.7/commons-codec-1.7.jar ./
hmacDemo#vi HMACDemo.java
hmacDemo#cat HMACDemo.java
import java.security.*;
import javax.crypto.*;

import org.apache.commons.codec.binary.Base64;
public class HMACDemo {
public static void main(String [] args) {
try {
String message = “data”;
KeyGenerator keygen = KeyGenerator.getInstance(“HmacSHA256”);//HmacMD5, HmacSHA1
Mac mac = Mac.getInstance(“HmacSHA256”);
mac.init(keygen.generateKey());
String str1 = new String(Base64.encodeBase64(mac.doFinal(message.getBytes())));
System.out.println(str1);
System.out.println(new String(Base64.encodeBase64(mac.doFinal(“data”.getBytes()))));
System.out.println(new String(Base64.encodeBase64(mac.doFinal(“datachange”.getBytes()))));

KeyGenerator ckeygen = KeyGenerator.getInstance(“HmacSHA256”);
Mac cmac = Mac.getInstance(“HmacSHA256”);
cmac.init(ckeygen.generateKey());
System.out.println(new String(Base64.encodeBase64(cmac.doFinal(message.getBytes()))));
System.out.println(new String(Base64.encodeBase64(cmac.doFinal(“data”.getBytes()))));

System.out.println(new String(Base64.encodeBase64(mac.doFinal(“data”.getBytes()))));
System.out.println(new String(Base64.encodeBase64(mac.doFinal(“datachange”.getBytes()))));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}

}

hmacDemo#ls
HMACDemo.java commons-codec-1.7.jar
hmacDemo#javac -cp commons-codec-1.7.jar HMACDemo.java
hmacDemo#java -cp ‘.:commons-codec-1.7.jar’ HMACDemo
pOyW2SPKuMuM4NMMb/fPEpxBqSpwSJTXN18bRcJaxck=
pOyW2SPKuMuM4NMMb/fPEpxBqSpwSJTXN18bRcJaxck=
SpScNNqrpnAAqATaphhp2yUMGXd4kPRWwOFejj27Uho=
9rG29RpNjBSVL6ZWpiM/yvkHJ68QFVIcI6bb5N4TYug=
9rG29RpNjBSVL6ZWpiM/yvkHJ68QFVIcI6bb5N4TYug=
pOyW2SPKuMuM4NMMb/fPEpxBqSpwSJTXN18bRcJaxck=
SpScNNqrpnAAqATaphhp2yUMGXd4kPRWwOFejj27Uho=
hmacDemo#
hmacDemo#java -cp ‘.:commons-codec-1.7.jar’ HMACDemo
hoDtjIiVS+FupfygXnmt7ufYZrayF3Nc6tdXDtvWBbU=
hoDtjIiVS+FupfygXnmt7ufYZrayF3Nc6tdXDtvWBbU=
cZYlVkbxCYIWb9F46GyGYptCrKhSAXBAgGvwcFUDZCc=
YjDp/uj3kg2+EotGjJqtLvlgX6uqhWmJSH4toeRmYvo=
YjDp/uj3kg2+EotGjJqtLvlgX6uqhWmJSH4toeRmYvo=
hoDtjIiVS+FupfygXnmt7ufYZrayF3Nc6tdXDtvWBbU=
cZYlVkbxCYIWb9F46GyGYptCrKhSAXBAgGvwcFUDZCc=
hmacDemo#
hmacDemo#java -cp ‘.:commons-codec-1.7.jar’ HMACDemo
exDKXWTZjgFoRgf33owjQIPwHBW6fD20eYJrOibLDPU=
exDKXWTZjgFoRgf33owjQIPwHBW6fD20eYJrOibLDPU=
3P7Y2WMn0TUlUriP6rDujHLfVXX6yjft23wAiMGFkB0=
kd5KLT/eMKqRgTqIMDnQGdnnsnNkmpE/mdOUt9gxPCU=
kd5KLT/eMKqRgTqIMDnQGdnnsnNkmpE/mdOUt9gxPCU=
exDKXWTZjgFoRgf33owjQIPwHBW6fD20eYJrOibLDPU=
3P7Y2WMn0TUlUriP6rDujHLfVXX6yjft23wAiMGFkB0=
hmacDemo#

Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: