Skip to content

Commit 80ed139

Browse files
authored
Merge pull request #532 from fgonzal/master
Add constructors to parse Key Serial Numbers.
2 parents 9ec7f80 + dd17841 commit 80ed139

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

jpos/src/main/java/org/jpos/security/KeySerialNumber.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.jpos.security;
2020

21+
import org.jpos.iso.ISOUtil;
2122
import org.jpos.util.Loggeable;
2223

2324
import java.io.PrintStream;
@@ -68,6 +69,34 @@ public KeySerialNumber (String baseKeyID, String deviceID, String transactionCou
6869
setTransactionCounter(transactionCounter);
6970
}
7071

72+
/**
73+
* Constructs a key serial number object from its hexadecimal representation.
74+
* @param hexKSN hexadecimal representation of the KSN.
75+
* @param idLength length of the base key ID.
76+
* @param deviceLength length of the device ID.
77+
* @param counterLength length of the transaction counter.
78+
*/
79+
public KeySerialNumber(String hexKSN, int idLength, int deviceLength, int counterLength) {
80+
if (hexKSN == null || hexKSN.trim().length() == 0)
81+
throw new IllegalArgumentException("KSN cannot be empty.");
82+
if (idLength + deviceLength + counterLength > hexKSN.length())
83+
throw new IllegalArgumentException("Length spec doesn't match KSN.");
84+
setBaseKeyID(hexKSN.substring(0, idLength));
85+
setDeviceID(hexKSN.substring(idLength, idLength + deviceLength));
86+
setTransactionCounter(hexKSN.substring(idLength + deviceLength, idLength + deviceLength + counterLength));
87+
}
88+
89+
/**
90+
* Constructs a key serial number object from its binary representation.
91+
* @param binKSN binary representation of the KSN.
92+
* @param idLength length of the base key ID.
93+
* @param deviceLength length of the device ID.
94+
* @param counterLength length of the transaction counter.
95+
*/
96+
public KeySerialNumber(byte[] binKSN, int idLength, int deviceLength, int counterLength) {
97+
this(ISOUtil.byte2hex(binKSN).toUpperCase(), idLength, deviceLength, counterLength);
98+
}
99+
71100
/**
72101
*
73102
* @param baseKeyID a HexString representing the BaseKeyID (also called KeySet ID)

jpos/src/test/java/org/jpos/security/KeySerialNumberTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost;
2323
import static org.junit.jupiter.api.Assertions.assertEquals;
2424
import static org.junit.jupiter.api.Assertions.assertNull;
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
2526
import static org.junit.jupiter.api.Assertions.assertTrue;
2627
import static org.junit.jupiter.api.Assertions.fail;
2728

2829
import java.io.ByteArrayOutputStream;
2930
import java.io.PrintStream;
3031

32+
import org.jpos.iso.ISOUtil;
3133
import org.junit.jupiter.api.Test;
3234

3335
public class KeySerialNumberTest {
@@ -123,4 +125,43 @@ public void testSetTransactionCounter() throws Throwable {
123125
assertEquals("testKeySerialNumberTransactionCounter1",
124126
keySerialNumber.transactionCounter, "keySerialNumber.transactionCounter");
125127
}
128+
129+
@Test
130+
public void testBinaryConstructor() {
131+
byte[] ksnBin = ISOUtil.hex2byte("9876543210E00008");
132+
KeySerialNumber ksn = new KeySerialNumber(ksnBin, 6, 5, 5);
133+
assertEquals("987654", ksn.getBaseKeyID());
134+
assertEquals("3210E", ksn.getDeviceID());
135+
assertEquals("00008", ksn.getTransactionCounter());
136+
}
137+
138+
@Test
139+
public void testHexConstructor() {
140+
String ksnHex = "9876543210E00008";
141+
KeySerialNumber ksn = new KeySerialNumber(ksnHex, 6, 5, 5);
142+
assertEquals("987654", ksn.getBaseKeyID());
143+
assertEquals("3210E", ksn.getDeviceID());
144+
assertEquals("00008", ksn.getTransactionCounter());
145+
}
146+
147+
@Test
148+
public void testHexConstructorWrongLength() {
149+
assertThrows(IllegalArgumentException.class, () -> {
150+
new KeySerialNumber("9876543210E008", 6, 5, 5);
151+
});
152+
}
153+
154+
@Test
155+
public void testHexConstructorNullKSN() {
156+
assertThrows(IllegalArgumentException.class, () -> {
157+
new KeySerialNumber((String) null, 6, 5, 5);
158+
});
159+
}
160+
161+
@Test
162+
public void testHexConstructorEmptyKSN() {
163+
assertThrows(IllegalArgumentException.class, () -> {
164+
new KeySerialNumber(" ", 6, 5, 5);
165+
});
166+
}
126167
}

0 commit comments

Comments
 (0)