Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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