Skip to main content

CPF and CNPJ validation and generation with support for alphanumeric CNPJ

Project description

📦 br-cpf-cnpj

Tests codecov

A Python library for CPF and CNPJ validation and generation, with support for the new alphanumeric CNPJ standard.


✨ Features

  • ✅ CPF validation
  • ✅ CNPJ validation (numeric and alphanumeric)
  • 🔢 Random CPF generator
  • 🔡 Random CNPJ generator (numeric or alphanumeric)
  • 🧪 Fully tested with pytest

📥 Installation

pip install br-cpf-cnpj

🚀 Usage

Validate CPF

from br_cpf_cnpj import is_valid_cpf

is_valid_cpf("529.982.247-25")
# True

# Validate unmasked CPF
is_valid_cpf("55782724366")
# True

# Validate numeric CPF
is_valid_cpf(70714720178)
# True

NOTE: If the value is a number, it will be converted to a string and any leading zeros will be removed.

Validate CNPJ

from br_cpf_cnpj import is_valid_cnpj

# Validate numeric CNPJ
is_valid_cnpj("11.222.333/0001-81")
# True

# Validate alphanumeric CNPJ
is_valid_cnpj("2P.76B.MNX/0001-66")
# True

# Validate unmasked CNPJ
is_valid_cnpj("B4ESBMHS000102")
# True

Generate Random CPF

from br_cpf_cnpj import generate_random_cpf

cpf = generate_random_cpf(masked=True)
print(cpf)
# e.g., "123.456.789-09"

cpf_unmasked = generate_random_cpf(masked=False)
print(cpf_unmasked)
# e.g., "12345678909"

Generate Random CNPJ

from br_cpf_cnpj import generate_random_cnpj

# Generate numeric CNPJ
cnpj_numeric = generate_random_cnpj(alphanumeric=False, masked=True)
print(cnpj_numeric)
# e.g., "12.345.678/0001-95"

# Generate alphanumeric CNPJ
cnpj_alphanumeric = generate_random_cnpj(alphanumeric=True, masked=False)
print(cnpj_alphanumeric)
# e.g., "RSASKDDW000100"

🧠 How it works

CPF

  • Uses fixed weights (10 → 2, then 11 → 2)
  • Applies the official modulo 11 algorithm
  • Prevents invalid repeated-digit CPFs (e.g. 11111111111)

CNPJ (Alphanumeric)

  • Supports digits (0–9) and uppercase letters (A–Z)
  • Characters are converted using: value = ord(char) - ord('0')
  • Weights cycle from 2 → 9

🤝 Contributing

Contributions are very welcome!

Development Setup

This project uses uv for dependency management. To set up your development environment:

  1. Clone the repository
  2. Install dependencies: uv sync --extra dev
  3. Run tests: uv run pytest

📄 License

MIT License.


Versão em Português

📦 br-cpf-cnpj

Uma biblioteca Python para validação e geração de CPF e CNPJ, com suporte ao novo padrão de CNPJ alfanumérico.


✨ Funcionalidades

  • ✅ Validação de CPF
  • ✅ Validação de CNPJ (numérico e alfanumérico)
  • 🔢 Gerador de CPF aleatório
  • 🔡 Gerador de CNPJ aleatório (numérico ou alfanumérico)
  • 🧪 Testes automatizados com pytest

📥 Instalação

pip install br-cpf-cnpj

🚀 Uso

Validar CPF

from br_cpf_cnpj import is_valid_cpf

is_valid_cpf("529.982.247-25")
# True

# Validar CPF sem máscara
is_valid_cpf("55782724366")
# True

# Validar CPF numérico
is_valid_cpf(70714720178)
# True

NOTA: Se o valor for um número, ele será convertido para string e quaisquer zeros à esquerda serão removidos.

Validar CNPJ

from br_cpf_cnpj import is_valid_cnpj

# Validar CNPJ numérico
is_valid_cnpj("11.222.333/0001-81")
# True

# Validar CNPJ alfanumérico
is_valid_cnpj("2P.76B.MNX/0001-66")
# True

# Validar CNPJ sem máscara
is_valid_cnpj("B4ESBMHS000102")
# True

Gerar CPF Aleatório

from br_cpf_cnpj import generate_random_cpf

cpf = generate_random_cpf(masked=True)
print(cpf)
# e.g., "123.456.789-09"

cpf_unmasked = generate_random_cpf(masked=False)
print(cpf_unmasked)
# e.g., "12345678909"

Gerar CNPJ Aleatório

from br_cpf_cnpj import generate_random_cnpj

# Gerar CNPJ numérico
cnpj_numeric = generate_random_cnpj(alphanumeric=False, masked=True)
print(cnpj_numeric)
# e.g., "12.345.678/0001-95"

# Gerar CNPJ alfanumérico
cnpj_alphanumeric = generate_random_cnpj(alphanumeric=True, masked=False)
print(cnpj_alphanumeric)
# e.g., "RSASKDDW000100"

🧠 Como Funciona

CPF

  • Pesos fixos (10 → 2 e 11 → 2)
  • Algoritmo módulo 11
  • Rejeita CPFs inválidos com dígitos repetidos (e.g. 11111111111)

CNPJ (Alfanumérico)

  • Aceita números (0–9) e letras (A–Z)
  • Conversão baseada em valor ASCII: value = ord(char) - ord('0')
  • Pesos cíclicos de 2 → 9

🤝 Contribuindo

Contribuições são muito bem-vindas!

Configuração do Ambiente de Desenvolvimento

Este projeto utiliza uv para gerenciamento de dependências. Para configurar seu ambiente de desenvolvimento:

  1. Clone o repositório
  2. Instale as dependências: uv sync --extra dev
  3. Execute os testes: uv run pytest

📄 License

MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

br_cpf_cnpj-0.2.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

br_cpf_cnpj-0.2.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file br_cpf_cnpj-0.2.1.tar.gz.

File metadata

  • Download URL: br_cpf_cnpj-0.2.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for br_cpf_cnpj-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d9ab29bcbee023147668914ad0f53837849235ddab253fa993b7e5f8ce43cd50
MD5 306c7759c09a6b7505ccf460269c4ae8
BLAKE2b-256 24db36c2e48981d65b26ea17ac711652e32b5371475c593733a2ce4811320b95

See more details on using hashes here.

Provenance

The following attestation bundles were made for br_cpf_cnpj-0.2.1.tar.gz:

Publisher: publish.yml on iupi-ip/br-cpf-cnpj

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file br_cpf_cnpj-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: br_cpf_cnpj-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for br_cpf_cnpj-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7989e1da48acf7522e1e0c4904f4329e918208a208f8771c92a49e567d6f4036
MD5 c976ce5565c0fa85771b5e1d3b2d4d7c
BLAKE2b-256 b9ded0c0920f6a2ffe05b7f37ef7b11c24fa2bd936177999a453990ef906b596

See more details on using hashes here.

Provenance

The following attestation bundles were made for br_cpf_cnpj-0.2.1-py3-none-any.whl:

Publisher: publish.yml on iupi-ip/br-cpf-cnpj

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page