Last active
August 29, 2015 14:11
-
-
Save dimiro1/36700bcf6c57ec84947a to your computer and use it in GitHub Desktop.
Validador de CPF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cpf | |
import ( | |
"errors" | |
"strconv" | |
"strings" | |
) | |
var CpfInvalido = errors.New("Cpf Inválido") | |
func ValidarCPF(cpf string) (bool, error) { | |
cpf = strings.Replace(cpf, ".", "", -1) | |
cpf = strings.Replace(cpf, "-", "", -1) | |
if len(cpf) != 11 { | |
return false, CpfInvalido | |
} | |
switch cpf { | |
case "00000000000": | |
fallthrough | |
case "11111111111": | |
fallthrough | |
case "22222222222": | |
fallthrough | |
case "33333333333": | |
fallthrough | |
case "44444444444": | |
fallthrough | |
case "55555555555": | |
fallthrough | |
case "66666666666": | |
fallthrough | |
case "77777777777": | |
fallthrough | |
case "88888888888": | |
fallthrough | |
case "99999999999": | |
return false, CpfInvalido | |
} | |
v := [2]int{} | |
sliceCpf := strings.Split(cpf, "") | |
sliceIntCpf := [11]int{} | |
for i, digito := range sliceCpf { | |
var err error | |
sliceIntCpf[i], err = strconv.Atoi(digito) | |
if err != nil { | |
return false, CpfInvalido | |
} | |
} | |
v[0] = sliceIntCpf[0] + 2*sliceIntCpf[1] + 3*sliceIntCpf[2] | |
v[0] += 4*sliceIntCpf[3] + 5*sliceIntCpf[4] + 6*sliceIntCpf[5] | |
v[0] += 7*sliceIntCpf[6] + 8*sliceIntCpf[7] + 9*sliceIntCpf[8] | |
v[0] = v[0] % 11 | |
v[0] = v[0] % 10 | |
v[1] = sliceIntCpf[1] + 2*sliceIntCpf[2] + 3*sliceIntCpf[3] | |
v[1] += 4*sliceIntCpf[4] + 5*sliceIntCpf[5] + 6*sliceIntCpf[6] | |
v[1] += 7*sliceIntCpf[7] + 8*sliceIntCpf[8] + 9*v[0] | |
v[1] = v[1] % 11 | |
v[1] = v[1] % 10 | |
return v[0] == sliceIntCpf[9] && v[1] == sliceIntCpf[10], nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cpf | |
import ( | |
"testing" | |
) | |
func TestCpfNumerosValidos(t *testing.T) { | |
valido, err := ValidarCPF("324.867.967-03") | |
if !valido { | |
t.Error(err) | |
} | |
valido, err = ValidarCPF("26167020701") | |
if !valido { | |
t.Error(err) | |
} | |
} | |
func TestCpfNumerosInvalidos(t *testing.T) { | |
valido, err := ValidarCPF("") | |
if valido { | |
t.Error(err) | |
} | |
valido, err = ValidarCPF("00000000000") | |
if valido { | |
t.Error(err) | |
} | |
valido, err = ValidarCPF("11111111111") | |
if valido { | |
t.Error(err) | |
} | |
valido, err = ValidarCPF("34828353845") | |
if valido { | |
t.Error(err) | |
} | |
valido, err = ValidarCPF("324.867.967-05") | |
if valido { | |
t.Error(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment