APIs & Integrations

surekhas
Member

webhook X-Hubspot-Signature does not match

I am trying to validate this in my java app like

		String sha256 = request.getHeader("X-HubSpot-Signature");
		String requestBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
		String clientSecret = "xxxxxx";
		String source = clientSecret + requestBody;
		MessageDigest digest = MessageDigest.getInstance("SHA-256");
		byte[] hash = digest.digest(source.getBytes(StandardCharsets.UTF_8));
		String encoded = Base64.getEncoder().encodeToString(hash);
        return sha256.equals(encoded);

 

This always returns false, can anyone see what's the issue here. I have redacted the client-secret.

0 Upvotes
3 Replies 3
dennisedson
HubSpot Product Team
HubSpot Product Team

webhook X-Hubspot-Signature does not match

Hey @surekhas ,

I admit that I am unfamiliar with programming in java, but looking at the code you have here, I am not seeing the http method or the URI anywhere.  

Referencing the v2 request validation docs,  It specifies that you will need a string with the following concatenated

Client secret + http method + URI + request body (if present)

 

Hope this helps,

0 Upvotes
surekhas
Member

webhook X-Hubspot-Signature does not match

0 Upvotes
surekhas
Member

webhook X-Hubspot-Signature does not match

I could get it working with below code

    final String sha256 = request.getHeader("X-HubSpot-Signature");
    final String requestBody = request.getReader().lines().collect(Collectors.joining());
    final String source = clientSecret + requestBody;
    final String encoded = Hashing.sha256().hashString(source, StandardCharsets.UTF_8).toString();
    return sha256.equals(encoded);