A iOS application written in Swift, this project is a library for CocoaPods that allows you to validate, generate and manipulate CPF and CNPJ numbers in a simple and efficient way. Ideal for testing and filling out forms.
-
CPF (Individual Taxpayer Registry): The CPF is an individual registration number used by the Brazilian Federal Revenue Service to identify an individual. The CPF is made up of 11 digits (XXX.XXX.XXX-XX). It is used for various purposes, such as opening bank accounts, issuing invoices, and filing taxes.
-
CNPJ (National Registry of Legal Entities): The CNPJ is a registration number used by the Brazilian Federal Revenue Service to identify companies or other legal entities. The CNPJ is made up of 14 digits (XX.XXX.XXX/0001-XX). It is used to formalize the existence of a company, allowing it to carry out commercial activities, such as issuing invoices, paying taxes, among others.
-
In view of the continuous growth in the number of companies and the imminent exhaustion of available CNPJ numbers, the Brazilian Federal Revenue Service is launching the Alphanumeric CNPJ. This solution aims to facilitate the identification of all companies and improve the business environment, contributing to the economic and social development of Brazil.
-
The Alphanumeric CNPJ will be assigned, starting in July 2026, exclusively to new registrations. Existing CNPJ numbers will not undergo any change, that is, those who are already registered with the CNPJ will keep their number valid!
- iOS 17.0 or later
- Xcode 15.0 or later
- Swift 5.0 or later
- CPF Validation: Check if a CPF number is valid, following the rules of the Federal Revenue Service.
- CNPJ Validation: Check if a CNPJ number is valid, according to tax rules.
- CNPJ Alphanumeric Validation: Check if a CNPJ Alphanumeric is valid, according to tax rules.
- Fake CPF generation: Generate a valid CPF number randomly.
- Fake CNPJ generation: Generate a valid CNPJ number randomly.
- Fake CNPJ Alphanumeric generation: Generate a valid CNPJ Alphanumeric randomly.
Add the dependency to your Podfile:
$ pod 'CPFCNPJTools'
Then run:
$ pod install
Swift Package Manager (SPM) is a tool for managing the distribution of Swift code as well as C-family dependency. From Xcode 11, SwiftPM got natively integrated with Xcode.
CPFCNPJTools support SPM from version 5.1.0. To use SPM, you should use Xcode to open your project. Click File
-> Add Package Dependencies
, enter CPFCNPJTools repo's URL.
- To validate a CPF, simply use the
validate(cpf: String) -> CPFStatus
method:
let cpfManager = CPFManager()
let status = cpfManager.validate(cpf: "11144477735")
print(status) // .valid, .invalid, .cpfNull, .equalDigits or .invalidFormat
- To generate a fake CPF with mask, simply use the
generateMasked()
method:
let cpfManager = CPFManager()
let fakeCPFMasked = cpfManager.generateMasked()
print(fakeCPFMasked) // "111.444.777-35" // Randomly generated CPF
- To validate a CNPJ, simply use the
validate(cnpj: String) -> CNPJStatus)
method:
let cnpjManager = CNPJManager()
let status = cnpjManager.validate(cnpj: "11444777000135")
print(status) // .valid, .invalid, .cnpjNull, .equalDigits or .invalidFormat
let cnpjManager = CNPJManager()
let fakeCNPJMasked = cnpjManager.generateMasked()
print(fakeCNPJMasked) // "11.444.777/0001-35" // Randomly generated CNPJ
- To validate a CNPJ Alphanumeric, simply use the
validate(cnpjDualFormat: String) -> CNPJDualFormatStatus
method:
let cnpjDualFormatManager = CNPJDualFormatManager()
let status = cnpjDualFormatManager.validate(cnpjDualFormat: "12ABC34501DE35")
print(status) // .valid, .invalid, .cnpjNull, .equalDigits or .invalidFormat
let cnpjDualFormatManager = CNPJDualFormatManager()
let fakeCNPJAlphaNum = cnpjDualFormatManager.generate()
print(fakeCNPJAlphaNum) // "12.ABC.345/01DE-35" // Randomly generated CNPJ Alphanumeric
- The file is formatted in Markdown (
.md
), which is the standard for GitHub readme files. - The methods are clearly described, with code examples in Swift to show how to use the library.
- The explanations and comments are all in English, making the documentation accessible to a wider audience.
Feel free to tweak any parts of the documentation to fit your style or add any additional information that you think is important!
If you'd like any more changes or additions, let me know.
- Diggo Silva
- Helio Mesquita