Gerações NIF NISS

This commit is contained in:
SECUNDIS\masantos
2024-04-10 19:42:13 +01:00
parent 2b99972e01
commit be60a4df8e
14 changed files with 442 additions and 0 deletions

25
.dockerignore Normal file
View File

@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

View File

@@ -12,6 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionFiles", "SolutionFi
EndProject
Project("{54A90642-561A-4BB1-A94E-469ADEE60C69}") = "geradoresfe", "geradoresfe\geradoresfe.esproj", "{1D9DDDEB-6FBA-4A9C-AE85-6A3630D8B9AC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeradoresWS", "GeradoresWS\GeradoresWS.csproj", "{78B34F6D-E9C9-4FD0-A3E4-670BA401ACF6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeradoresService", "GeradoresService\GeradoresService.csproj", "{35C4D726-3771-4D34-9A43-541588FCECAA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -24,6 +28,14 @@ Global
{1D9DDDEB-6FBA-4A9C-AE85-6A3630D8B9AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D9DDDEB-6FBA-4A9C-AE85-6A3630D8B9AC}.Release|Any CPU.Build.0 = Release|Any CPU
{1D9DDDEB-6FBA-4A9C-AE85-6A3630D8B9AC}.Release|Any CPU.Deploy.0 = Release|Any CPU
{78B34F6D-E9C9-4FD0-A3E4-670BA401ACF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{78B34F6D-E9C9-4FD0-A3E4-670BA401ACF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78B34F6D-E9C9-4FD0-A3E4-670BA401ACF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78B34F6D-E9C9-4FD0-A3E4-670BA401ACF6}.Release|Any CPU.Build.0 = Release|Any CPU
{35C4D726-3771-4D34-9A43-541588FCECAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35C4D726-3771-4D34-9A43-541588FCECAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35C4D726-3771-4D34-9A43-541588FCECAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35C4D726-3771-4D34-9A43-541588FCECAA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

89
GeradoresService/NIF.cs Normal file
View File

@@ -0,0 +1,89 @@
using System.Net.NetworkInformation;
namespace GeradoresService
{
public class NIF
{
public static string Generate(string? type)
{
return GenerateRandomNIF(type);
}
public static string GenerateRandomNIF(string? nifType)
{
var firstDigitValidate = new char[] { '1', '2','3', '5', '6', '8', '9' };
Random rnd = new Random();
char firstDigit;
if (string.IsNullOrEmpty(nifType))
{
// Gera o primeiro dígito aleatório dentro dos válidos
int firstDigitIndex = rnd.Next(0, 6); // Escolhe um índice de 0 a 5
firstDigit = firstDigitValidate[firstDigitIndex];
} else {
if (firstDigitValidate.Contains(nifType[0])){
firstDigit = nifType[0];
}else
{
int firstDigitIndex = rnd.Next(0, 6); // Escolhe um índice de 0 a 5
firstDigit = firstDigitValidate[firstDigitIndex];
}
}
// Gera os próximos 7 dígitos aleatórios
string nextDigits = "";
for (int i = 0; i < 7; i++)
{
nextDigits += rnd.Next(0, 10); // Gera um dígito aleatório de 0 a 9
}
// Calcula o dígito de controlo
int checkDigit = (firstDigit - '0') * 9;
for (int i = 2; i <= 8; i++)
{
checkDigit += (nextDigits[i - 2] - '0') * (10 - i);
}
checkDigit = 11 - (checkDigit % 11);
if (checkDigit >= 10)
checkDigit = 0;
// Concatena os dígitos gerados e o dígito de controlo
string randomNIF = firstDigit + nextDigits + checkDigit;
return randomNIF;
}
public static bool Validate(string nif)
{
// Verificar se o NIF tem 9 dígitos
if (nif.Length != 9)
{
return false;
}
// Converter o NIF para um array de dígitos
int[] digitos = new int[9];
for (int i = 0; i < 9; i++)
{
if (!int.TryParse(nif[i].ToString(), out digitos[i]))
{
return false; // Se algum caractere não for um dígito, o NIF é inválido
}
}
// Calcular o dígito de controlo
int soma = 0;
for (int i = 0; i < 8; i++)
{
soma += digitos[i] * (9 - i);
}
int resto = soma % 11;
int digitoControlo = resto <= 1 ? 0 : 11 - resto;
// Verificar se o dígito de controlo coincide com o último dígito do NIF
return digitoControlo == digitos[8];
}
}
}

90
GeradoresService/NISS.cs Normal file
View File

@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeradoresService
{
public class NISS
{
private static readonly Random random = new Random();
public static string Generate() {
return GenerateNISS();
}
private static string GenerateRandomDigits(int length)
{
string digits = "1";
for (int i = 0; i < length; i++)
{
digits += random.Next(0, 10); // Gera um dígito aleatório entre 0 e 9
}
return digits;
}
public static string GenerateNISS()
{
// Gera 9 dígitos aleatórios
string niss = GenerateRandomDigits(9);
// Calcula o dígito de controlo
int soma = 0;
for (int i = 0; i < 10; i++)
{
int digito = int.Parse(niss[i].ToString());
soma += digito * (i + 1);
}
int resto = soma % 11;
int digitoControlo = 11 - resto;
// Verifica se o dígito de controlo está no intervalo [0, 9]
if (digitoControlo == 10)
{
niss += "0";
}
else if (digitoControlo == 11)
{
niss += "1";
}
else
{
niss += digitoControlo.ToString();
}
return niss;
}
public static bool Validate(string niss)
{
// Verifica se o NISS tem 11 dígitos
if (niss.Length != 11)
return false;
// Verifica se é um número
foreach (char c in niss)
{
if (!char.IsDigit(c))
return false;
}
// Calcula o dígito de controlo
int soma = 0;
for (int i = 0; i < 10; i++)
{
int digito = int.Parse(niss[i].ToString());
soma += digito * (i + 1);
}
int resto = soma % 11;
int digitoControlo = 11 - resto;
// Verifica o último dígito do NISS
int ultimoDigito = int.Parse(niss[10].ToString());
if (digitoControlo == 10 && ultimoDigito != 0)
return false;
if (digitoControlo == 11 && ultimoDigito != 0 && ultimoDigito != 1)
return false;
if (digitoControlo != ultimoDigito)
return false;
return true;
}
}
}

View File

@@ -0,0 +1,38 @@
using Microsoft.AspNetCore.Mvc;
using GeradoresService;
namespace GeradoresWS.Controllers
{
[ApiController]
[Route("[controller]")]
public class GenerateController : Controller
{
#region NIF
[HttpGet("GenerateNIF")]
public string GenerateNIF(string? type)
{
return NIF.Generate(type);
}
[HttpGet("ValidateNIF")]
public bool ValidateNIF(string nif)
{
return NIF.Validate(nif);
}
#endregion
#region NISS
[HttpGet("GenerateNISS")]
public string GenerateNISS()
{
return NISS.Generate();
}
[HttpGet("ValidateNISS")]
public bool ValidateNISS(string nif)
{
return NISS.Validate(nif);
}
#endregion
}
}

View File

@@ -0,0 +1,33 @@
using Microsoft.AspNetCore.Mvc;
namespace GeradoresWS.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}

22
GeradoresWS/Dockerfile Normal file
View File

@@ -0,0 +1,22 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["GeradoresWS/GeradoresWS.csproj", "GeradoresWS/"]
RUN dotnet restore "GeradoresWS/GeradoresWS.csproj"
COPY . .
WORKDIR "/src/GeradoresWS"
RUN dotnet build "GeradoresWS.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "GeradoresWS.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "GeradoresWS.dll"]

View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>df357c84-88b1-4100-86d1-889166b333f7</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GeradoresService\GeradoresService.csproj" />
</ItemGroup>
</Project>

25
GeradoresWS/Program.cs Normal file
View File

@@ -0,0 +1,25 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,48 @@
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5053"
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7266;http://localhost:5053"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"publishAllPorts": true,
"useSSL": false
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:29191",
"sslPort": 44329
}
}
}

View File

@@ -0,0 +1,13 @@
namespace GeradoresWS
{
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}