-
Notifications
You must be signed in to change notification settings - Fork 0
/
is-cpf.go
75 lines (56 loc) · 1015 Bytes
/
is-cpf.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
package tutils
import (
"fmt"
"strconv"
)
func IsCPF(text string) bool {
notCpf := []string{
"00000000000000",
"11111111111111",
"22222222222222",
"33333333333333",
"44444444444444",
"55555555555555",
"66666666666666",
"77777777777777",
"88888888888888",
"99999999999999",
}
if (text == "") {
return false
}
if (len(text) != 14) {
return false
}
if (Contains(notCpf, text)) {
return false
}
temp := text[0:9]
count := int64(10)
total := int64(0)
for _, nbr := range temp {
nbrInt, _ := strconv.ParseInt(string(nbr), 10, 64)
total += nbrInt * count
count--
}
total = (total * 10) % 11
if (total > 9) {
total = 0
}
if fmt.Sprint(total) != string(text[9]) {
return false
}
temp = text[0:10]
count = 11
total = 0
for _, nbr := range temp {
nbrInt, _ := strconv.ParseInt(string(nbr), 10, 64)
total += nbrInt * count
count--
}
total = (total * 10) % 11
if (total > 9) {
total = 0
}
return fmt.Sprint(total) == string(text[10])
}