Skip to content

Instantly share code, notes, and snippets.

@nopslider
Created November 3, 2018 12:33
Show Gist options
  • Save nopslider/c2125c8f31d787c639f85bb68a4afca4 to your computer and use it in GitHub Desktop.
Save nopslider/c2125c8f31d787c639f85bb68a4afca4 to your computer and use it in GitHub Desktop.
import re
import sys
def luhn_checksum(card_number):
def digits_of(n):
return [int(d) for d in str(n)]
digits = digits_of(card_number)
odd_digits = digits[-1::-2]
even_digits = digits[-2::-2]
checksum = 0
checksum += sum(odd_digits)
for d in even_digits:
checksum += sum(digits_of(d*2))
return checksum % 10
def is_luhn_valid(card_number):
return luhn_checksum(card_number) == 0
count = 0
pattern = re.compile(r'\b[0-9]{13,16}\b')
for line in open(sys.argv[1]):
matches = re.findall(pattern,line)
for match in matches:
if is_luhn_valid(match):
count = count + 1
print(match)
if count > 0:
print("{0},{1}".format(sys.argv[1],count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment