Skip to content

Commit

Permalink
properly protect pan
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Jun 17, 2024
1 parent e776db2 commit 745b257
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
17 changes: 9 additions & 8 deletions jpos/src/main/java/org/jpos/iso/ISOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -906,16 +906,16 @@ public static String normalize (String s) {

public static String protect(String s, char mask) {
// Validation for minimum length
StringBuilder ps = new StringBuilder(s);
if (s.length() <= 4) {
char[] maskedArray = new char[s.length()];
Arrays.fill(maskedArray, mask);// 6 (BIN) + 4 (last digits) = 10
return new String(maskedArray);
}
StringBuilder ps = new StringBuilder(s);

// Identify the positions of separators (^ and =)
String separator = s.contains("^") ? "^" : "=";
int firstSeparatorIndex = ps.indexOf(separator);
String separator = s.contains("^") ? "^" : s.contains("=") ? "=" : null;
int firstSeparatorIndex = separator != null ? ps.indexOf(separator) : s.length();
if (firstSeparatorIndex < 6) {
return s; // nothing to do
}
Expand All @@ -925,15 +925,16 @@ public static String protect(String s, char mask) {
for (int i = 6; i < lastDigitIndex; i++) {
ps.setCharAt(i, mask);
}
for (int i = firstSeparatorIndex + 1; i < ps.length(); i++) {
char c = ps.charAt(i);
if ((c != '=' && c != '^')) {
ps.setCharAt(i, mask);
if (separator != null) {
for (int i = firstSeparatorIndex + 1; i < ps.length(); i++) {
char c = ps.charAt(i);
if ((c != '=' && c != '^')) {
ps.setCharAt(i, mask);
}
}
}
return ps.toString();
}

public static String protect(String s) {
return protect(s, '_');
}
Expand Down
6 changes: 6 additions & 0 deletions jpos/src/test/java/org/jpos/iso/ISOUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4238,6 +4238,12 @@ public void testProtect5() throws Throwable {
assertEquals("", result, "result");
}

@Test
public void testPan() throws Throwable {
String result = ISOUtil.protect("4111111111111111");
assertEquals("411111______1111", result, "result");
}


@Test
public void testProtectT1D3() throws Throwable {
Expand Down

0 comments on commit 745b257

Please sign in to comment.