Skip to content

Instantly share code, notes, and snippets.

@ramesh-lingappan
Created January 30, 2015 14:03
Show Gist options
  • Save ramesh-lingappan/48f6c9967d6e9672b8c8 to your computer and use it in GitHub Desktop.
Save ramesh-lingappan/48f6c9967d6e9672b8c8 to your computer and use it in GitHub Desktop.
Calculate HMAC-SHA1 value of payload
/**
* @param key - client secret key
* @param data - can be any data (eg: http request or response payload)
* @return
*/
public static String calculateHmacSHA1(String key, byte[] data) {
if (key == null || data == null || data.length <= 0)
return null;
String HMAC_SHA1_ALGORITHM = "HmacSHA1";
try {
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM));
byte[] rawHmac = mac.doFinal(data);
return Base64.encodeBase64String(rawHmac);
} catch (Exception e) {
// hanlde in your own way
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment