Created
January 30, 2015 14:03
-
-
Save ramesh-lingappan/48f6c9967d6e9672b8c8 to your computer and use it in GitHub Desktop.
Calculate HMAC-SHA1 value of payload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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