Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constructors to parse Key Serial Numbers. #532

Merged
merged 2 commits into from
May 6, 2023
Merged
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
29 changes: 29 additions & 0 deletions jpos/src/main/java/org/jpos/security/KeySerialNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.jpos.security;

import org.jpos.iso.ISOUtil;
import org.jpos.util.Loggeable;

import java.io.PrintStream;
Expand Down Expand Up @@ -68,6 +69,34 @@ public KeySerialNumber (String baseKeyID, String deviceID, String transactionCou
setTransactionCounter(transactionCounter);
}

/**
* Constructs a key serial number object from its hexadecimal representation.
* @param hexKSN hexadecimal representation of the KSN.
* @param idLength length of the base key ID.
* @param deviceLength length of the device ID.
* @param counterLength length of the transaction counter.
*/
public KeySerialNumber(String hexKSN, int idLength, int deviceLength, int counterLength) {
if (hexKSN == null || hexKSN.trim().length() == 0)
throw new IllegalArgumentException("KSN cannot be empty.");
if (idLength + deviceLength + counterLength > hexKSN.length())
throw new IllegalArgumentException("Length spec doesn't match KSN.");
setBaseKeyID(hexKSN.substring(0, idLength));
setDeviceID(hexKSN.substring(idLength, idLength + deviceLength));
setTransactionCounter(hexKSN.substring(idLength + deviceLength, idLength + deviceLength + counterLength));
}

/**
* Constructs a key serial number object from its binary representation.
* @param binKSN binary representation of the KSN.
* @param idLength length of the base key ID.
* @param deviceLength length of the device ID.
* @param counterLength length of the transaction counter.
*/
public KeySerialNumber(byte[] binKSN, int idLength, int deviceLength, int counterLength) {
this(ISOUtil.byte2hex(binKSN).toUpperCase(), idLength, deviceLength, counterLength);
}

/**
*
* @param baseKeyID a HexString representing the BaseKeyID (also called KeySet ID)
Expand Down
41 changes: 41 additions & 0 deletions jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.jpos.iso.ISOUtil;
import org.junit.jupiter.api.Test;

public class KeySerialNumberTest {
Expand Down Expand Up @@ -123,4 +125,43 @@ public void testSetTransactionCounter() throws Throwable {
assertEquals("testKeySerialNumberTransactionCounter1",
keySerialNumber.transactionCounter, "keySerialNumber.transactionCounter");
}

@Test
public void testBinaryConstructor() {
byte[] ksnBin = ISOUtil.hex2byte("9876543210E00008");
KeySerialNumber ksn = new KeySerialNumber(ksnBin, 6, 5, 5);
assertEquals("987654", ksn.getBaseKeyID());
assertEquals("3210E", ksn.getDeviceID());
assertEquals("00008", ksn.getTransactionCounter());
}

@Test
public void testHexConstructor() {
String ksnHex = "9876543210E00008";
KeySerialNumber ksn = new KeySerialNumber(ksnHex, 6, 5, 5);
assertEquals("987654", ksn.getBaseKeyID());
assertEquals("3210E", ksn.getDeviceID());
assertEquals("00008", ksn.getTransactionCounter());
}

@Test
public void testHexConstructorWrongLength() {
assertThrows(IllegalArgumentException.class, () -> {
new KeySerialNumber("9876543210E008", 6, 5, 5);
});
}

@Test
public void testHexConstructorNullKSN() {
assertThrows(IllegalArgumentException.class, () -> {
new KeySerialNumber((String) null, 6, 5, 5);
});
}

@Test
public void testHexConstructorEmptyKSN() {
assertThrows(IllegalArgumentException.class, () -> {
new KeySerialNumber(" ", 6, 5, 5);
});
}
}