Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created August 14, 2015 23:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juanplopes/074963f8837dae0f6286 to your computer and use it in GitHub Desktop.
Save juanplopes/074963f8837dae0f6286 to your computer and use it in GitHub Desktop.
def next_digit(value, base):
return value + str(sum(int(a)*b for a,b in zip(value, base))%11%10)
def make_valid(value, ap2, base):
return next_digit(next_digit(value, base), ap2+base)
def is_valid_cpf(cpf):
return make_valid(cpf[:9], [0], [1,2,3,4,5,6,7,8,9]) == cpf
def is_valid_cnpj(cnpj):
return make_valid(cnpj[:12], [5], [6,7,8,9,2,3,4,5,6,7,8,9]) == cnpj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment