def validate_number(self, field): error = validators.ValidationError("Invalid card number.") try: if not verify(field.data.replace('-', '')): raise error except: raise error
def search_string(self, string): matches = [] for match in self.reqex.finditer(string): card_number = match.group(0) if baluhn.verify(card_number): if not (self.ignore_test_card_numbers and card_number in test_card_numbers): matches.append(match) return matches
def generate_pan(pan_len=16): prefix = '500000' # not to mess with real world cards base = prefix + str( random.randint(10**(pan_len - len(prefix) - 2), 10**(pan_len - len(prefix) - 1) - 1)) pan = base + baluhn.generate(base) assert baluhn.verify(pan) #print "pan=%s" % pan return pan
def generate_pan(pan_len=16): prefix = '500000' # not to mess with real world cards base = prefix + str(random.randint( 10 ** (pan_len - len(prefix) - 2), 10 ** (pan_len - len(prefix) - 1) - 1)) pan = base + baluhn.generate(base) assert baluhn.verify(pan) #print "pan=%s" % pan return pan
def verify_npi(number): prefixed_number = "%s%s" % (LUHN_PREFIX, number) return verify(prefixed_number)
def validate_pjson(j, action): """ Input a JSON object and an action. return a list of errors. If error list is empty then the file is valid. """ action = action.lower() errors = [] warnings = [] response = {"errors": [], "warnings": []} # Check the action if action not in ("create", "update", "public"): error = "action must be create, update, or public." errors.append(error) response["errors"] = errors return response # Does the string contain JSON try: d = json.loads(j) except: error = "The string did not contain valid JSON." errors.append(error) response["errors"] = errors return response # Is it a dict {} (JSON object equiv)? if not isinstance(d, type({})): error = "The JSON string did not contain a JSON object i.e. {}." errors.append(error) response["errors"] = errors return response # Does it contain the top-level enumeration_type if "enumeration_type" not in d and action != "public": error = "The JSON object does not contain an enumeration_type." errors.append(error) response["errors"] = errors return response # Check for deactivation if "number" in d and not d.get( "enumeration_type", "") and action == "public": warning = "This appears to be a deactivated NPI. No information is available for deactivated NPIs." warnings.append(warning) response["warnings"] = warnings return response # Is the enumeration_type a valid? if d["enumeration_type"] not in ("NPI-1", "NPI-2", "OEID", "HPID"): error = "enumeration_type must be one of these: ('NPI-1', 'NPI-2', 'OEID', 'HPID')" errors.append(error) response["errors"] = errors return response # If a number is present we assume this is an update. if "number" not in d: number = None else: number = d['number'] if action == "create" and number: warning = "enumeration number is generated by CMS. The provided value will be ignored." warnings.append(warning) if action == "update" and not number: error = "enumeration number is required when performaing an update." errors.append(error) # Check if the Luhn checkdigit makes is correcense. if number and d['enumeration_type'] in ('NPI-1', 'NPI-2'): prefixed_number = "%s%s" % (LUHN_PREFIX, number) luhn_verified = verify(prefixed_number) if not luhn_verified: warning = "The number %s did not pass Luhn algorithm check digit sanitiy check." % ( number) warnings.append(warning) # Check for errors in the basic section basic_errors, basic_warnings = validate_basic_dict(d.get('basic', {}), d.get( 'enumeration_type'), action, number) # Check for errors in the addresses address_errors = validate_address_list( d.get('addresses', ()), d.get('enumeration_type')) # Check for errors in the license section if 'licenses' in d: license_errors = validate_license_list( d.get('licenses', []), d.get('enumeration_type'), action) else: license_errors = [] taxonomy_errors = validate_taxonomy_list( d.get( 'taxonomies', ()), d.get( 'enumeration_type', ()), d.get( 'licenses', []), d.get( 'taxonomy_licenses', []), d.get( 'basic', {}).get( 'sole_proprietor', "NO"), action) if 'identifiers' in d: identifier_errors = validate_identifier_list( d.get('identifiers', []), d.get('enumeration_type')) else: identifier_errors = [] if 'other_names' in d: other_names_errors = validate_other_name_list(d.get('other_names', []), d.get( 'enumeration_type'), d.get('basic', {})) else: other_names_errors = [] affiliation_errors, affiliation_warnings = validate_affiliation_list( d.get('affiliations', []), d.get('enumeration_type')) errors = errors + basic_errors + other_names_errors + address_errors + \ license_errors + taxonomy_errors + identifier_errors + affiliation_errors warnings = warnings + basic_warnings response["errors"] = errors response["warnings"] = warnings return response
def validate_pjson(j, action): """ Input a JSON object and an action. return a list of errors. If error list is empty then the file is valid. """ action = action.lower() errors = [] warnings = [] response = {"errors": [], "warnings": []} # Check the action if action not in ("create", "update", "public"): error = "action must be create, update, or public." errors.append(error) response["errors"] = errors return response # Does the string contain JSON try: d = json.loads(j) except: error = "The string did not contain valid JSON." errors.append(error) response["errors"] = errors return response # Is it a dict {} (JSON object equiv)? if not isinstance(d, type({})): error = "The JSON string did not contain a JSON object i.e. {}." errors.append(error) response["errors"] = errors return response # Does it contain the top-level enumeration_type if "enumeration_type" not in d and action != "public": error = "The JSON object does not contain an enumeration_type." errors.append(error) response["errors"] = errors return response # Check for deactivation if "number" in d and not d.get("enumeration_type", "") and action == "public": warning = "This appears to be a deactivated NPI. No information is available for deactivated NPIs." warnings.append(warning) response["warnings"] = warnings return response # Is the enumeration_type a valid? if d["enumeration_type"] not in ("NPI-1", "NPI-2", "OEID", "HPID"): error = "enumeration_type must be one of these: ('NPI-1', 'NPI-2', 'OEID', 'HPID')" errors.append(error) response["errors"] = errors return response # If a number is present we assume this is an update. if "number" not in d: number = None else: number = d['number'] if action == "create" and number: warning = "enumeration number is generated by CMS. The provided value will be ignored." warnings.append(warning) if action == "update" and not number: error = "enumeration number is required when performaing an update." errors.append(error) # Check if the Luhn checkdigit makes is correcense. if number and d['enumeration_type'] in ('NPI-1', 'NPI-2'): prefixed_number = "%s%s" % (LUHN_PREFIX, number) luhn_verified = verify(prefixed_number) if not luhn_verified: warning = "The number %s did not pass Luhn algorithm check digit sanitiy check." % ( number) warnings.append(warning) # Check for errors in the basic section basic_errors, basic_warnings = validate_basic_dict( d.get('basic', {}), d.get('enumeration_type'), action, number) # Check for errors in the addresses address_errors = validate_address_list(d.get('addresses', ()), d.get('enumeration_type')) # Check for errors in the license section if 'licenses' in d: license_errors = validate_license_list(d.get('licenses', []), d.get('enumeration_type'), action) else: license_errors = [] taxonomy_errors = validate_taxonomy_list( d.get('taxonomies', ()), d.get('enumeration_type', ()), d.get('licenses', []), d.get('taxonomy_licenses', []), d.get('basic', {}).get('sole_proprietor', "NO"), action) if 'identifiers' in d: identifier_errors = validate_identifier_list(d.get('identifiers', []), d.get('enumeration_type')) else: identifier_errors = [] if 'other_names' in d: other_names_errors = validate_other_name_list( d.get('other_names', []), d.get('enumeration_type'), d.get('basic', {})) else: other_names_errors = [] affiliation_errors, affiliation_warnings = validate_affiliation_list( d.get('affiliations', []), d.get('enumeration_type')) errors = errors + basic_errors + other_names_errors + address_errors + \ license_errors + taxonomy_errors + identifier_errors + affiliation_errors warnings = warnings + basic_warnings response["errors"] = errors response["warnings"] = warnings return response
def validate_affiliation_list(l, enumeration_type): errors = [] warnings = [] primary_count = 0 max_values = { 'purpose': 25, 'affiliation_data_type': 10, 'endpoint_data_type': 50, 'affiliation_identifier': 1024, 'endpoint': 1024, 'description': 1024, 'state': 2, } i=0 for d in l: affiliation_string = "Affiliation %s: " % (i) for k in max_values.keys(): if d.get(k): if max_values[k] < len(d.get(k,"").encode('ascii', 'ignore').decode('ascii')): error = "%s : %s exceeds max allowable length of %s." % (affiliation_string, k, max_values[k]) errors.append(error) #check for required information if d.get('purpose') not in AFFILIATION_PURPOSE: error = "%s purpose %s is not a valid value. Valid values are %s." % ( affiliation_string, d.get('purpose'), AFFILIATION_PURPOSE) errors.append(error) if d.get('affiliation_data_type') not in AFFILIATION_DATA_TYPE: error = "%s affiliation_data_type %s is not a valid value. Valid values are %s." % ( affiliation_string, d.get('affiliation_data_type'), AFFILIATION_DATA_TYPE) errors.append(error) if not str(d.get('affiliation_data_type')) and \ d.get('purpose') not in AFFILIATION_PURPOSE: error = "%s affiliation_data_type %s is required when purpose is one of these %s" % ( affiliation_string, d.get('affiliation_data_type'), AFFILIATION_PURPOSE) errors.append(error) if d.get('endpoint_data_type') and d.get('endpoint_data_type') not in ENDPOINT_DATA_TYPE: error = "%s endpoint_data_type %s is not a valid value. Valid values are %s." % \ (affiliation_string, d.get('endpoint_data_type', ''), ENDPOINT_DATA_TYPE) errors.append(error) if d.get('purpose', '') == "HIE-EXCHANGE" and not d.get('endpoint_data_type', ''): error = "%s endpoint_data_type is required when the purpose is HIE-EXCHANGE." % \ (affiliation_string) errors.append(error) if not d.get('affiliation_identifier', '') and \ d.get('affiliation_data_type', '') in AFFILIATION_DATA_TYPE: error = "%s affiliation_identifier is required when affiliation_data_type is in %s." % \ (affiliation_string, AFFILIATION_DATA_TYPE) errors.append(error) if not d.get('endpoint') and d.get('purpose') in ("HIE-EXCHANGE", "DOMAIN"): error = "%s endpoint is required when purpose is in %s." % \ (affiliation_string, ("HIE-EXCHANGE", "DOMAIN")) errors.append(error) if d.get('accepting_new_patients', None) not in (True, False, None): error = "%s accepting_new_patients must be boolean. i.e. true or false." % \ (affiliation_string) errors.append(error) if d.get('for_additional_documentation_request', None) not in (True, False, None): error = "%s for_additional_documentation_request must be boolean. i.e. true or false." % \ (affiliation_string) errors.append(error) if d.get('purpose') == "MEDICAID-NETWORK" and not d.get('state'): error = "%s state is required when purpose = MEDICIAD-NETWORK." % \ (affiliation_string) errors.append(error) if d.get('state') and d.get('state') not in STATES: error = "%s state %s is not a valid value. Valid values are %s." % \ (affiliation_string, d.get('state'), STATES) errors.append(error) if d.get('affiliation_data_type') in ('NPI-1', 'NPI-2'): prefixed_number = "%s%s" % (LUHN_PREFIX, d['affiliation_identifier']) luhn_verified = verify(prefixed_number) if not luhn_verified: error ="The NPI affiliation_identifier %s did not pass Luhn algorithm check digit sanitiy check." % (d['affiliation_identifier']) errors.append(error) if d.get('endpoint_data_type') in ('DIRECT-EMAIL-ADDRESS', 'REGULAR-EMAIL-ADDRESS'): is_valid = validate_email(d.get('endpoint')) if not is_valid: error = "%s %s has and endpoint_data_type of %s and is not a valid email." % \ (affiliation_string, d.get('endpoint'), d.get('endpoint_data_type') ) errors.append(error) i += 1 retval = [errors, warnings] return retval
visa = re.compile(r'4[0-9]{12}(?:[0-9]{3})?') mastercard = re.compile(r'5[1-5][0-9]{14}') amex = re.compile(r'3[47][0-9]{13}') diners = re.compile(r'3(?:0[0-5]|[68][0-9])[0-9]{11}') discover = re.compile(r'6(?:011|5[0-9]{2})[0-9]{12}') jcb = re.compile(r'(?:2131|1800|35[0-9]{3})[0-9]{11}') for root, dirs, files in os.walk(rootPath): for filename in fnmatch.filter(files, pattern): cardNumbers = open((os.path.join(root, filename)), "r", errors='ignore', encoding='utf-8') for line in cardNumbers: line = line.rstrip() cardSearch = visa.findall(line) for theCard in cardSearch: if verify(theCard): print("\n"+os.path.join(root, filename)) print("Visa "+theCard) cardSearch = mastercard.findall(line) for theCard in cardSearch: if verify(theCard): print("\n"+os.path.join(root, filename)) print("Mastercard "+theCard) cardSearch = amex.findall(line) for theCard in cardSearch: if verify(theCard): print("\n"+os.path.join(root, filename)) print("American Express "+theCard)
diners = re.compile(r'3(?:0[0-5]|[68][0-9])[0-9]{11}') discover = re.compile(r'6(?:011|5[0-9]{2})[0-9]{12}') jcb = re.compile(r'(?:2131|1800|35[0-9]{3})[0-9]{11}') for root, dirs, files in os.walk(rootPath): for filename in fnmatch.filter(files, pattern): cardNumbers = open((os.path.join(root, filename)), "r", errors='ignore', encoding='utf-8') for line in cardNumbers: line = line.rstrip() cardSearch = visa.findall(line) for theCard in cardSearch: if verify(theCard): print("\n" + os.path.join(root, filename)) print("Visa " + theCard) cardSearch = mastercard.findall(line) for theCard in cardSearch: if verify(theCard): print("\n" + os.path.join(root, filename)) print("Mastercard " + theCard) cardSearch = amex.findall(line) for theCard in cardSearch: if verify(theCard): print("\n" + os.path.join(root, filename)) print("American Express " + theCard)
def validate_affiliation_list(l, enumeration_type): errors = [] warnings = [] primary_count = 0 max_values = { 'purpose': 25, 'affiliation_data_type': 10, 'endpoint_data_type': 50, 'affiliation_identifier': 1024, 'endpoint': 1024, 'description': 1024, 'state': 2, } i = 0 for d in l: affiliation_string = "Affiliation %s: " % (i) for k in max_values.keys(): if d.get(k): if max_values[k] < len( d.get( k, "").encode( 'ascii', 'ignore').decode('ascii')): error = "%s : %s exceeds max allowable length of %s." % ( affiliation_string, k, max_values[k]) errors.append(error) # check for required information if d.get('purpose') not in AFFILIATION_PURPOSE: error = "%s purpose %s is not a valid value. Valid values are %s." % ( affiliation_string, d.get('purpose'), AFFILIATION_PURPOSE) errors.append(error) if d.get('affiliation_data_type') not in AFFILIATION_DATA_TYPE: error = "%s affiliation_data_type %s is not a valid value. Valid values are %s." % ( affiliation_string, d.get('affiliation_data_type'), AFFILIATION_DATA_TYPE) errors.append(error) if not str(d.get('affiliation_data_type')) and \ d.get('purpose') not in AFFILIATION_PURPOSE: error = "%s affiliation_data_type %s is required when purpose is one of these %s" % ( affiliation_string, d.get('affiliation_data_type'), AFFILIATION_PURPOSE) errors.append(error) if d.get('endpoint_data_type') and d.get( 'endpoint_data_type') not in ENDPOINT_DATA_TYPE: error = "%s endpoint_data_type %s is not a valid value. Valid values are %s." % ( affiliation_string, d.get('endpoint_data_type', ''), ENDPOINT_DATA_TYPE) errors.append(error) if d.get('purpose', '') == "HIE-EXCHANGE" and not d.get('endpoint_data_type', ''): error = "%s endpoint_data_type is required when the purpose is HIE-EXCHANGE." % \ (affiliation_string) errors.append(error) if not d.get('affiliation_identifier', '') and \ d.get('affiliation_data_type', '') in AFFILIATION_DATA_TYPE: error = "%s affiliation_identifier is required when affiliation_data_type is in %s." % ( affiliation_string, AFFILIATION_DATA_TYPE) errors.append(error) if not d.get('endpoint') and d.get( 'purpose') in ("HIE-EXCHANGE", "DOMAIN"): error = "%s endpoint is required when purpose is in %s." % \ (affiliation_string, ("HIE-EXCHANGE", "DOMAIN")) errors.append(error) if d.get('accepting_new_patients', None) not in (True, False, None): error = "%s accepting_new_patients must be boolean. i.e. true or false." % ( affiliation_string) errors.append(error) if d.get( 'for_additional_documentation_request', None) not in ( True, False, None): error = "%s for_additional_documentation_request must be boolean. i.e. true or false." % ( affiliation_string) errors.append(error) if d.get('purpose') == "MEDICAID-NETWORK" and not d.get('state'): error = "%s state is required when purpose = MEDICIAD-NETWORK." % \ (affiliation_string) errors.append(error) if d.get('state') and d.get('state') not in STATES: error = "%s state %s is not a valid value. Valid values are %s." % ( affiliation_string, d.get('state'), STATES) errors.append(error) if d.get('affiliation_data_type') in ('NPI-1', 'NPI-2'): prefixed_number = "%s%s" % ( LUHN_PREFIX, d['affiliation_identifier']) luhn_verified = verify(prefixed_number) if not luhn_verified: error = "The NPI affiliation_identifier %s did not pass Luhn algorithm check digit sanitiy check." % (d[ 'affiliation_identifier']) errors.append(error) if d.get('endpoint_data_type') in ( 'DIRECT-EMAIL-ADDRESS', 'REGULAR-EMAIL-ADDRESS'): is_valid = validate_email(d.get('endpoint')) if not is_valid: error = "%s %s has and endpoint_data_type of %s and is not a valid email." % ( affiliation_string, d.get('endpoint'), d.get('endpoint_data_type')) errors.append(error) i += 1 retval = [errors, warnings] return retval
def validateCC(cc): if not verify(cc): click.echo("ERROR: This card is invalid") sys.exit() return True