Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/com/adyen/util/HMACValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public String calculateHMAC(String data, String key)
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Missing data or key: " + e.getMessage());
} catch (DecoderException e) {
throw new IllegalArgumentException("Invalid Hex HMAC key: " + key);
throw new IllegalArgumentException(
"Invalid Hex HMAC key: it must be a hexadecimal string with an even number of characters");
} catch (Exception e) {
throw new SignatureException("Failed to generate HMAC: " + e.getMessage());
}
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/com/adyen/util/HMACValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.adyen.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -186,7 +187,20 @@ public void testCalculateHMACInvalidKeyFormat() {
assertThrows(
IllegalArgumentException.class,
() -> new HMACValidator().calculateHMAC("TestPayload", "BadKey"));
assertEquals("Invalid Hex HMAC key: BadKey", exception.getMessage());
assertEquals(
"Invalid Hex HMAC key: it must be a hexadecimal string with an even number of characters",
exception.getMessage());
}

@Test
public void testCalculateHMACInvalidKeyIsNotInMessage() {
// A key with quotes left around it fails the hex check; the secret must not reach the logs.
String quotedKey = "\"" + HMAC_KEY + "\"";
IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() -> new HMACValidator().calculateHMAC("TestPayload", quotedKey));
assertFalse(exception.getMessage().contains(HMAC_KEY));
}

@Test
Expand Down