diff --git a/Library/CnpjValidar.cs b/Library/CnpjValidar.cs index 8dfd48e..3d88092 100644 --- a/Library/CnpjValidar.cs +++ b/Library/CnpjValidar.cs @@ -5,51 +5,33 @@ namespace CpfCnpjLibrary { public static partial class Cnpj { + private static readonly int[] multiplicador1 = new int[12] { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 }; + private static readonly int[] multiplicador2 = new int[13] { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 }; + public static bool Validar(string cnpj) { if (string.IsNullOrWhiteSpace(cnpj)) return false; - cnpj = RemoverCaracteresEspeciais(cnpj); + cnpj = RemoverCaracteresEspeciais(cnpj).ToUpperInvariant(); - if (!ValidarNumerico(cnpj)) + if (!ValidarAlfanumerico(cnpj)) return false; - cnpj = ZeroEsquerda(cnpj, 14); + cnpj = AjustarComprimentoLegado(cnpj); - if (NumerosIguais(cnpj)) + if (cnpj.Length != 14) return false; - int[] multiplicador1 = new int[12] { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 }; - int[] multiplicador2 = new int[13] { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 }; - int soma; - int resto; - string digito; - string tempCnpj; - cnpj = cnpj.Trim(); - if (cnpj.Length != 14) + if (!char.IsDigit(cnpj[12]) || !char.IsDigit(cnpj[13])) return false; - tempCnpj = cnpj.Substring(0, 12); - soma = 0; - for (int i = 0; i < 12; i++) - soma += int.Parse(tempCnpj[i].ToString()) * multiplicador1[i]; - resto = (soma % 11); - if (resto < 2) - resto = 0; - else - resto = 11 - resto; - digito = resto.ToString(); - tempCnpj = tempCnpj + digito; - soma = 0; - for (int i = 0; i < 13; i++) - soma += int.Parse(tempCnpj[i].ToString()) * multiplicador2[i]; - resto = (soma % 11); - if (resto < 2) - resto = 0; - else - resto = 11 - resto; - digito = digito + resto.ToString(); - return cnpj.EndsWith(digito); + + if (TodosCaracteresIguais(cnpj)) + return false; + + string raiz = cnpj.Substring(0, 12); + string digitos = CalcularDigitosVerificadores(raiz); + return cnpj.EndsWith(digitos); } private static string RemoverCaracteresEspeciais(string numero) @@ -72,15 +54,59 @@ private static bool ValidarNumerico(string numero) return false; } - private static bool NumerosIguais(string numero) + private static bool ValidarAlfanumerico(string numero) + { + return numero.All(char.IsLetterOrDigit); + } + + private static string AjustarComprimentoLegado(string cnpj) + { + if (ValidarNumerico(cnpj)) + return ZeroEsquerda(cnpj, 14); + + return cnpj; + } + + private static bool TodosCaracteresIguais(string valor) { - char comparar = numero.ToString()[0]; - foreach (var n in numero.ToString()) + char comparar = valor[0]; + foreach (var caractere in valor) { - if (comparar != n) + if (comparar != caractere) return false; } return true; } + + private static string CalcularDigitosVerificadores(string raiz) + { + int primeiroDigito = CalcularDigito(raiz, multiplicador1); + int segundoDigito = CalcularDigito(raiz + primeiroDigito, multiplicador2); + return primeiroDigito.ToString() + segundoDigito.ToString(); + } + + private static int CalcularDigito(string valor, int[] pesos) + { + int soma = 0; + for (int i = 0; i < valor.Length; i++) + soma += ConverterCaractereParaValor(valor[i]) * pesos[i]; + + int resto = soma % 11; + if (resto < 2) + return 0; + + return 11 - resto; + } + + private static int ConverterCaractereParaValor(char caractere) + { + if (char.IsDigit(caractere)) + return caractere - '0'; + + if (caractere >= 'A' && caractere <= 'Z') + return caractere - '0'; + + return -1; + } } } diff --git a/Library/Library.csproj b/Library/Library.csproj index 39b49c7..79c5b75 100644 --- a/Library/Library.csproj +++ b/Library/Library.csproj @@ -2,15 +2,16 @@ netstandard2.0 - Cpf.Cnpj - 1.0.2 + Cpf.Cnpj + 1.0.3 Rafael Bonaldi - rafaelbonaldi@gmail.com Validar e/ou formatar os valores de CPF e CNPJ. Documentação brasileira. logo.png README nuget.md https://github.com/RBonaldi/CPF.CNPJ https://github.com/RBonaldi/CPF.CNPJ - True + False + cpf, cnpj, validar, formatar GitHub @@ -26,4 +27,5 @@ + diff --git a/README.md b/README.md index 0d037b0..cd6cb34 100644 --- a/README.md +++ b/README.md @@ -35,10 +35,20 @@ using CpfCnpjLibrary; Cnpj.Validar("00358823000193"); // True Cnpj.Validar("358823000193"); // True (0 à esquerda) + +//Cnpj alfanumerico + +Cnpj.Validar("Z8.1RG.51Z/0001-41"); //True +Cnpj.Validar("6W9ATTLB000119"); //True + +Cnpj.Validar("12.ABC.345/01DE-36"); //False +Cnpj.Validar("AA.AAA.AAA/AAAA-AA"); //False + Cnpj.Validar("00.358.823/0001-93"); // True Cnpj.Validar("00000000000000"); // False Cnpj.Validar("xpto"); // False + Cnpj.FormatarComPontuacao("00358823000193"); // "00.358.823/0001-93" Cnpj.FormatarComPontuacao("00.358.823/0001-93"); // "00.358.823/0001-93" Cnpj.FormatarComPontuacao("xpto"); // "xpto" diff --git a/Tests/CnpjValidarTests.cs b/Tests/CnpjValidarTests.cs index ccd5517..9f0781e 100644 --- a/Tests/CnpjValidarTests.cs +++ b/Tests/CnpjValidarTests.cs @@ -16,5 +16,52 @@ public void ValidWithMask() Assert.IsTrue(Cnpj.Validar("02089275000179")); Assert.IsTrue(Cnpj.Validar("2089275000179")); } + + [TestMethod] + public void ValidAlphanumeric() + { + Assert.IsTrue(Cnpj.Validar("12.ABC.345/01DE-35")); + Assert.IsTrue(Cnpj.Validar("12abc34501de35")); + Assert.IsTrue(Cnpj.Validar("Z8.1RG.51Z/0001-41")); + Assert.IsTrue(Cnpj.Validar("B0.5XL.TB5/0001-80")); + Assert.IsTrue(Cnpj.Validar("0C.P2R.LHM/0001-69")); + Assert.IsTrue(Cnpj.Validar("Z8.1RG.51Z/0001-41")); + Assert.IsTrue(Cnpj.Validar("6L.B1T.GP3/0001-71")); + Assert.IsTrue(Cnpj.Validar("6W9ATTLB000119")); + Assert.IsTrue(Cnpj.Validar("PZVJLGY3000166")); + Assert.IsTrue(Cnpj.Validar("ZAWTX14J000122")); + } + + [TestMethod] + public void InvalidAlphanumeric() + { + Assert.IsFalse(Cnpj.Validar("12.ABC.345/01DE-36")); + Assert.IsFalse(Cnpj.Validar("12.ABC.345/01DE-XX")); + Assert.IsFalse(Cnpj.Validar("AA.AAA.AAA/AAAA-AA")); + } + + [TestMethod] + public void InvalidNumeric() + { + Assert.IsFalse(Cnpj.Validar(null)); + Assert.IsFalse(Cnpj.Validar(string.Empty)); + Assert.IsFalse(Cnpj.Validar(" ")); + Assert.IsFalse(Cnpj.Validar("00.000.000/0000-00")); + Assert.IsFalse(Cnpj.Validar("11.111.111/1111-11")); + Assert.IsFalse(Cnpj.Validar("67.419.649/0001-13")); + Assert.IsFalse(Cnpj.Validar("67419649000113")); + Assert.IsFalse(Cnpj.Validar("67.419.649/0001-1A")); + Assert.IsFalse(Cnpj.Validar("67.419.649/0001-123")); + } + + [TestMethod] + public void InvalidAlphanumericFormat() + { + Assert.IsFalse(Cnpj.Validar("12.ABC.345/01D-35")); + Assert.IsFalse(Cnpj.Validar("12.ABC.345/01DEF-35")); + Assert.IsFalse(Cnpj.Validar("12.ABC.345/01D*-35")); + Assert.IsFalse(Cnpj.Validar("12.ABC.345/01DE-A5")); + Assert.IsFalse(Cnpj.Validar("12.ABC.345/01DE-3A")); + } } } diff --git a/calculodvcnpjalfanaumerico.pdf b/calculodvcnpjalfanaumerico.pdf new file mode 100644 index 0000000..8138d73 Binary files /dev/null and b/calculodvcnpjalfanaumerico.pdf differ