Snail.Toolkit.Blazor.Extensions.Cryptography
1.0.0
dotnet add package Snail.Toolkit.Blazor.Extensions.Cryptography --version 1.0.0
NuGet\Install-Package Snail.Toolkit.Blazor.Extensions.Cryptography -Version 1.0.0
<PackageReference Include="Snail.Toolkit.Blazor.Extensions.Cryptography" Version="1.0.0" />
<PackageVersion Include="Snail.Toolkit.Blazor.Extensions.Cryptography" Version="1.0.0" />
<PackageReference Include="Snail.Toolkit.Blazor.Extensions.Cryptography" />
paket add Snail.Toolkit.Blazor.Extensions.Cryptography --version 1.0.0
#r "nuget: Snail.Toolkit.Blazor.Extensions.Cryptography, 1.0.0"
#addin nuget:?package=Snail.Toolkit.Blazor.Extensions.Cryptography&version=1.0.0
#tool nuget:?package=Snail.Toolkit.Blazor.Extensions.Cryptography&version=1.0.0
Toolkit.Blazor.Extensions.Cryptography
A Blazor extension providing JavaScript-compatible symmetric and asymmetric cryptography operations through Web Crypto API interop.
Features
Symmetric Encryption (AES-based)
- Encrypt/decrypt strings with automatic Base64 conversion
- Configurable passphrase, IV, and salt
- JavaScript-compatible output
Asymmetric Encryption (RSA-OAEP)
- Generate key pairs
- Encrypt with public key
- Decrypt with private key
- Configurable modulus length and hash algorithm
Installation
Add the package to your Blazor project:
dotnet add package Snail.Toolkit.Blazor.Extensions.Cryptography
Configuration
Symmetric Encryption Setup
Choose one of these configuration methods:
- Programmatic configuration:
builder.Services.AddJsSymmetricCipher(o =>
{
o.Passphrase = "secure-passphrase-here";
o.IV = "unique-iv-value-16bytes";
o.Salt = "unique-salt-value-16bytes";
o.Iterations = 1000;
o.DesiredKeyLength = 32; // 256 bits
o.HashMethod = HashAlgo.SHA512;
});
- Configuration-based (appsettings.json):
builder.Services.AddJsSymmetricCipher(builder.Configuration);
- Default values (for development only):
builder.Services.AddJsSymmetricCipher();
Asymmetric Encryption Setup
builder.Services.AddJsAsymmetricCipher(o =>
{
o.ModulusLengthInBits = 4096;
o.HashMethod = HashAlgo.SHA512;
});
Sample appsettings.json
{
"Cryptography": {
"Passphrase": "secure-passphrase-here",
"IV": "unique-iv-value-16bytes",
"Salt": "unique-salt-value-16bytes",
"Iterations": 1000,
"DesiredKeyLength": 32,
"HashMethod": "SHA512"
}
}
Usage
Symmetric Encryption
public class MyService
{
private readonly ISymmetricCipher _cipher;
public MyService(ISymmetricCipher cipher)
{
_cipher = cipher;
}
public async Task<string> SecureOperation(string sensitiveData)
{
// Encrypt
var encrypted = await _cipher.EncryptToBase64Async(sensitiveData);
// Decrypt
var decrypted = await _cipher.DecryptFromBase64Async(encrypted);
return decrypted;
}
}
Asymmetric Encryption
public class MySecureService
{
private readonly IJsAsymmetricCipher _cipher;
public MySecureService(IJsAsymmetricCipher cipher)
{
_cipher = cipher;
}
public async Task<(byte[] encrypted, string privateKey)> EncryptData(string data)
{
var result = await _cipher.EncryptAsync(data);
return (result.EncryptedData, result.PrivateKey);
}
public async Task<string> DecryptData(string privateKey, byte[] encrypted)
{
return await _cipher.DecryptAsync(privateKey, encrypted);
}
}
Security Considerations
Supported Hash Algorithms
- SHA1 (legacy)
- SHA256
- SHA384
- SHA512
Unsupported Algorithms (will throw exceptions)
- MD5 (insecure)
- SHA3 variants (not compatible with Web Crypto API)
Best Practices
- Always use strong passphrases (min 16 chars)
- Generate unique IVs for each encryption operation
- Store private keys securely
- Use at least SHA256 for hashing
- Prefer 4096-bit keys for asymmetric encryption
JavaScript Interop
The library uses these JavaScript modules from your wwwroot
:
/_content/Snail.Toolkit.Blazor.Extensions.Cryptography/symmetric.js
/_content/Snail.Toolkit.Blazor.Extensions.Cryptography/asymmetric.js
License
Toolkit.Blazor.Extensions.Cryptography is a free and open source project, released under the permissible MIT license.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
-
net9.0
- Microsoft.Extensions.Configuration (>= 9.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.5)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.5)
- Microsoft.IdentityModel.Tokens (>= 8.0.1)
- Microsoft.JSInterop (>= 9.0.5)
- Snail.Toolkit.Cryptography (>= 1.0.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Snail.Toolkit.Blazor.Extensions.Cryptography:
Package | Downloads |
---|---|
Snail.Toolkit.Blazor.Extensions.LocalStorage
Extension for the framework Blazor |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 86 | 5/31/2025 |