From 822b72a96c0b3ad37de4629790f879d6eec01474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Federico=20Gonz=C3=A1lez?= Date: Thu, 7 Nov 2024 12:30:41 -0300 Subject: [PATCH] Move `padISO9797Method2` from `JCESecurityModule` to `ISOUtil`. --- jpos/src/main/java/org/jpos/iso/ISOUtil.java | 15 +++++++++++ .../jceadapter/JCESecurityModule.java | 27 +++++-------------- .../test/java/org/jpos/iso/ISOUtilTest.java | 10 +++++++ .../jceadapter/JCESecurityModuleTest.java | 18 +------------ 4 files changed, 32 insertions(+), 38 deletions(-) diff --git a/jpos/src/main/java/org/jpos/iso/ISOUtil.java b/jpos/src/main/java/org/jpos/iso/ISOUtil.java index f056358df5..dedc22b2a9 100644 --- a/jpos/src/main/java/org/jpos/iso/ISOUtil.java +++ b/jpos/src/main/java/org/jpos/iso/ISOUtil.java @@ -210,6 +210,21 @@ public static String zeropadRight (String s, int len) { d.append('0'); return d.toString(); } + + /** + * Pads {@code data} as per ISO/IEC 9797-1, method 2. + * @param data Data to be padded. + * @return Returns {@code data} padded as per ISO/IEC 9797-1, method 2. + */ + public static byte[] padISO9797Method2(byte[] data) { + byte[] t = new byte[data.length - data.length % 8 + 8]; + System.arraycopy(data, 0, t, 0, data.length); + for (int i = data.length; i < t.length; i++) + t[i] = (byte) (i == data.length ? 0x80 : 0x00); + data = t; + return data; + } + /** * converts to BCD * @param s - the number diff --git a/jpos/src/main/java/org/jpos/security/jceadapter/JCESecurityModule.java b/jpos/src/main/java/org/jpos/security/jceadapter/JCESecurityModule.java index 270283010f..4fa8b72551 100644 --- a/jpos/src/main/java/org/jpos/security/jceadapter/JCESecurityModule.java +++ b/jpos/src/main/java/org/jpos/security/jceadapter/JCESecurityModule.java @@ -551,26 +551,11 @@ protected boolean verifyCVC3Impl(SecureDESKey imkcvc3, String accountNo, String private byte[] calculateIVCVC3(Key mkcvc3, byte[] data) throws JCEHandlerException { - byte[] paddedData = paddingISO9797Method2(data); + byte[] paddedData = ISOUtil.padISO9797Method2(data); byte[] mac = calculateMACISO9797Alg3(mkcvc3, paddedData); return Arrays.copyOfRange(mac,6,8); } - /** - * ISO/IEC 9797-1 padding method 2 - * @param d da to be padded - * @return padded data - */ - private byte[] paddingISO9797Method2(byte[] d) { - //Padding - first byte 0x80 rest 0x00 - byte[] t = new byte[d.length - d.length%8 + 8]; - System.arraycopy(d, 0, t, 0, d.length); - for (int i=d.length;i