Advertisement
Peaser

credit card number validator

Jul 20th, 2015
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. #### NOTICE ####
  2. # All this code does is VALIDATE the number, checks if its real or not.
  3. # The generator creates a spoof number to test the validator.
  4. # Nothing illegal is happening here
  5.  
  6. import random
  7. def validate(creditcardnumber):
  8.     # Synopsis:
  9.     # Say you have a number...
  10.     creditcardnumber = [int(i) for i in creditcardnumber]
  11.     for k, v in enumerate(creditcardnumber):
  12.         if k % 2 == 0:
  13.         # Take every other digit from the right
  14.             creditcardnumber[k] = v * 2
  15.             # multiply it by two
  16.     # All added digits are now a sum of digits...
  17.     # that is to say
  18.     # 7 becomes 14, which becomes 1+4, which becomes 5.
  19.     finalstr = ''.join([str(i) for i in creditcardnumber])
  20.  
  21.     return not sum([int(i) for i in finalstr]) % 10
  22.     # If the sum of all the digits of the final number is a multiple of 10,
  23.     # It's valid!
  24.  
  25. def generate(cardCpny=None):
  26.     patterns = [
  27.         "4",                                  # 0, Visa
  28.         "5{0}".format(random.randrange(1,6)), # 1, Mastercard
  29.         random.choice(["6011", "644", "65"]), # 2, Discover
  30.         random.choice(["34", "37"])           # 3, American Express
  31.     ]
  32.     foundone = 0
  33.     while not foundone:
  34.         baseStr = [str(random.choice("0123456789")) for i in "_"*16]
  35.         if cardCpny != None:
  36.             for i, digit in enumerate(patterns[cardCpny]):
  37.                 baseStr[i] = digit # replace first several digits of base with company digits, if chosen.
  38.         baseStr = ''.join(baseStr)
  39.         if validate(baseStr):
  40.             foundone = 1
  41.             return baseStr
  42.  
  43. def test():
  44.     thing = {True: "yup.", False: "NOPE!! (This answer will never be printed)"}
  45.     example = generate(2) # discover
  46.     print "Generated #: "+ example
  47.     print "Is it valid? "+ thing[validate(example)]
  48.  
  49. def main():
  50.     test()
  51.  
  52. if __name__ == "__main__":
  53.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement