![]() |
VOOZH | about |
dotnet add package TPJ.Encrypt --version 10.0.0
NuGet\Install-Package TPJ.Encrypt -Version 10.0.0
<PackageReference Include="TPJ.Encrypt" Version="10.0.0" />
<PackageVersion Include="TPJ.Encrypt" Version="10.0.0" />Directory.Packages.props
<PackageReference Include="TPJ.Encrypt" />Project file
paket add TPJ.Encrypt --version 10.0.0
#r "nuget: TPJ.Encrypt, 10.0.0"
#:package TPJ.Encrypt@10.0.0
#addin nuget:?package=TPJ.Encrypt&version=10.0.0Install as a Cake Addin
#tool nuget:?package=TPJ.Encrypt&version=10.0.0Install as a Cake Tool
TPJ.Encrypt is a simple .NET 10 package for common encryption and secret-handling tasks.
It includes:
PasswordHasher for one-way password hashing using Argon2idEncryptAes for two-way AES encryption and decryptionAzureKeyVault helpers for reading secrets and connection strings from Azure Key VaultUse this package when you need to:
AES-GCM for new implementationsPasswordHasherUse PasswordHasher for passwords and other values that should not be decrypted later.
Argon2idEncryptAesUse EncryptAes when you need to encrypt data and decrypt it later.
AES-CBC methods for key/IV based encryptionAES-GCM methods for authenticated encryptionFor new code, prefer the authenticated
AES-GCMmethods such asEncryptWithAuthenticationandDecryptWithAuthentication.
AzureKeyVaultUse AzureKeyVault when your app needs to:
dotnet add package TPJ.Encrypt
using TPJ.Encrypt;
var (hash, metadata) = PasswordHasher.HashPassword("MySecurePassword123!");
Console.WriteLine($"Hash: {hash}");
Console.WriteLine($"Metadata: {metadata}");
using TPJ.Encrypt;
var (hash, metadata) = PasswordHasher.HashPassword("MySecurePassword123!");
var isValid = PasswordHasher.VerifyPassword("MySecurePassword123!", hash, metadata);
var isInvalid = PasswordHasher.VerifyPassword("WrongPassword", hash, metadata);
Console.WriteLine(isValid); // True
Console.WriteLine(isInvalid); // False
using TPJ.Encrypt;
var (key, nonce) = EncryptAes.GenerateAesGcmKeyNonce();
var encrypted = EncryptAes.EncryptWithAuthentication("Hello world", key, nonce);
var decrypted = EncryptAes.DecryptWithAuthentication(encrypted, key);
Console.WriteLine(decrypted); // Hello world
using TPJ.Encrypt;
var (keyBytes, nonceBytes) = EncryptAes.GenerateAesGcmKeyNonce();
var key = Convert.ToBase64String(keyBytes);
var nonce = Convert.ToBase64String(nonceBytes);
var encrypted = EncryptAes.EncryptWithAuthenticationToBase64("Secret message", key, nonce);
var decrypted = EncryptAes.DecryptWithAuthenticationFromBase64(encrypted, key);
Console.WriteLine(decrypted); // Secret message
This example shows both password hashing and AES encryption in a simple console app.
using TPJ.Encrypt;
Console.WriteLine("TPJ.Encrypt console demo");
// Password hashing
var password = "MySecurePassword123!";
var (passwordHash, metadata) = PasswordHasher.HashPassword(password);
Console.WriteLine($"Password hash: {passwordHash}");
Console.WriteLine($"Password valid: {PasswordHasher.VerifyPassword(password, passwordHash, metadata)}");
// AES-GCM encryption
var (key, nonce) = EncryptAes.GenerateAesGcmKeyNonce();
var encryptedBytes = EncryptAes.EncryptWithAuthentication("Sensitive console data", key, nonce);
var decryptedText = EncryptAes.DecryptWithAuthentication(encryptedBytes, key);
Console.WriteLine($"Encrypted: {Convert.ToBase64String(encryptedBytes)}");
Console.WriteLine($"Decrypted: {decryptedText}");
This example shows how to use the package in an ASP.NET Core minimal API.
using TPJ.Encrypt;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var (key, nonce) = EncryptAes.GenerateAesGcmKeyNonce();
app.MapPost("/hash-password", (PasswordRequest request) =>
{
var (hash, metadata) = PasswordHasher.HashPassword(request.Value);
return Results.Ok(new { hash, metadata });
});
app.MapPost("/verify-password", (VerifyPasswordRequest request) =>
{
var isValid = PasswordHasher.VerifyPassword(request.Password, request.Hash, request.Metadata);
return Results.Ok(new { isValid });
});
app.MapPost("/encrypt", (TextRequest request) =>
{
var encrypted = EncryptAes.EncryptWithAuthentication(request.Value, key, nonce);
return Results.Ok(new { encrypted = Convert.ToBase64String(encrypted) });
});
app.MapPost("/decrypt", (EncryptedRequest request) =>
{
var encryptedBytes = Convert.FromBase64String(request.Value);
var decrypted = EncryptAes.DecryptWithAuthentication(encryptedBytes, key);
return Results.Ok(new { decrypted });
});
app.Run();
record PasswordRequest(string Value);
record VerifyPasswordRequest(string Password, string Hash, string Metadata);
record TextRequest(string Value);
record EncryptedRequest(string Value);
POST /hash-password
{
"value": "MySecurePassword123!"
}
POST /encrypt
{
"value": "Hello from the API"
}
If you store secret names and credential settings in configuration, you can use AzureKeyVault to retrieve secrets.
using TPJ.Encrypt;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/db-connection", async (IConfiguration configuration) =>
{
var connectionString = await AzureKeyVault.GetConnectionStringAsync(configuration, "DefaultConnection");
return Results.Ok(new { connectionString });
});
app.Run();
Example configuration structure:
{
"ConnectionStrings": {
"DefaultConnection": "Server=server-name;Database=my-db;User Id=my-user;Password=##Password##;"
},
"TPJ": {
"Encrypt": {
"Azure": {
"KeyVault": {
"Url": "https://your-keyvault-name.vault.azure.net/",
"Secrets": {
"DbPassword": "my-database-password-secret-name"
},
"EnvironmentVariables": {
"TenantId": "AZURE_TENANT_ID",
"ClientId": "AZURE_CLIENT_ID",
"ClientSecret": "AZURE_CLIENT_SECRET"
}
}
}
}
}
}
PasswordHasher for passwords, not reversible encryption.EncryptAes when you need to decrypt the data later.AES-GCM for new development.| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
V10.0.0 now runs on .NET 10, uses Argon2 and includes helper class for using Azure Key Vault to store secrets see github for more details