Skip to content

Commit

Permalink
Add: Add random Card Security Code
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonLzy committed Jul 19, 2023
1 parent 3f3a1c5 commit 5594baf
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 3 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,25 @@ f.gender()

f.industry()
# Software & Services

f.CVD()
# 252

f.CVV()
# 748

f.CID()
# 207

f.CAV()
# 459

f.CVC()
# 767

f.CSC()
# 3473

f.CVN()
# 541
```
8 changes: 8 additions & 0 deletions lfinancial/factory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from lfinancial.generators.bank import BankGenerator
from lfinancial.generators.card import CardGenerator
from lfinancial.generators.contry import CountryGenerator
from lfinancial.generators.currency import CurrencyGenerator
from lfinancial.generators.document import IDCodeGenerator
Expand Down Expand Up @@ -40,6 +41,13 @@ def __init__(self):
"product": StockGenerator(),
"gender": GenderGenerator(),
"industry": IndustryGenerator(),
"CSC": CardGenerator(),
"CID": CardGenerator(),
"CVC": CardGenerator(),
"CAV": CardGenerator(),
"CVV": CardGenerator(),
"CVD": CardGenerator(),
"CVN": CardGenerator(),
}

def register_generator(self, name, generator):
Expand Down
29 changes: 29 additions & 0 deletions lfinancial/financial.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ def gender(self, country=None):
def industry(self, country=None):
return self._generate("industry", country)

def CSC(self, country=None):
return self._generate("CSC", country)

def CID(self, country=None):
return self._generate("CID", country)

def CVC(self, country=None):
return self._generate("CVC", country)

def CAV(self, country=None):
return self._generate("CAV", country)

def CVV(self, country=None):
return self._generate("CVV", country)

def CVD(self, country=None):
return self._generate("CVD", country)

def CVN(self, country=None):
return self._generate("CVN", country)


if __name__ == '__main__':
f = Financial()
Expand Down Expand Up @@ -108,3 +129,11 @@ def industry(self, country=None):
print(f.product())
print(f.gender())
print(f.industry())
print(f.CVD())
print(f.CVV())
print(f.CID())
print(f.CAV())
print(f.CVC())
print(f.CSC())
print(f.CVN())

63 changes: 61 additions & 2 deletions lfinancial/generators/card.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@
import random


class CardGenerator:

def __init__(self):
self.default_country = {
"card": "US",
"CSC": "US"
}

def gen(self, card_type, country=None):
if country is None:
country = self.default_country.get(card_type)

match card_type:
case "bankcard":
doc_type = BankCard(country)
case "CSC":
doc_type = CSC("CSC")
case "CID":
doc_type = CSC("CID")
case "CVC":
doc_type = CSC("CVC")
case "CAV":
doc_type = CSC("CAV")
case "CVV":
doc_type = CSC("CVV")
case "CVD":
doc_type = CSC("CVD")
case "CVN":
doc_type = CSC("CVN")
case _:
raise ValueError(f"Unsupported card type: {card_type}.")
return doc_type.gen()


class CardType:
def __init__(self, name):
self.name = name

def gen(self):
raise NotImplementedError("Subclasses must implement gen method.")


class BankCard(CardType):
"""
https://en.wikipedia.org/wiki/Payment_card_number
"""
IIN = {
"American Express": [5610, [i for i in range(560221, 560225)]],
"American Express": [5610] + [i for i in range(560221, 560225)],
"Bankcard": [5610, 560221 - 560225],
"China T-Union": [5610, 560221 - 560225],
"China UnionPay": [5610, 560221 - 560225],
Expand Down Expand Up @@ -35,6 +80,11 @@ class CardGenerator:
"Napas": [5610, 560221 - 560225],
}

def gen(self):
return random.choice(self.IIN)


class CSC(CardType):
"""
https://zh.wikipedia.org/wiki/%E9%93%B6%E8%A1%8C%E5%8D%A1%E5%AE%89%E5%85%A8%E7%A0%81
"""
Expand All @@ -48,7 +98,16 @@ class CardGenerator:
"CVN": 3
}

def __init__(self, csc_name):
super().__init__(csc_name)

def gen(self):
length = self.CSC.get(self.name)
range_start = 10 ** (length - 1)
range_end = (10 ** length) - 1
return random.randint(range_start, range_end)


if __name__ == '__main__':
c = CardGenerator()
print(c.IIN)
print(c.gen("CID"))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='lfinancial',
version='0.3.0',
version='0.3.1',
author='zaneliu',
description='Generate financial test data',
long_description=long_description,
Expand Down

0 comments on commit 5594baf

Please sign in to comment.