Skip to content

Instantly share code, notes, and snippets.

@motaher13
Created March 19, 2023 15:43
Show Gist options
  • Save motaher13/6276079a79248adf4ae372bbf42c71a2 to your computer and use it in GitHub Desktop.
Save motaher13/6276079a79248adf4ae372bbf42c71a2 to your computer and use it in GitHub Desktop.
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