-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredit.go
132 lines (126 loc) · 2.95 KB
/
credit.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
package redact
func (this *creditCardRedaction) clear() {
this.start = 0
this.length = 0
this.totalSum = 0
this.isSecond = false
this.numBreaks = 0
}
func (this *creditCardRedaction) match(input []byte) {
inputLength := len(input) - 1
for i := inputLength; i > 0; i-- {
if i < len(this.used)-1 && this.used[i] {
this.resetCount(i, inputLength)
continue
}
if isNumeric(input[i]) {
this.numericLength++
this.length++
this.start = i
this.luhnCheck(input[i])
continue
}
if this.validateCard(input[this.start]) {
this.appendMatch(this.start, this.length)
this.resetCount(i, inputLength)
continue
}
if this.length > 0 && this.validateBreaks(input[i]) && !this.validateBreaks(input[i+1]) {
if this.breakType == 0 {
this.breakType = input[i]
}
if !validateBreakPositions(this.previousNumericLength, this.numericLength) && isNumeric(input[i+1]) {
this.resetCount(i, inputLength)
continue
}
this.previousNumericLength = this.numericLength
this.numericLength = 0
this.numBreaks++
this.length++
continue
}
this.resetCount(i, inputLength)
}
if this.length != 0 && isNumeric(input[0]) {
this.length++
this.start = 0
this.luhnCheck(input[this.start])
}
if this.validateCard(input[this.start]) {
this.appendMatch(this.start, this.length)
this.resetCount(0, inputLength)
}
}
func (this *creditCardRedaction) resetCount(i, length int) {
if this.length < length {
this.start = i - 1
}
this.length = 0
this.totalSum = 0
this.isSecond = false
this.numBreaks = 0
this.breakType = 0
this.numericLength = 0
this.previousNumericLength = 0
}
func (this *creditCardRedaction) luhnCheck(input byte) {
var value uint64
value = uint64(input - '0')
if this.isSecond {
value *= 2
}
this.totalSum += value / 10
this.totalSum += value % 10
this.isSecond = !this.isSecond
}
func (this *creditCardRedaction) validateBreaks(input byte) bool {
return (input == ' ' || input == '-') && (this.breakType == input || this.breakType == 0)
}
func (this *creditCardRedaction) validateCard(input byte) bool {
if this.numBreaks != 0 && (this.numBreaks < MinCreditBreakLength || this.numBreaks > MaxCreditBreakLength) {
return false
}
numericLength := this.length - this.numBreaks
if numericLength < MinCreditLength_NoBreaks || numericLength > MaxCreditLength_NoBreaks {
return false
}
if !validateNetwork(input) {
return false
}
return this.totalSum%10 == 0
}
func validateNetwork(input byte) bool {
return input >= '3' && input <= '6'
}
func validateBreakPositions(prev, curr int) bool {
switch prev {
case 0:
return true
case 4:
if curr == 4 {
return true
}
case 3:
if curr == 4 {
return true
}
case 5:
if curr == 6 || curr == 4 {
return true
}
case 6:
if curr == 4 {
return true
}
default:
return false
}
return false
}
// Valid credit card lengths are:
/*
#### #### ##### (4-4-5)
#### ###### ##### (4-6-5)
#### #### #### #### (4-4-4-4)
#### #### #### #### ### (4-4-4-4-3)
*/