-
Notifications
You must be signed in to change notification settings - Fork 0
/
is-cnpj.go
89 lines (67 loc) · 1.28 KB
/
is-cnpj.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
package tutils
import "strconv"
func IsCNPJ(text string) bool {
notCnpj := []string{
"00000000000000",
"11111111111111",
"22222222222222",
"33333333333333",
"44444444444444",
"55555555555555",
"66666666666666",
"77777777777777",
"88888888888888",
"99999999999999",
}
if (text == "") {
return false
}
if (len(text) != 14) {
return false
}
if (Contains(notCnpj, text)) {
return false
}
length := len(text) - 2
numbers := text[:length]
digits := text[length:]
sum := int64(0)
pos := length - 7
for i := length; i >= 1; i-- {
nbr, _ := strconv.ParseInt(string(numbers[length - i]), 10, 64)
pos--
sum += nbr * int64(pos)
if (pos < 2) {
pos = 9
}
}
var result int64
if (sum % 11 < 2) {
result = 0
} else {
result = 11 - (sum % 11)
}
firstChar, _ := strconv.ParseInt(string(digits[0]), 10, 64)
if result != firstChar {
return false
}
length += 1
numbers = text[0:length]
sum = 0
pos = length - 7
for i := length; i >= 1; i-- {
nbr, _ := strconv.ParseInt(string(numbers[length-i]), 10, 64)
pos--
sum += nbr * int64(pos)
if (pos < 2) {
pos = 9
}
}
if (sum % 11 < 2) {
result = 0
} else {
result = 11 - (sum % 11)
}
secondChar, _ := strconv.ParseInt(string(digits[1]), 10, 64)
return result == secondChar;
}