Skip to content

Instantly share code, notes, and snippets.

@smutneja03
Created April 1, 2017 07:21
Show Gist options
  • Save smutneja03/1f58d8c0ea7f4773396eebe9e26778fb to your computer and use it in GitHub Desktop.
Save smutneja03/1f58d8c0ea7f4773396eebe9e26778fb to your computer and use it in GitHub Desktop.
HMAC Util for packet encryption and to prevent message tampering
package in.co.teni.service.banking.util;
import java.io.IOException;
import java.math.BigDecimal;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import in.co.teni.service.banking.ecollect.model.NotificationRequest;
import in.co.teni.service.banking.ecollect.model.ValidationRequest;
public class HmacUtil {
private final static String HMAC_SHA1_ALGORITHM = "HmacSHA1";
private static final Logger logger = LoggerFactory.getLogger(HmacUtil.class);
public static void main(String[] args) throws HttpException, IOException, NoSuchAlgorithmException {
String clientKey = "";
String clientSecret = "";
System.out.println("Client ID is " + clientKey + " and client secret is " + clientSecret );
ValidationRequest validationRequest = new ValidationRequest();
validationRequest.setAmount(new BigDecimal(10000));
validationRequest.setTxnRefId("P16111764473891");
validationRequest.setAccountNumber("SHOPXDT051624");
String stringValidationRequest = new ObjectMapper().writeValueAsString(validationRequest);
String messageDigestValidationRequest = calculateMD5(stringValidationRequest);
String hmacValidationRequest = calculateHMAC(clientSecret, messageDigestValidationRequest);
System.out.println("\n For validation request, " + stringValidationRequest + " message digest and hmac value is "
+ messageDigestValidationRequest + " " + hmacValidationRequest);
NotificationRequest notificationRequest = new NotificationRequest();
notificationRequest.setTxnRefId("P16111764473891");
notificationRequest.setTxnStatus("SUCCESS");
notificationRequest.setTxnRemark("Payment has been successfully acknowledged and transferred to the shopx account.");
notificationRequest.setUtrNumber("PBVD11764972802");
String stringNotificationRequest = new ObjectMapper().writeValueAsString(notificationRequest);
String messageDigestNotificationRequest = calculateMD5(stringNotificationRequest);
String hmacNotificationRequest = calculateHMAC(clientSecret, messageDigestNotificationRequest);
System.out.println("\n For notification request, " + stringNotificationRequest + " message digest and hmac value is "
+ messageDigestNotificationRequest + " " + hmacNotificationRequest);
}
public static String calculateHMAC(String secret, String data) {
try {
SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
return new String(Base64.encodeBase64(rawHmac));
} catch (GeneralSecurityException e) {
logger.warn("Unexpected error while creating hash: " + e.getMessage(), e);
return null;
} catch (Exception e){
logger.error("Unknown Exception occurred while doing the HMAC Calculation", e);
return null;
}
}
public static String calculateMD5(String contentToEncode) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(contentToEncode.getBytes());
return new String(Base64.encodeBase64(digest.digest()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment