Autopsy  4.4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
CreditCardValidator.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2017 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.keywordsearch;
20 
21 import com.google.common.base.CharMatcher;
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit;
24 
38 final class CreditCardValidator {
39 
40  private CreditCardValidator() {
41  }
42 
43  private static final LuhnCheckDigit CREDIT_CARD_NUM_LUHN_CHECK = new LuhnCheckDigit();
44 
55  static public boolean isValidCCN(String rawCCN) {
56  //check for a valid separator
57  boolean hasSpace = StringUtils.contains(rawCCN, ' ');
58  boolean hasDash = StringUtils.contains(rawCCN, '-');
59  if (hasSpace && hasDash) {
60  return false; //can only have dashes or spaces, not both.
61  }
62 
63  Character separator = null;
64  if (hasSpace) {
65  separator = ' ';
66  } else if (hasDash) {
67  separator = '-';
68  }
69 
70  final String cannonicalCCN;
71  String[] splitCCN;
72  if (separator != null) {
73  //there is a seperator, strip if for canoncial form of CCN
74  cannonicalCCN = CharMatcher.anyOf(separator.toString()).removeFrom(rawCCN);
75  splitCCN = rawCCN.split(separator.toString());
76  } else {
77  //else use 'defualt'values
78  cannonicalCCN = rawCCN;
79  splitCCN = new String[]{cannonicalCCN};
80  }
81 
82  // validate digit grouping for 15, 16, and 19 digit cards
83  switch (cannonicalCCN.length()) {
84  case 15:
85  if (false == isValid15DigitGrouping(splitCCN)) {
86  return false;
87  }
88  break;
89  case 16:
90  if (false == isValid16DigitGrouping(splitCCN)) {
91  return false;
92  }
93  break;
94  case 19:
95  if (false == isValid19DigitGrouping(splitCCN)) {
96  return false;
97  }
98  break;
99  default:
100  if (false == isValidOtherDigitGrouping(splitCCN)) {
101  return false;
102  }
103  }
104 
105  return CREDIT_CARD_NUM_LUHN_CHECK.isValid(cannonicalCCN);
106  }
107 
108  static private boolean isValidOtherDigitGrouping(String[] splitCCN) {
109  if (splitCCN.length == 1) {
110  return true;
111  } else {
112  return splitCCN[0].length() == 4;
113  }
114  }
115 
116  static private boolean isValid19DigitGrouping(String[] splitCCN) {
117  switch (splitCCN.length) {
118  case 1:
119  return true;
120  case 2:
121  return splitCCN[0].length() == 6
122  && splitCCN[1].length() == 13;
123  case 5:
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;
129  default:
130  return false;
131  }
132  }
133 
134  static private boolean isValid16DigitGrouping(String[] splitCCN) {
135  switch (splitCCN.length) {
136  case 1:
137  return true;
138  case 4:
139  return splitCCN[0].length() == 4
140  && splitCCN[1].length() == 4
141  && splitCCN[2].length() == 4
142  && splitCCN[3].length() == 4;
143  default:
144  return false;
145  }
146  }
147 
148  static private boolean isValid15DigitGrouping(String[] splitCCN) {
149  switch (splitCCN.length) {
150  case 1:
151  return true;
152  case 3:
153  return (splitCCN[0].length() == 4 && splitCCN[1].length() == 6 && splitCCN[2].length() == 5);
154 // UATP || ((splitCCN[0].length() == 4 && splitCCN[1].length() == 5 && splitCCN[2].length() == 6));
155  default:
156  return false;
157  }
158  }
159 }

Copyright © 2012-2016 Basis Technology. Generated on: Fri Sep 29 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.