Skip to content

Instantly share code, notes, and snippets.

@igorrendulic
Created December 3, 2017 22:17
Show Gist options
  • Save igorrendulic/fd6c547b9609c599642ab2a48f40cb22 to your computer and use it in GitHub Desktop.
Save igorrendulic/fd6c547b9609c599642ab2a48f40cb22 to your computer and use it in GitHub Desktop.
MailGun Webhook Signature Validation
private static final key = "mailgun_key"
public static boolean isSignatureValid(String token , long timestamp, String signature) {
try {
Mac hmac = Mac.getInstance("HmacSHA256");
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA256");
hmac.init(signingKey);
String signed = Hex.encodeHexString(hmac.doFinal((timestamp + token).getBytes()));
if (signed.equals(signature)) {
return true;
}
return false;
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment