Skip to content

Commit

Permalink
feat: add Java and C packages
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Jul 9, 2024
1 parent 156db0c commit 3b927e3
Show file tree
Hide file tree
Showing 13 changed files with 510 additions and 94 deletions.
77 changes: 77 additions & 0 deletions .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# This workflow will build a package using Maven and then publish it to GitHub packages when a release is created
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#apache-maven-with-a-settings-path

name: Maven Package

on:
release:
types: [created]

workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v4

- name: Set up JDK 8
uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'temurin'
server-id: github
settings-path: ${{ github.workspace }}

- name: Create settings.xml
run: |
echo '<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/gabriel-logan/Gerador-CPF-e-CNPJ-valido</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>gabriel-logan</username>
<password>${{secrets.MVN_TOKEN_GITHUB_PACKAGES}}</password>
</server>
</servers>
</settings>' > settings.xml
- name: Build with Maven
working-directory: ./packages/java
run: mvn -B package --file pom.xml

- name: Publish to GitHub Packages Apache Maven
working-directory: ./packages/java
run: mvn deploy -s settings.xml
env:
GITHUB_TOKEN: ${{ github.token }}
33 changes: 33 additions & 0 deletions packages/c/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Object files
*.o

# Executable files
*.exe
*.out

# Compiler generated files
*.dSYM/
*.gcda
*.gcno

# Dependency files
*.d

# IDE configuration files
/.vscode/
/.idea/

# Build files
/build/

# Log files
*.log

# Temporary files
*.tmp

# Backup files
*~

# Output directory
/output/
13 changes: 13 additions & 0 deletions packages/c/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# cpf-and-cnpj-generator

## How to use

```bash
gcc cpfGenerator.c -o cpfGenerator
./cpfGenerator
```

```bash
gcc cnpjGenerator.c -o cnpjGenerator
./cnpjGenerator
```
73 changes: 73 additions & 0 deletions packages/c/src/cnpjGenerator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to generate a random number between 0 and 9
int randomDigit() {
return rand() % 10;
}

// Function to generate the first 12 digits of the CNPJ
void generateCNPJBase(int cnpjBase[]) {
for (int i = 0; i < 12; i++) {
cnpjBase[i] = randomDigit();
}
}

// Function to calculate the first verifier digit
int calculateFirstVerifier(int cnpjBase[]) {
int weight[12] = {5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2};
int sum = 0;

for (int i = 0; i < 12; i++) {
sum += cnpjBase[i] * weight[i];
}

int remainder = sum % 11;

return remainder < 2 ? 0 : 11 - remainder;
}

// Function to calculate the second verifier digit
int calculateSecondVerifier(int cnpjBase[], int firstVerifier) {
int weight[13] = {6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2};
int sum = 0;

for (int i = 0; i < 12; i++) {
sum += cnpjBase[i] * weight[i];
}

sum += firstVerifier * weight[12];

int remainder = sum % 11;

return remainder < 2 ? 0 : 11 - remainder;
}

void generateValidCNPJ(char* cnpj) {
int cnpjBase[12];
generateCNPJBase(cnpjBase);

int firstVerifier = calculateFirstVerifier(cnpjBase);

int cnpjBaseWithVerifier[13];
for (int i = 0; i < 12; i++) {
cnpjBaseWithVerifier[i] = cnpjBase[i];
}
cnpjBaseWithVerifier[12] = firstVerifier;

int secondVerifier = calculateSecondVerifier(cnpjBaseWithVerifier, firstVerifier);

sprintf(cnpj, "%d%d%d%d%d%d%d%d%d%d%d%d%d%d",
cnpjBase[0], cnpjBase[1], cnpjBase[2], cnpjBase[3], cnpjBase[4], cnpjBase[5],
cnpjBase[6], cnpjBase[7], cnpjBase[8], cnpjBase[9], cnpjBase[10], cnpjBase[11],
firstVerifier, secondVerifier);
}

int main() {
srand(time(NULL));
char cnpj[15];
generateValidCNPJ(cnpj);
printf("CNPJ válido gerado: %s\n", cnpj);
return 0;
}
53 changes: 53 additions & 0 deletions packages/c/src/cpfGenerator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to generate a random number between 0 and 9
int randomNum() {
// generates a random number of 9 digits
return rand() % 10;
}

// Function to calculate the verifier digits
int calculateDigit(int cpfArray[], int factor) {
// The sum is calculated by multiplying each digit by a factor and summing the results
int sum = 0;
for (int i = 0; i < factor - 1; i++) {
sum += cpfArray[i] * (factor - i);
}
// If the sum is less than 2, the verifier digit is 0, otherwise it is 11 minus the remainder of the sum divided by 11
return sum % 11 < 2 ? 0 : 11 - (sum % 11);
}

// Function to generate and validate a CPF
void genAndValidate(char *cpf) {
int cpfArray[11];

// Generates an array of 9 random numbers
for (int i = 0; i < 9; i++) {
cpfArray[i] = randomNum();
}

// Calculates the first verifier digit
cpfArray[9] = calculateDigit(cpfArray, 10);

// Calculates the second verifier digit
cpfArray[10] = calculateDigit(cpfArray, 11);

// Converts the generated CPF numbers into a string
for (int i = 0; i < 11; i++) {
cpf[i] = '0' + cpfArray[i];
}
cpf[11] = '\0'; // Null-terminate the string
}

int main() {
// Seed the random number generator
srand(time(NULL));

char cpf[12];
genAndValidate(cpf);
printf("Generated CPF: %s\n", cpf);

return 0;
}
31 changes: 31 additions & 0 deletions packages/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Compiled class files
*.class

# Package files
*.jar
*.war
*.ear

# Maven specific files
target/

# Gradle specific files
build/

# IntelliJ IDEA specific files
.idea/
*.iml
*.iws
*.ipr

# Eclipse specific files
.classpath
.project
.settings/

# Logs
*.log

# OS specific files
.DS_Store
Thumbs.db
2 changes: 2 additions & 0 deletions packages/java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# cpf-and-cnpj-generator

34 changes: 34 additions & 0 deletions packages/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github</groupId>
<artifactId>cpf_and_cnpj-generator</artifactId>
<version>0.0.1</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
<repository>
<id>github</id>
<name>GitHub gabriel-logan Apache Maven Packages</name>
<url>https://maven.pkg.github.com/gabriel-logan/Gerador-CPF-e-CNPJ-valido</url>
</repository>
</distributionManagement>

</project>
Loading

0 comments on commit 3b927e3

Please sign in to comment.