I'm trying to verify the signature received from a webhook, but I can't.
I've asked chatgpt for help, but I can't get it to compile what you suggested.
I'll paste it here anyway.
Thanks.
I've asked chatgpt for help, but I can't get it to compile what you suggested.
I'll paste it here anyway.
Thanks.
B4X:
For added security, you can define a secret value when creating a webhook. This secret is used to generate a digital signature (HMAC with the SHA-256 algorithm) that is included in each notification, within the X-Webhook-Signature HTTP header.
This signature allows the webhook recipient to verify that:
The message was sent through our API
The content was not modified during transmission
To verify the signature:
You must obtain the exact body of the received message (unaltered).
Use the same secret associated with the webhook to calculate an HMAC-SHA256 signature (in hexadecimal).
Compare the generated signature with the one contained in the X-Webhook-Signature header.
If both signatures match, you can trust that the message is authentic.
B4X:
Sub ValidateWebhookSignature(body As String, headerSignature As String, secret As String)
Dim mac As Mac
mac.Initialise("HMACSHA256", secret.GetBytes("UTF8"))
Dim data() As Byte = body.GetBytes("UTF8")
Dim computed() As Byte = bc.(mac.Sign(data)
Dim bc As ByteConverter
Dim computedHex As String = bc.HexFromBytes(computed).ToLowerCase
If computedHex = headerSignature.ToLowerCase Then
Log("✅ Firma válida")
Else
Log("❌ Firma inválida")
End If
End Sub