|
| 1 | +import argparse # Import argparse for command-line argument parsing |
| 2 | +import re # Import re for regular expression matching |
| 3 | + |
| 4 | +# Validate credit card number using Luhn Algorithm |
| 5 | +def luhn_algorithm(card_number): |
| 6 | + def digits_of(n): |
| 7 | + return [int(d) for d in str(n)] # Convert each character in the number to an integer |
| 8 | + |
| 9 | + digits = digits_of(card_number) # Get all digits of the card number |
| 10 | + odd_digits = digits[-1::-2] # Get digits from the right, skipping one digit each time (odd positions) |
| 11 | + even_digits = digits[-2::-2] # Get every second digit from the right (even positions) |
| 12 | + |
| 13 | + checksum = sum(odd_digits) # Sum all odd position digits |
| 14 | + for d in even_digits: |
| 15 | + checksum += sum(digits_of(d*2)) # Double each even position digit and sum the resulting digits |
| 16 | + |
| 17 | + return checksum % 10 == 0 # Return True if checksum modulo 10 is 0 |
| 18 | + |
| 19 | + |
| 20 | +# Function to check credit card number using Luhn's alogorithm |
| 21 | +def check_credit_card_number(card_number): |
| 22 | + card_number = card_number.replace(' ', '') # Remove spaces from the card number |
| 23 | + if not card_number.isdigit(): # Check if the card number contains only digits |
| 24 | + return False |
| 25 | + return luhn_algorithm(card_number) # Validate using the Luhn algorithm |
| 26 | + |
| 27 | +# Function to get the card type based on card number using RegEx |
| 28 | +def get_card_type(card_number): |
| 29 | + card_number = card_number.replace(' ', '') # Remove spaces from the card number |
| 30 | + card_types = { |
| 31 | + "Visa": r"^4[0-9]{12}(?:[0-9]{3})?$", # Visa: Starts with 4, length 13 or 16 |
| 32 | + "MasterCard": r"^5[1-5][0-9]{14}$", # MasterCard: Starts with 51-55, length 16 |
| 33 | + "American Express": r"^3[47][0-9]{13}$", # AmEx: Starts with 34 or 37, length 15 |
| 34 | + "Discover": r"^6(?:011|5[0-9]{2})[0-9]{12}$", # Discover: Starts with 6011 or 65, length 16 |
| 35 | + "JCB": r"^(?:2131|1800|35\d{3})\d{11}$", # JCB: Starts with 2131, 1800, or 35, length 15 or 16 |
| 36 | + "Diners Club": r"^3(?:0[0-5]|[68][0-9])[0-9]{11}$", # Diners Club: Starts with 300-305, 36, or 38, length 14 |
| 37 | + "Maestro": r"^(5018|5020|5038|56|57|58|6304|6759|676[1-3])\d{8,15}$", # Maestro: Various starting patterns, length 12-19 |
| 38 | + "Verve": r"^(506[01]|507[89]|6500)\d{12,15}$" # Verve: Starts with 5060, 5061, 5078, 5079, or 6500, length 16-19 |
| 39 | + } |
| 40 | + |
| 41 | + for card_type, pattern in card_types.items(): |
| 42 | + if re.match(pattern, card_number): # Check if card number matches the pattern |
| 43 | + return card_type |
| 44 | + return "Unknown" # Return Unknown if no pattern matches |
| 45 | + |
| 46 | + |
| 47 | +# Processing a file containing card numbers. |
| 48 | +def process_file(file_path): |
| 49 | + |
| 50 | + try: |
| 51 | + with open(file_path, 'r') as file: # Open the file for reading |
| 52 | + card_numbers = file.readlines() # Read all lines from the file |
| 53 | + results = {} |
| 54 | + for card_number in card_numbers: |
| 55 | + card_number = card_number.strip() # Remove any leading/trailing whitespace |
| 56 | + is_valid = check_credit_card_number(card_number) # Validate card number |
| 57 | + card_type = get_card_type(card_number) # Detect card type |
| 58 | + results[card_number] = (is_valid, card_type) # Store result |
| 59 | + return results |
| 60 | + except Exception as e: |
| 61 | + print(f"Error reading file: {e}") # Print error message if file cannot be read |
| 62 | + return None |
| 63 | + |
| 64 | + |
| 65 | +def main(): |
| 66 | + parser = argparse.ArgumentParser(description="Check if a credit card number is legitimate and identify its type using the Luhn algorithm.") |
| 67 | + parser.add_argument('-n', '--number', type=str, help="A single credit card number to validate.") # Argument for single card number |
| 68 | + parser.add_argument('-f', '--file', type=str, help="A file containing multiple credit card numbers to validate.") # Argument for file input |
| 69 | + |
| 70 | + args = parser.parse_args() # Parse command-line arguments |
| 71 | + |
| 72 | + if args.number: |
| 73 | + is_valid = check_credit_card_number(args.number) # Validate single card number |
| 74 | + card_type = get_card_type(args.number) # Detect card type |
| 75 | + print(f"[!] Credit card number {args.number} is {'valid' if is_valid else 'invalid'} and is of type {card_type}.") # Print result |
| 76 | + |
| 77 | + if args.file: |
| 78 | + results = process_file(args.file) # Process file with card numbers |
| 79 | + if results: |
| 80 | + for card_number, (is_valid, card_type) in results.items(): |
| 81 | + print(f"[!] Credit card number {card_number} is {'valid' if is_valid else 'invalid'} and is of type {card_type}.") # Print results for each card number |
| 82 | + |
| 83 | +# Execute tha main function |
| 84 | +if __name__ == '__main__': |
| 85 | + main() |
0 commit comments