-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpf.go
82 lines (67 loc) · 1.53 KB
/
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
76
77
78
79
80
81
82
package cpf
import (
"bytes"
"math"
"regexp"
"strconv"
"strings"
)
// Valid checks if the passed cpf is valid
func Valid(cpf string) bool {
if cpf = clean(cpf); len(cpf) != 11 {
return false
}
reg := regexp.MustCompile(`(1|2|3|4|5|6|7|8|9){11}`)
if reg.MatchString(clean(cpf)) {
return false
}
verifiedCPF, err := validateCPF(cpf)
if err != nil {
return false
}
return verifiedCPF == clean(cpf)
}
func validateCPF(cpf string) (string, error) {
fd, err := calcFirstDigit(cpf[:9])
if err != nil {
return "", err
}
sd, err := calcSecondDigit(cpf[:9] + strconv.Itoa(fd))
if err != nil {
return "", err
}
var b bytes.Buffer
b.WriteString(clean(cpf)[:9])
b.WriteString(strconv.Itoa(fd))
b.WriteString(strconv.Itoa(sd))
return b.String(), nil
}
func calcFirstDigit(cpf string) (int, error) {
verifiers := []int{10, 9, 8, 7, 6, 5, 4, 3, 2}
digits := strings.Split(cpf, "")
return calcDigit(digits, verifiers, 9)
}
func calcSecondDigit(cpf string) (int, error) {
verifiers := []int{11, 10, 9, 8, 7, 6, 5, 4, 3, 2}
digits := strings.Split(cpf, "")
return calcDigit(digits, verifiers, 10)
}
func calcDigit(digits []string, verifiers []int, size int) (int, error) {
var sum int
for i := 0; i < size; i++ {
dig, err := strconv.Atoi(digits[i])
if err != nil {
return sum, err
}
sum = sum + verifiers[i]*dig
}
if r := math.Mod(float64(sum), 11); r >= 2 {
return int(11 - r), nil
}
return 0, nil
}
func clean(s string) string {
reg := regexp.MustCompile(`[^\d]+`)
c := reg.ReplaceAllString(s, "")
return c
}