Created
December 3, 2017 22:17
-
-
Save igorrendulic/fd6c547b9609c599642ab2a48f40cb22 to your computer and use it in GitHub Desktop.
MailGun Webhook Signature Validation
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
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