Skip to content

Instantly share code, notes, and snippets.

@tonio-m
Created January 12, 2021 12:26
Show Gist options
  • Save tonio-m/aba6bdbd0dbdf757f01fd8581d337356 to your computer and use it in GitHub Desktop.
Save tonio-m/aba6bdbd0dbdf757f01fd8581d337356 to your computer and use it in GitHub Desktop.
Função do python que valida cnpjs usando as normas do dataprev. (http://sa.previdencia.gov.br/site/2015/07/rgrv_RegrasValidacao.pdf)
def validate_cnpj(cnpj):
if len(cnpj) != 14 or len(set(cnpj)) == 1:
return False
def calculate_verifier(digits,multipliers):
multiplied_pairwise = [(int(x)*int(y)) for x,y in zip(digits,multipliers)]
sum_ = sum(multiplied_pairwise)
modulo = sum_ % 11
verifier = 0 if modulo < 2 else (11 - modulo)
return str(verifier)
verifiers = cnpj[12:]
first_verifier = calculate_verifier(cnpj[:12],'543298765432')
second_verifier = calculate_verifier(cnpj[:12] + first_verifier,'6543298765432')
if f'{first_verifier}{second_verifier}' == verifiers:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment