Tooark.Securities 2.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tooark.Securities --version 2.3.0
                    
NuGet\Install-Package Tooark.Securities -Version 2.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tooark.Securities" Version="2.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tooark.Securities" Version="2.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Tooark.Securities" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Tooark.Securities --version 2.3.0
                    
#r "nuget: Tooark.Securities, 2.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Tooark.Securities@2.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Tooark.Securities&version=2.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Tooark.Securities&version=2.3.0
                    
Install as a Cake Tool

Tooark.Securities

Biblioteca de segurança para aplicações .NET, fornecendo serviços de criptografia AES e autenticação JWT com suporte a múltiplos algoritmos.

📦 Conteúdo do Pacote

Serviços

Classe Descrição
JwtTokenService Serviço para criação e validação de tokens JWT
CryptographyService Serviço de criptografia/descriptografia AES-256

Interfaces

Interface Descrição
IJwtTokenService Contrato para manipulação de tokens JWT

DTOs

Classe Descrição
JwtTokenDto Dados para criação de token (id, login, security)
UserTokenDto Resultado da validação do token

Options

Classe Descrição
JwtOptions Configurações do JWT (algoritmo, chaves, issuer, audience, expiração)
CryptographyOptions Configurações de criptografia (algoritmo, secret)

Extensions

Classe Descrição
RoleClaimsExtension Extensão para manipulação de claims de função

🔧 Instalação

dotnet add package Tooark.Securities

⚙️ Configuração

appsettings.json

Para configurar os serviços de segurança, adicione as seções Jwt e Cryptography no seu arquivo appsettings.json:

Configuração exemplo para token JWT com algoritmos simétricos (utiliza Secret para assinatura e validação):

{
  "Jwt": {
    "Algorithm": "HS256",
    "Secret": "sua-chave-secreta-com-pelo-menos-32-caracteres",
    "Issuer": "sua-aplicacao",
    "Audience": "seus-clientes",
    "Expires": 60
  }
}

Configuração exemplo para token JWT com algoritmos assimétricos (PrivateKey obrigatória para assinatura, PublicKey obrigatória para validação):

{
  "Jwt": {
    "Algorithm": "RS256",
    "PrivateKey": "sua-chave-privada-em-base64",
    "PublicKey": "sua-chave-publica-em-base64",
    "Issuer": "sua-aplicacao",
    "Audience": "seus-clientes",
    "Expires": 60
  }
}

Configuração exemplo para criptografia AES:

{
  "Cryptography": {
    "Algorithm": "GCM",
    "Secret": "sua-chave-secreta-com-32-caracteres"
  }
}

Program.cs

using Tooark.Securities.Injections;

var builder = WebApplication.CreateBuilder(args);

// Adiciona os serviços de segurança
builder.Services.AddTooarkSecurities(builder.Configuration);

var app = builder.Build();

Ou, para adicionar apenas o serviço de token JWT:

using Tooark.Securities.Injections;

var builder = WebApplication.CreateBuilder(args);

// Adiciona os serviços de segurança
builder.Services.AddTooarkJwtToken(builder.Configuration);

var app = builder.Build();

Ou, para adicionar apenas o serviço de criptografia:

using Tooark.Securities.Injections;

var builder = WebApplication.CreateBuilder(args);

// Adiciona os serviços de segurança
builder.Services.AddTooarkCryptography(builder.Configuration);

var app = builder.Build();

🔐 JWT - Algoritmos Suportados

Algoritmos Simétricos (HMAC)

Algoritmo Descrição Requisitos
HS256 HMAC-SHA256 Secret (≥32 caracteres)
HS384 HMAC-SHA384 Secret (≥48 caracteres)
HS512 HMAC-SHA512 Secret (≥64 caracteres)

Algoritmos Assimétricos (RSA)

Algoritmo Descrição Requisitos
RS256 RSA-SHA256 PrivateKey/PublicKey (≥2048 bits)
RS384 RSA-SHA384 PrivateKey/PublicKey (≥2048 bits)
RS512 RSA-SHA512 PrivateKey/PublicKey (≥2048 bits)
PS256 RSA-PSS-SHA256 PrivateKey/PublicKey (≥2048 bits)
PS384 RSA-PSS-SHA384 PrivateKey/PublicKey (≥2048 bits)
PS512 RSA-PSS-SHA512 PrivateKey/PublicKey (≥2048 bits)

Algoritmos Assimétricos (ECDsa)

Algoritmo Descrição Curva Requerida
ES256 ECDSA-SHA256 P-256 (secp256r1)
ES384 ECDSA-SHA384 P-384 (secp384r1)
ES512 ECDSA-SHA512 P-521 (secp521r1)

🔒 Criptografia - Algoritmos Suportados

Algoritmo Modo Descrição
GCM AES-256-GCM Recomendado - Authenticated encryption
CBC AES-256-CBC Modo tradicional com IV aleatório
CBCUnsafe AES-256-CBC ⚠️ Legado - IV zerado (não recomendado)

📝 Exemplos de Uso

JWT - Criação e Validação de Token

Configuração com HMAC (Simétrico)
{
  "Jwt": {
    "Algorithm": "HS256",
    "Secret": "minha-chave-secreta-super-segura-32chars",
    "Issuer": "minha-api",
    "Audience": "meus-clientes",
    "Expires": 60
  }
}
Configuração com RSA (Assimétrico)
{
  "Jwt": {
    "Algorithm": "RS256",
    "PrivateKey": "MIIEvQIBADANBgkqh...chave-privada-base64...",
    "PublicKey": "MIIBIjANBgkqhkiG9w...chave-publica-base64...",
    "Issuer": "minha-api",
    "Audience": "meus-clientes",
    "Expires": 60
  }
}
Criando um Token
public class AuthController : ControllerBase
{
    private readonly IJwtTokenService _jwtService;

    public AuthController(IJwtTokenService jwtService)
    {
        _jwtService = jwtService;
    }

    [HttpPost("login")]
    public IActionResult Login(LoginRequest request)
    {
        // Validar credenciais...

        // Criar dados do token
        var tokenData = new JwtTokenDto(
            id: user.Id,
            login: user.Email,
            security: user.SecurityStamp
        );

        // Gerar token
        var token = _jwtService.Create(tokenData);

        return Ok(new { Token = token });
    }

    // Com audience customizada e claims extras
    [HttpPost("login-custom")]
    public IActionResult LoginCustom(LoginRequest request)
    {
        var tokenData = new JwtTokenDto(user.Id, user.Email, user.SecurityStamp);

        var extraClaims = new[]
        {
            new Claim("role", "admin"),
            new Claim("department", "IT")
        };

        var token = _jwtService.Create(tokenData, audience: "app-mobile", extraClaims: extraClaims);

        return Ok(new { Token = token });
    }
}
Validando um Token
[HttpGet("validate")]
public IActionResult ValidateToken([FromHeader] string authorization)
{
    var token = authorization.Replace("Bearer ", "");

    var result = _jwtService.Validate(token);

    if (!string.IsNullOrEmpty(result.ErrorToken))
    {
        return Unauthorized(new { Error = result.ErrorToken });
    }

    return Ok(new
    {
        UserId = result.Id,
        Login = result.Login,
        // Ou usando helpers
        GuidId = result.GetGuidId,
        IntId = result.GetIntId
    });
}

Criptografia - Encrypt e Decrypt

Configuração
{
  "Cryptography": {
    "Algorithm": "GCM",
    "Secret": "minha-chave-secreta-32-caracteres"
  }
}
Usando CryptographyService
public class DataProtectionService
{
    private readonly CryptographyService _crypto;

    public DataProtectionService(IOptions<CryptographyOptions> options)
    {
        _crypto = new CryptographyService(options);
    }

    public string ProtectSensitiveData(string plainText)
    {
        // Retorna texto criptografado em Base64
        return _crypto.Encrypt(plainText);
    }

    public string UnprotectData(string encryptedText)
    {
        // Retorna texto original descriptografado
        return _crypto.Decrypt(encryptedText);
    }
}
Exemplo Completo
// Configurar no Program.cs
builder.Services.Configure<CryptographyOptions>(
    builder.Configuration.GetSection(CryptographyOptions.Section));
builder.Services.AddSingleton<CryptographyService>();

// Usar no serviço
public class UserService
{
    private readonly CryptographyService _crypto;

    public UserService(CryptographyService crypto)
    {
        _crypto = crypto;
    }

    public void SaveUser(User user)
    {
        // Criptografar dados sensíveis antes de salvar
        user.CreditCard = _crypto.Encrypt(user.CreditCard);
        user.SSN = _crypto.Encrypt(user.SSN);

        // Salvar no banco...
    }

    public User GetUser(int id)
    {
        var user = // Buscar do banco...

        // Descriptografar dados sensíveis
        user.CreditCard = _crypto.Decrypt(user.CreditCard);
        user.SSN = _crypto.Decrypt(user.SSN);

        return user;
    }
}

🔑 Gerando Chaves

Chave para HMAC (HS256/HS384/HS512)

# Gerar chave aleatória de 32 bytes (256 bits) para HS256
openssl rand -base64 32

# Gerar chave aleatória de 64 bytes (512 bits) para HS512
openssl rand -base64 64

Chaves RSA (RS256/PS256)

# Gerar chave privada RSA de 2048 bits
openssl genrsa -out private.pem 2048

# Extrair chave pública
openssl rsa -in private.pem -pubout -out public.pem

# Converter para formato PKCS8 (recomendado)
openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in private.pem -out private_pkcs8.pem

# Obter chave em Base64 (sem headers)
cat private_pkcs8.pem | grep -v "BEGIN\|END" | tr -d '\n'
cat public.pem | grep -v "BEGIN\|END" | tr -d '\n'

Chaves ECDsa (ES256/ES384/ES512)

# ES256 (P-256)
openssl ecparam -genkey -name prime256v1 -noout -out ec_private.pem
openssl ec -in ec_private.pem -pubout -out ec_public.pem

# ES384 (P-384)
openssl ecparam -genkey -name secp384r1 -noout -out ec_private.pem

# ES512 (P-521)
openssl ecparam -genkey -name secp521r1 -noout -out ec_private.pem

# Converter para PKCS8
openssl pkcs8 -topk8 -nocrypt -in ec_private.pem -out ec_private_pkcs8.pem

📋 Dependências

Pacote Versão Descrição
Microsoft.AspNetCore.Authentication.JwtBearer 8.x Autenticação JWT para ASP.NET Core

🎯 Boas Práticas

JWT

  1. Use algoritmos assimétricos (RS/PS/ES) em produção - Permite validar tokens sem expor a chave de assinatura
  2. Configure Expires apropriadamente - Tokens de curta duração são mais seguros
  3. Use Issuer e Audience - Previne uso indevido de tokens entre aplicações
  4. Armazene chaves em Secret Manager - Nunca commite chaves no código fonte

Criptografia

  1. Prefira GCM sobre CBC - GCM fornece autenticação integrada
  2. Nunca use CBCUnsafe - Apenas para compatibilidade com sistemas legados
  3. Use chaves de 32 caracteres - Garante AES-256
  4. Gere chaves aleatórias - Use openssl rand ou RandomNumberGenerator

⚠️ Erros Comuns

Código Descrição Solução
Jwt.KeyNotConfigured Chave não configurada Configure Secret (HMAC) ou PrivateKey/PublicKey (RSA/ECDsa)
Jwt.InvalidKeySize Tamanho da chave RSA inválido Use chaves RSA de pelo menos 2048 bits
Jwt.InvalidKeyCurve Curva ECDsa incompatível Use a curva correta para o algoritmo (P-256 para ES256, etc.)
Jwt.InvalidKey Chave malformada Verifique se a chave está em Base64 válido
Jwt.AlgorithmNotSupported Algoritmo não suportado Use HS/RS/PS/ES + 256/384/512
Token.Expired Token expirado Gere um novo token
Token.Invalid Token inválido Verifique formato e assinatura
Cryptography.SecretNotConfigured Secret não configurado Configure Secret nas opções

Contribuição

Contribuições são bem-vindas! Sinta-se à vontade para abrir issues e pull requests no repositório Tooark.Entities.

📄 Licença

Este projeto está licenciado sob a licença BSD 3-Clause. Veja o arquivo LICENSE para mais detalhes.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Tooark.Securities:

Package Downloads
Tooark

Package with all Tooark resources for .NET applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.0 71 1/21/2026
3.1.0 96 1/10/2026
3.0.0 98 1/9/2026
2.3.0 64 1/21/2026
2.2.1 110 1/6/2026