aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/icrowley/fake/credit_cards.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-08-16 18:21:36 +0200
committerMichael Muré <batolettre@gmail.com>2018-08-16 18:22:00 +0200
commit1e43a6a7e35b32a95b0c533d8b2d31f242e72463 (patch)
treea52a07b97ca5daae0ca3576e3f3fe0e565818493 /vendor/github.com/icrowley/fake/credit_cards.go
parentf510e43418aef31e8521d346abdcda6c38f34eaf (diff)
downloadgit-bug-1e43a6a7e35b32a95b0c533d8b2d31f242e72463.tar.gz
add a new main to generate random bugs
Diffstat (limited to 'vendor/github.com/icrowley/fake/credit_cards.go')
-rw-r--r--vendor/github.com/icrowley/fake/credit_cards.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/github.com/icrowley/fake/credit_cards.go b/vendor/github.com/icrowley/fake/credit_cards.go
new file mode 100644
index 00000000..47d6ca72
--- /dev/null
+++ b/vendor/github.com/icrowley/fake/credit_cards.go
@@ -0,0 +1,69 @@
+package fake
+
+import (
+ "strings"
+
+ "strconv"
+)
+
+type creditCard struct {
+ vendor string
+ length int
+ prefixes []int
+}
+
+var creditCards = map[string]creditCard{
+ "visa": {"VISA", 16, []int{4539, 4556, 4916, 4532, 4929, 40240071, 4485, 4716, 4}},
+ "mastercard": {"MasterCard", 16, []int{51, 52, 53, 54, 55}},
+ "amex": {"American Express", 15, []int{34, 37}},
+ "discover": {"Discover", 16, []int{6011}},
+}
+
+// CreditCardType returns one of the following credit values:
+// VISA, MasterCard, American Express and Discover
+func CreditCardType() string {
+ n := len(creditCards)
+ var vendors []string
+ for _, cc := range creditCards {
+ vendors = append(vendors, cc.vendor)
+ }
+
+ return vendors[r.Intn(n)]
+}
+
+// CreditCardNum generated credit card number according to the card number rules
+func CreditCardNum(vendor string) string {
+ if vendor != "" {
+ vendor = strings.ToLower(vendor)
+ } else {
+ var vendors []string
+ for v := range creditCards {
+ vendors = append(vendors, v)
+ }
+ vendor = vendors[r.Intn(len(vendors))]
+ }
+ card := creditCards[vendor]
+ prefix := strconv.Itoa(card.prefixes[r.Intn(len(card.prefixes))])
+ num := []rune(prefix)
+ for i := 0; i < card.length-len(prefix); i++ {
+ num = append(num, genCCDigit(num))
+ }
+ return string(num)
+}
+
+func genCCDigit(num []rune) rune {
+ sum := 0
+ for i := len(num) - 1; i >= 0; i-- {
+ n := int(num[i])
+ if i%2 != 0 {
+ sum += n
+ } else {
+ if n*2 > 9 {
+ sum += n*2 - 9
+ } else {
+ sum += n * 2
+ }
+ }
+ }
+ return rune(((sum/10+1)*10 - sum) % 10)
+}