bchavez / bchavez/Bogus

Adds support for cnpj alphanumeric values

Open
#634 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
9.7k
Forks
538
PR merge metrics
No merged PRs in 30d

Description

### Description

Recently, the official Brazilian government decreed a new rule requiring that the CNPJ include not only digits, but also capital letters. This rule will come into effect in July 2026. At this time, most of systems are adapting. It would be interesting if we could generate a cnpj in this new format. I've tried to open a pull request with this feature, but without succeeded.

### LINQPad Code Example

```c#
public static class ExtensionsForBrazil
{
private static readonly int[] CnpjFirstDigitWeights = [5,4,3,2,9,8,7,6,5,4,3,2];
private static readonly int[] CnpjSecondDigitWeights = [6,5,4,3,2,9,8,7,6,5,4,3,2];
private const string AlphaNumericChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private const string NumericChars = "0123456789";

///
/// Cadastro Nacional da Pessoa Jurídica
///
/// Includes formatting symbols.
public static string Cnpj(this Company c, bool includeFormatSymbols = true, bool includeOnlyDigits = false, bool includeOnlyAlphaNumerics = false)
{
string chars;

if (includeOnlyDigits)
chars = NumericChars;
else if (includeOnlyAlphaNumerics)
chars = AlphaNumericChars;
else
chars = c.Random.Bool() ? NumericChars : AlphaNumericChars;

var cnpj = "";
for (var i = 0; i < 8; i++)
cnpj += chars[c.Random.Int(0, chars.Length - 1)];

cnpj += "0001";

var firstDigit = CalculateDigit(cnpj, CnpjFirstDigitWeights);
cnpj += firstDigit;

var secondDigit = CalculateDigit(cnpj, CnpjSecondDigitWeights);
cnpj += secondDigit;

if ( includeFormatSymbols )
cnpj = $"{cnpj[0]}{cnpj[1]}.{cnpj[2]}{cnpj[3]}{cnpj[4]}.{cnpj[5]}{cnpj[6]}{cnpj[7]}/{cnpj[8]}{cnpj[9]}{cnpj[10]}{cnpj[11]}-{cnpj[12]}{cnpj[13]}";

return cnpj;
}

private static int CalculateDigit(string cnpj, int[] weights)
{
var sum = cnpj.Select((value, i) => (value - '0') * weights[i]).Sum();
var remainder = sum % 11;
return remainder < 2 ? 0 : 11 - remainder;
}
}
```

### What alternatives have you considered?

n/a

### Could you help with a pull-request?

Yes

Contributor guide

Open the contributing guide

Research direction

Start by locating the CNPJ entry point and compare its behavior with the LINQPad Code Example, especially ExtensionsForBrazil.Cnpj and CalculateDigit. Check how the existing generator handles formatting and numeric values. Done means CNPJ generation supports the new alphanumeric format while preserving the requested output options and correct check digits.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.