Created
March 19, 2023 15:43
-
-
Save motaher13/6276079a79248adf4ae372bbf42c71a2 to your computer and use it in GitHub Desktop.
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 boolean isValidWebhook(String secret, String body, String timestamp, String headerSignature) { | |
String computedSignature = getSignature(secret, body, timestamp); | |
return headerSignature.equals(computedSignature); | |
} | |
String getSignature(String secret, String body, String timestamp) { | |
SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), HMAC); | |
try { | |
String signature = body + timestamp; | |
Mac mac = Mac.getInstance(HMAC); | |
mac.init(signingKey); | |
byte[] rawHmac = mac.doFinal(signature.getBytes(StandardCharsets.UTF_8)); | |
return DatatypeConverter.printBase64Binary(rawHmac); | |
} catch (Exception e) { | |
LOGGER.error("Failed to generate Webhook Signature", e.getMessage()); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment