-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopan.go
146 lines (124 loc) · 2.64 KB
/
gopan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package gopan
import (
"encoding/json"
"io/ioutil"
"math/rand"
"path"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
const (
UNKNOWN = "Unknown"
AMERICAN_EXPRESS = "AMERICAN EXPRESS"
CHINA_UNION_PAY = "CHINA UNION PAY"
DINERS_CLUB = "DINERS CLUB"
DISCOVER = "DISCOVER"
JCB = "JCB"
MASTERCARD = "MASTERCARD"
VISA = "VISA"
)
type Config struct {
BrandPrefix []ConfigBrandPrefix `json:"brand_prefix"`
}
type ConfigBrandPrefix struct {
Name string `json:"name"`
Prefixes []string `json:"prefix"`
Length int `json:"length"`
}
func IsValid(pan string) bool {
pan = clean(pan)
var sum int
var even bool
l := len(pan)
if l < 13 || l > 19 {
return false
}
for i := l - 1; i > -1; i-- {
mod, _ := strconv.Atoi(string(pan[i]))
if even {
mod *= 2
if mod > 9 {
mod = (mod % 10) + 1
}
}
even = !even
sum += mod
}
return sum%10 == 0
}
func GetBrand(pan string) string {
pan = clean(pan)
l := len(pan)
config := getConfig()
for _, brandPrefix := range config.BrandPrefix {
for _, prefix := range brandPrefix.Prefixes {
if strings.HasPrefix(pan, prefix) && l == brandPrefix.Length {
return brandPrefix.Name
}
}
}
return UNKNOWN
}
func GetHiddenPan(pan string, hiddenChar ...string) string {
pan = clean(pan)
hc := "x"
if len(hiddenChar) == 1 {
hc = hiddenChar[0]
}
l := len(pan)
h := []string{pan[:6], strings.Repeat(hc, l-10), pan[l-4:]}
return strings.Join(h, "")
}
func Generate(brand ...string) string {
rand.Seed(time.Now().UnixNano())
config := getConfig()
bpx := config.BrandPrefix[rand.Intn(len(config.BrandPrefix)-1)]
if len(brand) > 0 {
for _, brandPrefix := range config.BrandPrefix {
if brandPrefix.Name == brand[0] {
bpx = brandPrefix
break
}
}
}
pfx := bpx.Prefixes[0]
pl := len(bpx.Prefixes)
if pl > 1 {
pfx = bpx.Prefixes[rand.Intn(pl-1)]
}
fl := bpx.Length - len(pfx) - 1
pansl := []string{pfx}
for i := 0; i < fl; i++ {
pansl = append(pansl, strconv.Itoa(rand.Intn(9)))
}
pan := strings.Join(pansl, "")
sum := 0
for k, _ := range pan {
sub, _ := strconv.Atoi(string(pan[(len(pan)-1)-k]))
w := sub * (2 - (k % 2))
if w < 10 {
sum += w
} else {
sum += w - 9
}
}
last := (10 - sum%10) % 10
return pan + strconv.Itoa(last)
}
func getConfig() Config {
_, filename, _, _ := runtime.Caller(1)
file, err := ioutil.ReadFile(path.Dir(filename) + "/config.json")
if err != nil {
panic(err)
}
var config Config
json.Unmarshal(file, &config)
return config
}
func clean(pan string) string {
r := regexp.MustCompile(`[\d]+`)
return strings.Join(r.FindAllString(pan, -1), "")
}