Last active
May 23, 2018 19:24
-
-
Save AlbertoMonteiro/3ec1dcf541a52d51921b to your computer and use it in GitHub Desktop.
Validação de CPF/CNPJ
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
private static bool IsValidCNPJ(string cnpj) | |
{ | |
var mul1 = new[] {5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}; | |
var mul2 = new[] {6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}; | |
var cpnjWithoutCheckDigit = cnpj.Substring(0, 12); | |
var sum = 0; | |
for (var i = 0; i < 12; i++) | |
sum += int.Parse(cpnjWithoutCheckDigit[i].ToString())*mul1[i]; | |
var rest = sum % 11; | |
rest = rest < 2 ? 0 : 11 - rest; | |
var digits = rest.ToString(); | |
cpnjWithoutCheckDigit += digits; | |
sum = 0; | |
for (var i = 0; i < 13; i++) | |
sum += int.Parse(cpnjWithoutCheckDigit[i].ToString())*mul2[i]; | |
rest = sum%11; | |
rest = rest < 2 ? 0 : 11 - rest; | |
digits += rest.ToString(); | |
return cnpj.EndsWith(digits); | |
} |
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
public static class ValidadorDocumento | |
{ | |
static readonly int[] cpfMultipliers = new[] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 }; | |
static readonly int[] cnpjMultipliers = new[] { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 }; | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static string CleanString(this string cpf) | |
{ | |
var sb = new StringBuilder(); | |
foreach (var item in cpf) | |
if (char.IsDigit(item)) | |
sb.Append(item); | |
return sb.ToString(); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static bool IsValidCPF(string cpf) | |
=> cpf.CleanString().IsValidModulo11(cpfMultipliers, 11); | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static bool IsValidCNPJ(string cnpj) | |
=> cnpj.CleanString().IsValidModulo11(cnpjMultipliers, 14); | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
private static bool IsValidModulo11(this string code, int[] multipliers, int length) | |
{ | |
if (code.Length != length || VerficarSeTodosOsDigitosSaoIdenticos(code)) | |
return false; | |
int multiplicar(char digit, int mult) => ObterDigito(digit) * mult; | |
int sum = code.Zip(multipliers.Skip(1), multiplicar).Sum(); | |
var rest1 = sum % 11; | |
rest1 = rest1 < 2 ? 0 : 11 - rest1; | |
sum = code.Zip(multipliers, multiplicar).Sum(); | |
var rest2 = sum % 11; | |
rest2 = rest2 < 2 ? 0 : 11 - rest2; | |
return rest1 == ObterDigito(code[9]) && rest2 == ObterDigito(code[10]); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
private static int ObterDigito(char code) => code - '0'; | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
private static bool VerficarSeTodosOsDigitosSaoIdenticos(string code) | |
{ | |
var previous = -1; | |
for (var i = 0; i < code.Length; i++) | |
{ | |
var digito = code[i] - '0'; | |
if (previous == -1) | |
previous = digito; | |
else | |
{ | |
if (previous != digito) | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment