Package com.adyen.util
Class HMACValidator
- java.lang.Object
-
- com.adyen.util.HMACValidator
-
public class HMACValidator extends Object
Utility class for generating and validating HMAC signatures used in Adyen webhooks.
-
-
Field Summary
Fields Modifier and Type Field Description static StringDATA_SEPARATORSeparator used when joining data for signature calculation.static StringHMAC_SHA256_ALGORITHMThe HMAC algorithm used.
-
Constructor Summary
Constructors Constructor Description HMACValidator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringcalculateHMAC(NotificationRequestItem notificationRequestItem, String key)Computes the HMAC SHA-256 signature for a given NotificationRequestItem.StringcalculateHMAC(String data, String key)Computes the HMAC SHA-256 signature for the given data using the provided hex-encoded HMAC key.StringgetDataToSign(NotificationRequestItem notificationRequestItem)Builds the concatenated data string from a NotificationRequestItem to be used in HMAC signature calculation.booleanvalidateHMAC(NotificationRequestItem notificationRequestItem, String key)Validates the HMAC signature of an NotificationRequestItem.booleanvalidateHMAC(String hmacSignature, String hmacKey, String payload)Validates an HMAC signature for generic webhooks (e.g.
-
-
-
Field Detail
-
HMAC_SHA256_ALGORITHM
public static final String HMAC_SHA256_ALGORITHM
The HMAC algorithm used.- See Also:
- Constant Field Values
-
DATA_SEPARATOR
public static final String DATA_SEPARATOR
Separator used when joining data for signature calculation.- See Also:
- Constant Field Values
-
-
Method Detail
-
calculateHMAC
public String calculateHMAC(String data, String key) throws IllegalArgumentException, SignatureException
Computes the HMAC SHA-256 signature for the given data using the provided hex-encoded HMAC key.- Parameters:
data- the data to signkey- the HMAC key in hexadecimal format- Returns:
- the Base64-encoded HMAC signature
- Throws:
IllegalArgumentException- if the data or key is nullSignatureException- if signature generation fails
-
calculateHMAC
public String calculateHMAC(NotificationRequestItem notificationRequestItem, String key) throws IllegalArgumentException, SignatureException
Computes the HMAC SHA-256 signature for a given NotificationRequestItem.- Parameters:
notificationRequestItem- the notification to signkey- the HMAC key in hexadecimal format- Returns:
- the Base64-encoded HMAC signature
- Throws:
IllegalArgumentException- if the notification item or key is invalidSignatureException- if signature generation fails
-
validateHMAC
public boolean validateHMAC(String hmacSignature, String hmacKey, String payload) throws SignatureException
Validates an HMAC signature for generic webhooks (e.g. Banking, Management).- Parameters:
hmacSignature- the signature received in the webhookhmacKey- the configured HMAC keypayload- the payload string used for HMAC calculation- Returns:
trueif the signature matches,falseotherwise- Throws:
SignatureException- if validation fails
-
validateHMAC
public boolean validateHMAC(NotificationRequestItem notificationRequestItem, String key) throws IllegalArgumentException, SignatureException
Validates the HMAC signature of an NotificationRequestItem.- Parameters:
notificationRequestItem- the notification containing the HMAC signaturekey- the HMAC key in hexadecimal format- Returns:
trueif the signature matches,falseotherwise- Throws:
IllegalArgumentException- if the notification or signature is missingSignatureException- if signature validation fails
-
getDataToSign
public String getDataToSign(NotificationRequestItem notificationRequestItem) throws IllegalArgumentException
Builds the concatenated data string from a NotificationRequestItem to be used in HMAC signature calculation.The data string is composed of the following fields, separated by a colon (":"):
- pspReference
- originalReference
- merchantAccountCode
- merchantReference
- amount.value
- amount.currency
- eventCode
- success
If any value is
null, it is represented as an empty string in the final payload.- Parameters:
notificationRequestItem- the notification request item- Returns:
- the colon-separated string of fields
- Throws:
IllegalArgumentException- if the input is null
-
-