19 package org.sleuthkit.autopsy.keywordsearch;
21 import com.google.common.base.CharMatcher;
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit;
38 final class CreditCardValidator {
40 private CreditCardValidator() {
43 private static final LuhnCheckDigit CREDIT_CARD_NUM_LUHN_CHECK =
new LuhnCheckDigit();
55 static public boolean isValidCCN(String rawCCN) {
57 boolean hasSpace = StringUtils.contains(rawCCN,
' ');
58 boolean hasDash = StringUtils.contains(rawCCN,
'-');
59 if (hasSpace && hasDash) {
63 Character separator = null;
70 final String cannonicalCCN;
72 if (separator != null) {
74 cannonicalCCN = CharMatcher.anyOf(separator.toString()).removeFrom(rawCCN);
75 splitCCN = rawCCN.split(separator.toString());
78 cannonicalCCN = rawCCN;
79 splitCCN =
new String[]{cannonicalCCN};
83 switch (cannonicalCCN.length()) {
85 if (
false == isValid15DigitGrouping(splitCCN)) {
90 if (
false == isValid16DigitGrouping(splitCCN)) {
95 if (
false == isValid19DigitGrouping(splitCCN)) {
100 if (
false == isValidOtherDigitGrouping(splitCCN)) {
105 return CREDIT_CARD_NUM_LUHN_CHECK.isValid(cannonicalCCN);
108 static private boolean isValidOtherDigitGrouping(String[] splitCCN) {
109 if (splitCCN.length == 1) {
112 return splitCCN[0].length() == 4;
116 static private boolean isValid19DigitGrouping(String[] splitCCN) {
117 switch (splitCCN.length) {
121 return splitCCN[0].length() == 6
122 && splitCCN[1].length() == 13;
124 return splitCCN[0].length() == 4
125 && splitCCN[1].length() == 4
126 && splitCCN[2].length() == 4
127 && splitCCN[3].length() == 4
128 && splitCCN[4].length() == 3;
134 static private boolean isValid16DigitGrouping(String[] splitCCN) {
135 switch (splitCCN.length) {
139 return splitCCN[0].length() == 4
140 && splitCCN[1].length() == 4
141 && splitCCN[2].length() == 4
142 && splitCCN[3].length() == 4;
148 static private boolean isValid15DigitGrouping(String[] splitCCN) {
149 switch (splitCCN.length) {
153 return (splitCCN[0].length() == 4 && splitCCN[1].length() == 6 && splitCCN[2].length() == 5);