Skip to content

Commit

Permalink
Downgrade dependency Base64 for terminal (#1350)
Browse files Browse the repository at this point in the history
* added commons-codec byte64 encoding and decoding support for Android 7

* adjusted unit-test

* added commons-codec to NexoCrypto.java
  • Loading branch information
DjoykeAbyah authored Aug 30, 2024
1 parent c29fdef commit a6a3b19
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@
<version>3.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version> <!-- or the latest version -->
</dependency>
<!-- Generated model annotations -->
<dependency>
<groupId>io.swagger</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Map;
import java.util.Objects;

import java.util.Base64;
import org.apache.commons.codec.binary.Base64;
import com.adyen.model.applicationinfo.ApplicationInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -278,11 +278,12 @@ public String toString() {

public String toBase64() {
String json = PRETTY_PRINT_GSON.toJson(this);
return new String(Base64.getEncoder().encode(json.getBytes()));
byte[] encodedBytes = Base64.encodeBase64(json.getBytes());
return new String(encodedBytes);
}

public static SaleToAcquirerData fromBase64(String base64) {
byte[] decoded = Base64.getDecoder().decode(base64);
byte[] decoded = Base64.decodeBase64(base64);
try (Reader reader = new InputStreamReader(new ByteArrayInputStream(decoded))) {
return PRETTY_PRINT_GSON.fromJson(reader, SaleToAcquirerData.class);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/adyen/terminal/security/NexoCrypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.adyen.model.terminal.security.SecurityKey;
import com.adyen.model.terminal.security.SecurityTrailer;
import com.adyen.terminal.security.exception.NexoCryptoException;
import java.util.Base64;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
Expand Down Expand Up @@ -74,15 +74,15 @@ public SaleToPOISecuredMessage encrypt(String saleToPoiMessageJson, MessageHeade

SaleToPOISecuredMessage saleToPoiSecuredMessage = new SaleToPOISecuredMessage();
saleToPoiSecuredMessage.setMessageHeader(messageHeader);
saleToPoiSecuredMessage.setNexoBlob(new String(Base64.getEncoder().encode(encryptedSaleToPoiMessage)));
saleToPoiSecuredMessage.setNexoBlob(new String(Base64.encodeBase64(encryptedSaleToPoiMessage)));
saleToPoiSecuredMessage.setSecurityTrailer(securityTrailer);

return saleToPoiSecuredMessage;
}

public String decrypt(SaleToPOISecuredMessage saleToPoiSecuredMessage) throws Exception {
NexoDerivedKey derivedKey = getNexoDerivedKey();
byte[] encryptedSaleToPoiMessageByteArray = Base64.getDecoder().decode(saleToPoiSecuredMessage.getNexoBlob().getBytes());
byte[] encryptedSaleToPoiMessageByteArray = Base64.decodeBase64(saleToPoiSecuredMessage.getNexoBlob().getBytes());
byte[] ivNonce = saleToPoiSecuredMessage.getSecurityTrailer().getNonce();
byte[] decryptedSaleToPoiMessageByteArray = crypt(encryptedSaleToPoiMessageByteArray, derivedKey, ivNonce, Cipher.DECRYPT_MODE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.GsonBuilder;
import java.util.Base64;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;

import java.util.HashMap;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void testSerialize() {

// test if base64 works
String serialized = saleToAcquirerDataModelAdapter.serialize(saleToAcquirerData, null, null).getAsString();
SaleToAcquirerData saleToAcquirerDataDecoded = new Gson().fromJson(new String(Base64.getDecoder().decode(serialized)), SaleToAcquirerData.class);
SaleToAcquirerData saleToAcquirerDataDecoded = new Gson().fromJson(new String(Base64.decodeBase64(serialized)), SaleToAcquirerData.class);
assertEquals(saleToAcquirerData, saleToAcquirerDataDecoded);
}

Expand Down

0 comments on commit a6a3b19

Please sign in to comment.