Skip to content

Instantly share code, notes, and snippets.

@keeth
Created March 4, 2015 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keeth/c9394cc2bdca1b9ef255 to your computer and use it in GitHub Desktop.
Save keeth/c9394cc2bdca1b9ef255 to your computer and use it in GitHub Desktop.
package fitt.util
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.security.SignatureException
class HmacUtil {
static String hmac(String text, String secret) {
String result
try {
// get an hmac_sha1 key from the raw key bytes
SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), "HmacSHA1");
// get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
// compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(text.getBytes());
result= rawHmac.encodeHex()
} catch (Exception e) {
throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment