By default all webhooks use HMAC_SHA_1 as encryption algorithm.
Encrypt mechanism:
- Convert request body to bytes
- Use merchant x-api-key as sign key
- Save result as hex string
- Send data to merchant in "x-payload-digest" request header
Decrypt/check mechanism:
- Retry 1-3 encryption steps on merchant side
- Compare result with "x-payload-digest" string from request header - must be equals
private String signHMAC(final byte[] body, final String apiKey) {
return new HmacUtils(HmacAlgorithms.HMAC_SHA_1, apiKey).hmacHex(body);
}
func verifySign(rawBody []byte, requestSign string) bool {
fmt.Println("Verify signature")
currentSign := generateSignature(rawBody)
fmt.Println("Request hmac: " + requestSign)
fmt.Println("Current hmac: " + currentSign)
return currentSign == requestSign
}
func generateSignature(payloadBody []byte) string {
mac := hmac.New(sha1.New, []byte(apiKey))
mac.Write(payloadBody)
expectedMAC := mac.Sum(nil)
return hex.EncodeToString(expectedMAC)
}