-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidatebr.go
278 lines (229 loc) · 4.52 KB
/
validatebr.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package validatebr
import (
"errors"
"regexp"
"slices"
"strconv"
"strings"
"unicode"
)
var (
cpfP1 = []int{10, 9, 8, 7, 6, 5, 4, 3, 2}
cpfP2 = []int{11, 10, 9, 8, 7, 6, 5, 4, 3, 2}
cnpjP1 = []int{5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
cnpjP2 = []int{6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
cnpjRegex = regexp.MustCompile(
`^[0-9]{2}[\.]?[0-9]{3}[\.]?[0-9]{3}[\/]?[0-9]{4}[-]?[0-9]{2}$`)
cnpjAlphaRegex = regexp.MustCompile(
`^[0-9A-Z]{2}\.?[0-9A-Z]{3}\.?[0-9A-Z]{3}/?[0-9A-Z]{4}-?[0-9]{2}$`)
cpfRegex = regexp.MustCompile(`^[0-9]{3}[\.]?[0-9]{3}[\.]?[0-9]{3}[-]?[0-9]{2}$`)
emailRegex = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`)
ErrInvalidPixType = errors.New("invalid pix type")
ErrInvalidCharacter = errors.New("invalid character")
)
func IsEmailValid(e string) bool {
s := strings.ToLower(e)
return emailRegex.MatchString(s)
}
func IsCNPJ(e string) bool {
return cnpjRegex.MatchString(e)
}
func IsCNPJAlpha(e string) bool {
return cnpjAlphaRegex.MatchString(strings.ToUpper(e))
}
func IsCPF(e string) bool {
return cpfRegex.MatchString(e)
}
func RemoveNonDigits(s string) string {
var b strings.Builder
for _, c := range s {
if unicode.IsDigit(c) {
b.WriteRune(c)
}
}
return b.String()
}
func RemoveNonAlphaNum(s string) string {
var b strings.Builder
for _, c := range s {
if unicode.IsDigit(c) ||
unicode.IsLetter(c) {
b.WriteRune(c)
}
}
return b.String()
}
func IsRepetitive(s string) bool {
if len(s) == 0 {
return false
}
c := s[0]
for _, v := range []byte(s) {
if c != v {
return false
}
}
return true
}
func sum(s string, table []int) int {
r := 0
for i, v := range table {
c := s[i]
d := int(c - '0')
r += v * d
}
return r
}
func getAlphanumericValue(r rune) (int, error) {
r = unicode.ToUpper(r)
if unicode.IsDigit(r) ||
unicode.IsLetter(r) {
return int(r) - 48, nil
}
return 0, ErrInvalidCharacter
}
func sumAlpha(s string, table []int) (int, error) {
total := 0
for i, v := range table {
val, err := getAlphanumericValue(rune(s[i]))
if err != nil {
return -1, err
}
total += val * v
}
return total, nil
}
func CPF(cpf string) bool {
cpf = RemoveNonDigits(cpf)
if len(cpf) != 11 {
return false
}
if IsRepetitive(cpf) {
return false
}
p1 := cpf[:9]
p2 := cpf[9:]
s := sum(p1, cpfP1)
r1 := s % 11
d1 := 0
if r1 >= 2 {
d1 = 11 - r1
}
s2 := p1 + strconv.Itoa(d1)
dsum := sum(s2, cpfP2)
r2 := dsum % 11
d2 := 0
if r2 >= 2 {
d2 = 11 - r2
}
p1aux := int(p2[0] - '0')
p2aux := int(p2[1] - '0')
return byte(d1) == byte(p1aux) && byte(d2) == byte(p2aux)
}
func CNPJ(cnpj string) bool {
cnpj = RemoveNonDigits(cnpj)
if len(cnpj) != 14 {
return false
}
if IsRepetitive(cnpj) {
return false
}
p1 := cnpj[:12]
p2 := cnpj[12:]
s := sum(p1, cnpjP1)
r1 := s % 11
d1 := 0
if r1 >= 2 {
d1 = 11 - r1
}
s2 := p1 + strconv.Itoa(d1)
dsum := sum(s2, cnpjP2)
r2 := dsum % 11
d2 := 0
if r2 >= 2 {
d2 = 11 - r2
}
p1aux := int(p2[0] - '0')
p2aux := int(p2[1] - '0')
return byte(d1) == byte(p1aux) && byte(d2) == byte(p2aux)
}
// CNPJAlphanumeric is a experimental function to validate CNPJ with alphanumeric characters use with caution.
func CNPJAlphanumeric(cnpj string) bool {
cnpj = RemoveNonAlphaNum(cnpj)
if len(cnpj) != 14 {
return false
}
if IsRepetitive(cnpj) {
return false
}
for i := 12; i < 14; i++ {
if cnpj[i] < '0' || cnpj[i] > '9' {
return false
}
}
p1 := cnpj[:12]
p2 := cnpj[12:]
s, err := sumAlpha(p1, cnpjP1)
if err != nil {
return false
}
if s < 0 {
return false
}
r1 := s % 11
d1 := 0
if r1 >= 2 {
d1 = 11 - r1
}
p1ComDV := p1 + strconv.Itoa(d1)
s2, err := sumAlpha(p1ComDV, cnpjP2)
if err != nil {
return false
}
if s2 < 0 {
return false
}
r2 := s2 % 11
d2 := 0
if r2 >= 2 {
d2 = 11 - r2
}
dv1, _ := strconv.Atoi(string(p2[0]))
dv2, _ := strconv.Atoi(string(p2[1]))
return d1 == dv1 && d2 == dv2
}
func PixKeyType(pixkey string) ([]string, error) {
types := map[string]bool{
"CNPJ": false,
"CPF": false,
"EMAIL": false,
"EVP": false,
"PHONE": false,
}
if IsEmailValid(pixkey) {
types["EMAIL"] = true
}
if IsCNPJ(pixkey) && CNPJ(pixkey) {
types["CNPJ"] = true
}
if IsCPF(pixkey) && CPF(pixkey) {
types["CPF"] = true
}
if len(pixkey) == 36 {
types["EVP"] = true
}
if PhoneWithBrazilianAreaCode(pixkey) {
types["PHONE"] = true
}
var ret []string
for k, v := range types {
if v {
ret = append(ret, k)
}
}
if len(ret) == 0 {
return nil, ErrInvalidPixType
}
slices.Sort(ret)
return ret, nil
}