Gerações NIF NISS
This commit is contained in:
9
GeradoresService/GeradoresService.csproj
Normal file
9
GeradoresService/GeradoresService.csproj
Normal 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
89
GeradoresService/NIF.cs
Normal 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
90
GeradoresService/NISS.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user