Skip to content

Commit

Permalink
Merge pull request jpos#532 from fgonzal/master
Browse files Browse the repository at this point in the history
Add constructors to parse Key Serial Numbers.
  • Loading branch information
ar authored May 6, 2023
2 parents 9ec7f80 + dd17841 commit 80ed139
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
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);
});
}
}

0 comments on commit 80ed139

Please sign in to comment.