Last active
January 7, 2025 19:18
-
-
Save flpnascto/4e1af45e054551eea65a75e7a5855d16 to your computer and use it in GitHub Desktop.
cng-validate-go
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
// ATENTION - INVALID CODE | |
// This code is an attempt to create a valid calculation of a CNH verifier digit | |
package main | |
import ( | |
"fmt" | |
"strconv" | |
) | |
var list = []string{ | |
"80255838330", | |
"07964414045", | |
"36363798091", | |
"87583985760", // r1=6 e r2=0 | |
"37493111906", // r1=0 e r2=6 | |
"92174299000", // r1=10 e r2=2 | |
"58117335901", // r1=10 e r2=3 | |
"08530846740", // r1=4 e r2=10 | |
"29742991300", // r1=10 e r2=10 | |
"48929785301", // r1=10 e r2=1 gera erro | |
"89010942604", | |
"94683104108", // r1=0 e r2=8 | |
"96644878310", // r1=1 e r2=10 | |
"88853444102", // r1=10 e r2=0 gera erro | |
"88853444109", // deve ser válido | |
"74258825400", | |
"33206482401", | |
"36278875701", | |
"00997215200", | |
"88267431500", | |
"18848472705", | |
"48240927404", | |
"34470769500", | |
} | |
func main() { | |
for _, item := range list { | |
calculate(item) | |
} | |
} | |
func calculate(value string) { | |
t1 := 0 | |
t2 := 0 | |
numbers, err := StringToIntArray(value[:9]) | |
if err != nil { | |
panic(err) | |
} | |
dsr1 := 9 | |
dsr2 := 1 | |
for _, num := range numbers { | |
t1 += num * dsr1 | |
t2 += num * dsr2 | |
dsr1-- | |
dsr2++ | |
} | |
rest1 := t1 % 11 | |
rest2 := t2 % 11 | |
dv1 := rest1 | |
dv2 := rest2 | |
if rest1 > 9 { | |
dv1 = 0 | |
// if rest2-2 < 0 { | |
// dv2 += 9 | |
// } | |
// if rest2-2 >= 0 { | |
// dv2 -= 2 | |
// } | |
} | |
if rest2 > 9 { | |
dv2 = 0 | |
} | |
if rest1 > 9 && dv2 >= 2 { | |
dv2 -= 2 | |
} | |
dv := strconv.Itoa(dv1) + strconv.Itoa(dv2) | |
if dv != value[9:] { | |
fmt.Printf("error no CNH %s, esperado %s - resultado %s\n", value, value[9:], dv) | |
fmt.Println("resto 1 ->", rest1) | |
fmt.Println("resto 2 ->", rest2) | |
fmt.Println("===============================================") | |
} | |
} | |
func StringToIntArray(s string) ([]int, error) { | |
var result []int | |
for _, char := range s { | |
digit, _ := strconv.Atoi(string(char)) | |
result = append(result, digit) | |
} | |
return result, nil | |
} | |
/* references: | |
- https://asmetro.org.br/portalsn/storage/2021/12/RESOLUCAO-cONTRAN-No-886-DE-13-DE-DEZEMBRO-DE-2021-RESOLUCAO-cONTRAN-No-886-DE-13-DE-DEZEMBRO-DE-2021-DOU-Imprensa-Nacional.pdf | |
- https://www.gov.br/transportes/pt-br/assuntos/transito/conteudo-contran/resolucoes/resolucao59820162.pdf | |
- https://siga0984.wordpress.com/2019/05/01/algoritmos-validacao-de-cnh/ | |
- https://github.com/vn-labs/BRdocs-validation/blob/main/br_docs/validators/cnh.py | |
- https://github.com/danielsouza/validarCNH/blob/master/ValidarCNH.java | |
- https://github.com/ogilvieira/validator-brasil/blob/main/src/isCNH.ts | |
- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment