diff --git a/jpos/src/main/java/org/jpos/emv/cryptogram/CPACryptogram.java b/jpos/src/main/java/org/jpos/emv/cryptogram/CPACryptogram.java index 1446242b53..2beaa8987b 100644 --- a/jpos/src/main/java/org/jpos/emv/cryptogram/CPACryptogram.java +++ b/jpos/src/main/java/org/jpos/emv/cryptogram/CPACryptogram.java @@ -4,15 +4,11 @@ import org.jpos.security.MKDMethod; import org.jpos.security.SKDMethod; -import org.jpos.emv.cryptogram.CryptogramDataBuilder.PaddingMethod; - /** * Common Payment Application (CPA) Cryptogram Specification */ public class CPACryptogram implements CryptogramSpec { - final PaddingMethod paddingMethod = CryptogramDataBuilder.ISO9797Method2; - @Override public MKDMethod getMKDMethod() { return MKDMethod.OPTION_A; diff --git a/jpos/src/main/java/org/jpos/emv/cryptogram/CVN10DataBuilder.java b/jpos/src/main/java/org/jpos/emv/cryptogram/CVN10DataBuilder.java index 283d6d1fa9..437d89181f 100644 --- a/jpos/src/main/java/org/jpos/emv/cryptogram/CVN10DataBuilder.java +++ b/jpos/src/main/java/org/jpos/emv/cryptogram/CVN10DataBuilder.java @@ -36,7 +36,6 @@ public String getDefaultARPCRequest(boolean approved) { return approved ? "0000" : "9900"; } - @Override public String buildARQCRequest(TLVList data, IssuerApplicationData iad) { StringBuilder sb = new StringBuilder(); @@ -46,7 +45,7 @@ public String buildARQCRequest(TLVList data, IssuerApplicationData iad) { } @Override - public String buildARQCRequest_padded(TLVList data, IssuerApplicationData iad, PaddingMethod paddingMethod){ - return paddingMethod.apply(buildARQCRequest(data, iad)); + public PaddingMethod getPaddingMethod() { + return PaddingMethod.ISO9797Method1; } } diff --git a/jpos/src/main/java/org/jpos/emv/cryptogram/CVN18DataBuilder.java b/jpos/src/main/java/org/jpos/emv/cryptogram/CVN18DataBuilder.java index e01f1175c7..a7afa712c9 100644 --- a/jpos/src/main/java/org/jpos/emv/cryptogram/CVN18DataBuilder.java +++ b/jpos/src/main/java/org/jpos/emv/cryptogram/CVN18DataBuilder.java @@ -49,7 +49,7 @@ public String buildARQCRequest(TLVList data, IssuerApplicationData iad) { } @Override - public String buildARQCRequest_padded(TLVList data, IssuerApplicationData iad, PaddingMethod paddingMethod) { - return paddingMethod.apply(buildARQCRequest(data, iad)); + public PaddingMethod getPaddingMethod() { + return PaddingMethod.ISO9797Method1; } } diff --git a/jpos/src/main/java/org/jpos/emv/cryptogram/CVNCPADataBuilder.java b/jpos/src/main/java/org/jpos/emv/cryptogram/CVNCPADataBuilder.java index e81033c40e..f6c99b9c9c 100644 --- a/jpos/src/main/java/org/jpos/emv/cryptogram/CVNCPADataBuilder.java +++ b/jpos/src/main/java/org/jpos/emv/cryptogram/CVNCPADataBuilder.java @@ -20,7 +20,7 @@ public String buildARQCRequest(TLVList data, IssuerApplicationData iad) { } @Override - public String buildARQCRequest_padded(TLVList data, IssuerApplicationData iad, PaddingMethod paddingMethod) { - return paddingMethod.apply(buildARQCRequest(data, iad)); + public PaddingMethod getPaddingMethod() { + return PaddingMethod.ISO9797Method2; } } diff --git a/jpos/src/main/java/org/jpos/emv/cryptogram/CVNMCDataBuilder.java b/jpos/src/main/java/org/jpos/emv/cryptogram/CVNMCDataBuilder.java index a555e60c85..ec61f65dba 100644 --- a/jpos/src/main/java/org/jpos/emv/cryptogram/CVNMCDataBuilder.java +++ b/jpos/src/main/java/org/jpos/emv/cryptogram/CVNMCDataBuilder.java @@ -48,14 +48,13 @@ public String buildARQCRequest(TLVList data, IssuerApplicationData iad) { return sb.toString(); } - @Override - public String buildARQCRequest_padded(TLVList data, IssuerApplicationData iad, PaddingMethod paddingMethod) { - return paddingMethod.apply(buildARQCRequest(data, iad)); - } - @Override public String getDefaultARPCRequest(boolean approved) { return approved ? "0012" : "9900"; } + @Override + public PaddingMethod getPaddingMethod() { + return PaddingMethod.ISO9797Method2; + } } diff --git a/jpos/src/main/java/org/jpos/emv/cryptogram/CryptogramDataBuilder.java b/jpos/src/main/java/org/jpos/emv/cryptogram/CryptogramDataBuilder.java index 5c48b5038a..f159e682ee 100644 --- a/jpos/src/main/java/org/jpos/emv/cryptogram/CryptogramDataBuilder.java +++ b/jpos/src/main/java/org/jpos/emv/cryptogram/CryptogramDataBuilder.java @@ -33,35 +33,53 @@ */ public interface CryptogramDataBuilder { - final PaddingMethod NO_PADDING = data -> data; - - /** - * ISO/IEC 9797-1 padding method 1 - * for Block size 8, n = 64 - */ - final PaddingMethod ISO9797Method1 = data -> data.isEmpty() ? - "0000000000000000" : - ISOUtil.zeropadRight(data, data.length() % 16 == 0 ? data.length() : data.length() + 16 - data.length() % 16); - - /** - * ISO/IEC 9797-1 padding method 2 - * for Block size 8, n = 64 - */ - final PaddingMethod ISO9797Method2 = data -> ISO9797Method1.apply(data + "80"); - - /** - * ISO/IEC 9797-1 padding method 3 - * for Block size 8, n = 64 - */ - final PaddingMethod ISO9797Method3 = data -> { - StringBuilder sb = new StringBuilder(); - String D = ISO9797Method1.apply(data); - String Ld = ISOUtil.byte2hex(ISOUtil.int2byte(data.length() / 2)); - String Lp = ISO9797Method1.apply(Ld); - Lp = Ld.length() % 16 == 0 ? "" : Lp.substring(Ld.length()); - return sb.append(Lp).append(Ld).append(D).toString(); - }; - + enum PaddingMethod { + NO_PADDING, + + /** + * ISO/IEC 9797-1 padding method 1 + * for Block size 8, n = 64 + */ + ISO9797Method1 { + @Override + public String apply(String data) { + return data.isEmpty() ? + "0000000000000000" : + ISOUtil.zeropadRight(data, data.length() % 16 == 0 ? data.length() : data.length() + 16 - data.length() % 16); + } + }, + + /** + * ISO/IEC 9797-1 padding method 2 + * for Block size 8, n = 64 + */ + ISO9797Method2 { + @Override + public String apply(String data) { + return ISO9797Method1.apply(data + "80"); + } + }, + + /** + * ISO/IEC 9797-1 padding method 3 + * for Block size 8, n = 64 + */ + ISO9797Method3 { + @Override + public String apply(String data) { + StringBuilder sb = new StringBuilder(); + String D = ISO9797Method1.apply(data); + String Ld = ISOUtil.byte2hex(ISOUtil.int2byte(data.length() / 2)); + String Lp = ISO9797Method1.apply(Ld); + Lp = Ld.length() % 16 == 0 ? "" : Lp.substring(Ld.length()); + return sb.append(Lp).append(Ld).append(D).toString(); + } + }; + + public String apply(String data) { + return data; + } + } /** * Method that selects the minimum set of data elements recommended for @@ -86,7 +104,6 @@ static List minimumSetOfDataElement(TLVList data) { ); } - /** * Method that returns default issuer response data (ARC or CSU) * @@ -105,22 +122,21 @@ static List minimumSetOfDataElement(TLVList data) { */ String buildARQCRequest(TLVList data, IssuerApplicationData iad); - /** * Select necessary data elements and create the string used to generate the ARQC with padding *

* * @param data ICC data received * @param iad Issuer application Data - * @param paddingMethod Padding method to use * @return String used to generate the ARQC */ - String buildARQCRequest_padded(TLVList data, IssuerApplicationData iad, PaddingMethod paddingMethod); + default String buildARQCRequest_padded(TLVList data, IssuerApplicationData iad) { + return getPaddingMethod().apply(buildARQCRequest(data, iad)); + } - /** - * Padding Method Interface + /** Defines how to pad the request data when generating the ARQC. + * @return PaddingMethod this builder uses */ - interface PaddingMethod { - String apply(String data); - } + PaddingMethod getPaddingMethod(); + } diff --git a/jpos/src/test/java/org/jpos/emv/cryptogram/CPACryptogramTest.java b/jpos/src/test/java/org/jpos/emv/cryptogram/CPACryptogramTest.java index 8abd1bc6f2..b1329e714a 100644 --- a/jpos/src/test/java/org/jpos/emv/cryptogram/CPACryptogramTest.java +++ b/jpos/src/test/java/org/jpos/emv/cryptogram/CPACryptogramTest.java @@ -12,10 +12,10 @@ public class CPACryptogramTest { @Test void test(){ CPACryptogram spec = new CPACryptogram(); - assertEquals(spec.paddingMethod, CryptogramDataBuilder.ISO9797Method2); assertEquals(spec.getMKDMethod(), MKDMethod.OPTION_A); assertEquals(spec.getSKDMethod(), SKDMethod.EMV_CSKD); assertEquals(spec.getARPCMethod(), ARPCMethod.METHOD_2); assertTrue(spec.getDataBuilder() instanceof CVNCPADataBuilder); + assertEquals(CryptogramDataBuilder.PaddingMethod.ISO9797Method2, spec.getDataBuilder().getPaddingMethod()); } } diff --git a/jpos/src/test/java/org/jpos/emv/cryptogram/CVN10DataBuilderTest.java b/jpos/src/test/java/org/jpos/emv/cryptogram/CVN10DataBuilderTest.java index 0373f5d3d7..495931b1c1 100644 --- a/jpos/src/test/java/org/jpos/emv/cryptogram/CVN10DataBuilderTest.java +++ b/jpos/src/test/java/org/jpos/emv/cryptogram/CVN10DataBuilderTest.java @@ -58,7 +58,7 @@ void testBuildARQCRequest() { assertEquals( "00000000010000000000000008400000000000084018123101ABCDEF101800000203000000000000", - builder.buildARQCRequest_padded(data, iad, builder.ISO9797Method1) + builder.buildARQCRequest_padded(data, iad) ); diff --git a/jpos/src/test/java/org/jpos/emv/cryptogram/CVN18DataBuilderTest.java b/jpos/src/test/java/org/jpos/emv/cryptogram/CVN18DataBuilderTest.java index 63f7c7905a..10a4e19def 100644 --- a/jpos/src/test/java/org/jpos/emv/cryptogram/CVN18DataBuilderTest.java +++ b/jpos/src/test/java/org/jpos/emv/cryptogram/CVN18DataBuilderTest.java @@ -58,7 +58,7 @@ void testBuildARQCRequest() { assertEquals( "00000000010000000000000008400000000000084018123101ABCDEF101800000106011203000000", - builder.buildARQCRequest_padded(data, iad, builder.ISO9797Method1) + builder.buildARQCRequest_padded(data, iad) ); diff --git a/jpos/src/test/java/org/jpos/emv/cryptogram/CVNCPADataBuilderTest.java b/jpos/src/test/java/org/jpos/emv/cryptogram/CVNCPADataBuilderTest.java index f1c8fb7331..5314e0f6bc 100644 --- a/jpos/src/test/java/org/jpos/emv/cryptogram/CVNCPADataBuilderTest.java +++ b/jpos/src/test/java/org/jpos/emv/cryptogram/CVNCPADataBuilderTest.java @@ -37,7 +37,7 @@ void testBuildARQCRequest() { assertEquals( "0000000100000000000010000840000000108008409807040011111111580034560FA500A03800000000000000000000000F01000000000000000000000000000080000000000000", - builder.buildARQCRequest_padded(data, iad, builder.ISO9797Method2) + builder.buildARQCRequest_padded(data, iad) ); } } diff --git a/jpos/src/test/java/org/jpos/emv/cryptogram/CryptogramDataBuilderTest.java b/jpos/src/test/java/org/jpos/emv/cryptogram/CryptogramDataBuilderTest.java index 8d255dec11..f127f97565 100644 --- a/jpos/src/test/java/org/jpos/emv/cryptogram/CryptogramDataBuilderTest.java +++ b/jpos/src/test/java/org/jpos/emv/cryptogram/CryptogramDataBuilderTest.java @@ -7,7 +7,7 @@ import java.util.Arrays; import java.util.Random; -import static org.jpos.emv.cryptogram.CryptogramDataBuilder.*; +import static org.jpos.emv.cryptogram.CryptogramDataBuilder.PaddingMethod.*; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals;