prowave / chargify-webhook-java

Java library to parse the request body of a Chargify Webhook.
Apache License 2.0
6 stars 9 forks source link

Comparing Hmac value #25

Closed aakashcentime closed 1 month ago

aakashcentime commented 2 years ago

The issue is with request body that comes. Chargify calculates HMAC with partially encoded body which creates a problem.

how i solved it.

public ResponseEntity receiver(@RequestBody String requestBody, @RequestParam String id, @NotNull @RequestParam String event, @RequestHeader("X-Chargify-Webhook-Signature-Hmac-Sha-256") String hmacHeader) { requestBody= requestBody.replace("%5B","["); requestBody = requestBody.replace("%5D","]"); requestBody = requestBody.replace("+","%20");

Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec("YOUR KEY".getBytes("UTF-8"), "HmacSHA256"); sha256_HMAC.init(secret_key); System.out.println("hmacgiven: "+hmacHeader); System.out.println(Hex.encodeHexString(sha256_HMAC.doFinal(requestBody.getBytes(StandardCharsets.UTF_8))));