-
Notifications
You must be signed in to change notification settings - Fork 0
/
cardUtils.go
226 lines (201 loc) · 5.15 KB
/
cardUtils.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package CardUtils
import (
"fmt"
"github.com/mickstar/payment-card-utils-go/Scheme"
"math/rand"
"strconv"
"strings"
"time"
)
// maskPan Returns a masked version of the card number
// this is the first 6 digits and the last 4 digits,
// with the rest replaced by asterisks.
// if the pan is less than 10 digits, the PAN is returned without modification.
func MaskPan(pan string) string {
return MaskPanWithCharacter(pan, '*')
}
// same as maskPan except using the utf8 character to mask the pan
func MaskPanWithCharacter(pan string, maskCharacter uint8) string {
// mask pan
if len(pan) <= 10 {
return pan
}
return pan[0:6] + strings.Repeat(string(maskCharacter), len(pan)-10) + pan[len(pan)-4:]
}
func GetCardScheme(pan string) Scheme.Scheme {
// test for visa
if strings.HasPrefix(pan, "4") {
return Scheme.Visa
}
// test for Amex
if strings.HasPrefix(pan, "34") || strings.HasPrefix(pan, "37") {
return Scheme.AmericanExpress
}
// test for MasterCard (51 - 55)
if strings.HasPrefix(pan, "51") ||
strings.HasPrefix(pan, "52") ||
strings.HasPrefix(pan, "53") ||
strings.HasPrefix(pan, "54") ||
strings.HasPrefix(pan, "55") {
return Scheme.MasterCard
}
// test for Diners
if strings.HasPrefix(pan, "36") || strings.HasPrefix(pan, "38") {
return Scheme.DinersClub
}
// test for JCB
if strings.HasPrefix(pan, "35") {
return Scheme.JCB
}
// test for Discover
if strings.HasPrefix(pan, "6011") || strings.HasPrefix(pan, "65") {
return Scheme.Discover
}
//test for BP Card
if strings.HasPrefix(pan, "7052") || strings.HasPrefix(pan, "7050") {
return Scheme.BPCard
}
//test for union pay 6200**-6250**
if strings.HasPrefix(pan, "62") && pan[2] >= '0' && pan[2] <= '5' &&
!(pan[2] == '5' && pan[3] != '0') {
return Scheme.UnionPay
}
return Scheme.Unknown
}
// GenerateRandomPanOfLength generates a random pan of the specified length
// This PAN is guaranteed to pass the Luhn check.
func GenerateRandomPanOfLength(length int) string {
// seed the random number generator
rand.Seed(time.Now().UnixNano())
// generate 9 random numbers
// each one within the range 0-9
nums := make([]int, length-1)
for i := 0; i < 9; i++ {
nums[i] = rand.Intn(10)
}
// create the PAN string
pan := ""
for _, n := range nums {
pan += fmt.Sprintf("%d", n)
}
for i := 0; i < 10; i++ {
if LuhnCheck(pan + strconv.Itoa(i)) {
return pan + strconv.Itoa(i)
}
}
// should never happen.
panic("Could not generate a valid PAN from base " + pan)
}
// GenerateRandomPanOfLength generates a random pan of 16 digits
// This PAN is guaranteed to pass the Luhn check.
func GenerateRandomPan() string {
return GenerateRandomPanOfLength(16)
}
func fillPanWithRandomUntilValid(pan string, length int) string {
for len(pan) < (length - 1) {
pan += strconv.Itoa(rand.Intn(10))
}
for i := 0; i < 10; i++ {
if LuhnCheck(pan + strconv.Itoa(i)) {
return pan + strconv.Itoa(i)
}
}
// should never happen.
panic("Could not generate a valid PAN from base " + pan)
}
func GenerateRandomPanOfScheme(scheme Scheme.Scheme) string {
switch scheme {
case Scheme.Visa:
return fillPanWithRandomUntilValid("4", 16)
case Scheme.AmericanExpress:
// 34 or 37
sPan := "3"
if rand.Intn(2) == 0 {
sPan += "4"
} else {
sPan += "7"
}
return fillPanWithRandomUntilValid(sPan, 15)
case Scheme.MasterCard:
// 51 - 55
sPan := "5"
sPan += strconv.Itoa(rand.Intn(5) + 1)
return fillPanWithRandomUntilValid(sPan, 16)
case Scheme.DinersClub:
sPan := "3"
if rand.Intn(2) == 0 {
sPan += "6"
} else {
sPan += "8"
}
return fillPanWithRandomUntilValid(sPan, 14)
case Scheme.JCB:
return fillPanWithRandomUntilValid("35", 16)
case Scheme.Discover:
// 6011 or 65
return fillPanWithRandomUntilValid("6011", 16)
case Scheme.BPCard:
return generateRandomBPCardPan()
case Scheme.UnionPay:
return generateRandomUnionPayPan()
default:
return GenerateRandomPan()
}
}
func generateRandomBPCardPan() string {
// 7052 or 7050
sPan := "705"
if rand.Intn(2) == 0 {
sPan += "2"
} else {
sPan += "0"
}
return fillPanWithRandomUntilValid(sPan, 19)
}
func generateRandomUnionPayPan() string {
// 6200**-6250**
sPan := "62"
next := rand.Intn(6) // number between 0 and 5
sPan += strconv.Itoa(next)
if next == 5 || next == 0 {
sPan += "0"
}
return fillPanWithRandomUntilValid(sPan, 16)
}
// LuhnCheck performs a Luhn check on the card number
func LuhnCheck(pan string) bool {
// check every digit is int
for _, c := range pan {
if c < '0' || c > '9' {
return false
}
}
controlDigit := pan[len(pan)-1]
sum := 0
shouldDouble := true
for i := len(pan) - 2; i >= 0; i-- {
digit := pan[i] - '0'
if shouldDouble {
digit *= 2
if digit > 9 {
digit -= 9
}
}
sum += int(digit)
shouldDouble = !shouldDouble
}
return (10-sum%10)%10 == int(controlDigit-'0')
}
// ValidityCheck verifies that the PAN matches the Scheme's card length,
// that the PAN is all numbers
// and that the PAN passes the Luhn check.
func ValidityCheck(pan string) bool {
// check if pan is valid
if len(pan) < 14 || len(pan) > 19 {
return false
}
if !Scheme.LengthCheckForScheme(GetCardScheme(pan), len(pan)) {
return false
}
return LuhnCheck(pan)
}