Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### NOTICE ####
- # All this code does is VALIDATE the number, checks if its real or not.
- # The generator creates a spoof number to test the validator.
- # Nothing illegal is happening here
- import random
- def validate(creditcardnumber):
- # Synopsis:
- # Say you have a number...
- creditcardnumber = [int(i) for i in creditcardnumber]
- for k, v in enumerate(creditcardnumber):
- if k % 2 == 0:
- # Take every other digit from the right
- creditcardnumber[k] = v * 2
- # multiply it by two
- # All added digits are now a sum of digits...
- # that is to say
- # 7 becomes 14, which becomes 1+4, which becomes 5.
- finalstr = ''.join([str(i) for i in creditcardnumber])
- return not sum([int(i) for i in finalstr]) % 10
- # If the sum of all the digits of the final number is a multiple of 10,
- # It's valid!
- def generate(cardCpny=None):
- patterns = [
- "4", # 0, Visa
- "5{0}".format(random.randrange(1,6)), # 1, Mastercard
- random.choice(["6011", "644", "65"]), # 2, Discover
- random.choice(["34", "37"]) # 3, American Express
- ]
- foundone = 0
- while not foundone:
- baseStr = [str(random.choice("0123456789")) for i in "_"*16]
- if cardCpny != None:
- for i, digit in enumerate(patterns[cardCpny]):
- baseStr[i] = digit # replace first several digits of base with company digits, if chosen.
- baseStr = ''.join(baseStr)
- if validate(baseStr):
- foundone = 1
- return baseStr
- def test():
- thing = {True: "yup.", False: "NOPE!! (This answer will never be printed)"}
- example = generate(2) # discover
- print "Generated #: "+ example
- print "Is it valid? "+ thing[validate(example)]
- def main():
- test()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement