Pandatech.Crypto
2.6.0
See the version list below for details.
dotnet add package Pandatech.Crypto --version 2.6.0
NuGet\Install-Package Pandatech.Crypto -Version 2.6.0
<PackageReference Include="Pandatech.Crypto" Version="2.6.0" />
paket add Pandatech.Crypto --version 2.6.0
#r "nuget: Pandatech.Crypto, 2.6.0"
// Install Pandatech.Crypto as a Cake Addin #addin nuget:?package=Pandatech.Crypto&version=2.6.0 // Install Pandatech.Crypto as a Cake Tool #tool nuget:?package=Pandatech.Crypto&version=2.6.0
PandaTech.Crypto
Introduction
PandaTech.Crypto is a wrapper library that consolidates several widely used cryptographic libraries and tools into
one
simple-to-use package. It eliminates the need for multiple dependencies, excessive using
directives, and
duplicated
code, offering an intuitive API to streamline most popular cryptographic tasks.
Whether you need to encrypt data, hash passwords, or generate secure random tokens, PandaTech.Crypto provides lightweight abstractions over popular cryptographic solutions, ensuring simplicity and usability without sacrificing performance.
The Argon2Id password hashing is optimized to run efficiently even in resource-constrained environments (e.g., hash generation under 500ms on a container with 1 vCore and 1GB of RAM). Other operations such as AES encryption, SHA hashing, and GZip compression are lightweight enough for almost any environment.
Installation
Install the NuGet package via the Package Manager Console:
Install-Package Pandatech.Crypto
How to Use
Configuring Dependency Injection
Add the following code to your Program.cs file to configure AES256 and Argon2Id services with minimal setup:
using Pandatech.Crypto;
// For Aes256
builder.services.AddPandatechCryptoAes256(options =>
{
options.Key = "YourAes256KeyHere"; // Make sure to use a secure key
});
// For Argon2Id default configuration
builder.services.AddPandatechCryptoArgon2Id();
// For Argon2Id overriding default configurations
builder.services.AddPandatechCryptoArgon2Id(options =>
{
options.SaltSize = 16;
options.DegreeOfParallelism = 8;
options.Iterations = 5;
options.MemorySize = 128 * 1024;
});
AES256 Class
Encryption/Decryption methods with hashing
byte[] cipherText = aes256.Encrypt("your-plaintext");
string plainText = aes256.Decrypt(cipherText);
Encryption/Decryption methods without hashing
byte[] cipherText = aes256.EncryptWithoutHash("your-plaintext");
string plainText = aes256.DecryptWithoutHash(cipherText);
Encryption/Decryption methods with custom key (overriding options for one time)
string customKey = "your-custom-base64-encoded-key";
byte[] cipherText = aes256.Encrypt("your-plaintext", customKey);
string plainText = aes256.Decrypt(cipherText, customKey);
Stream-based Encryption/Decryption methods
The AES256 class also supports stream-based operations, allowing for encryption and decryption directly on streams, which is ideal for handling large files or data streams efficiently.
using var inputStream = new MemoryStream(Encoding.UTF8.GetBytes("your-plaintext"));
using var outputStream = new MemoryStream();
aes256.EncryptStream(inputStream, outputStream, "your-custom-base64-encoded-key");
byte[] encryptedBytes = outputStream.ToArray();
using var inputStream = new MemoryStream(encryptedBytes);
using var outputStream = new MemoryStream();
aes256.DecryptStream(inputStream, outputStream, "your-custom-base64-encoded-key");
string decryptedText = Encoding.UTF8.GetString(outputStream.ToArray());
Notes
- IV: A random IV is generated for each Encryption, enhancing security.
- PaddingMode: PKCS7
- Hashing: The AES256 class by defaults also uses SHA3 512 hash before encryption and stores it in front of byte array in order to be able to do unique cheques and other operations on encrypted fields. For example imagine you are encrypting emails in your software and also want that emails to be unique. With our Aes256 class by default your emails will be unique as in front will be the unique hash.
Argon2id Class
Default Configurations
- Salt: A random salt is generated for each password hash, enhancing security.
- DegreeOfParallelism: 8
- Iterations: 5
- MemorySize: 128 MB
Examples on usage
// Example usage for hashing
var hashedPassword = _argon2Id.HashPassword("yourPassword");
// Example usage for verifying a hash
var isPasswordValid = _argon2Id.VerifyHash("yourPassword", hashedPassword);
Random Class
var randomBytes = Random.GenerateBytes(16);
var aesKey = Random.GenerateAes256KeyString();
var unimaginableUniqueAndRandomToken = Random.GenerateSecureToken() //256-bit token in string format
Password Class
var includeUppercase = true;
var includeLowercase = true;
var includeDigits = true;
var includeSpecialChars = true;
//Method for generating random password
string password = Password.GenerateRandom(16, includeUppercase, includeLowercase, includeDigits, includeSpecialChars);
//Method for validation of password
bool isValid = Password.Validate(password, 16, includeUppercase, includeLowercase, includeDigits, includeSpecialChars);
Sha2 Class
The Sha2
class simplifies HMAC-SHA256 operations by offering byte array, hex, and Base64 outputs.
// Prepare the key and message
var key = Encoding.UTF8.GetBytes("your-secret-key");
var message = "HelloWorld";
// Compute HMAC-SHA256 as a byte array
byte[] hashBytes = Sha2.ComputeHmacSha256(key, message);
// Get HMAC-SHA256 as a hex string
string hexHash = Sha2.GetHmacSha256Hex(key, message);
// Output: 2e91612bb72b29d82f32789d063de62d5897a4ee5d3b5d34459801b94397b099
// Get HMAC-SHA256 as a Base64 string
string base64Hash = Sha2.GetHmacSha256Base64(key, message);
// Output: LpFhK7crKdgvMnidBj3mLViXpO5dO100RZgBuUOXsJk=
Sha3 Class
// Example usage for generating hash
var sha3Hash = Sha3.Hash("yourPlainText");
// Example usage for verifying a hash
var isHashValid = Sha3.VerifyHash("yourPlainText", sha3Hash);
GZip Class
Compression and Decompression
The GZip
class provides methods for compressing and decompressing data using GZip. It supports operations on strings,
byte arrays, and streams.
Example usage for compressing and decompressing a string:
using Pandatech.Crypto;
// Compress a string
string data = "Sample Data";
byte[] compressedData = GZip.Compress(data);
// Decompress back to string
string decompressedData = Encoding.UTF8.GetString(GZip.Decompress(compressedData));
Example usage for compressing and decompressing with streams:
using var inputStream = new MemoryStream(Encoding.UTF8.GetBytes("Sample Data"));
using var compressedStream = new MemoryStream();
GZip.Compress(inputStream, compressedStream);
byte[] compressedData = compressedStream.ToArray();
using var inputStream = new MemoryStream(compressedData);
using var decompressedStream = new MemoryStream();
GZip.Decompress(inputStream, decompressedStream);
string decompressedData = Encoding.UTF8.GetString(decompressedStream.ToArray());
Mask Class
The Mask
class in the PandaTech.Crypto library provides methods to mask sensitive information like email addresses and
phone numbers, ensuring that they are partially hidden and thus safeguarded.
Masking Email Addresses
The MaskEmail
method masks the local part of an email address, showing only the first two characters and replacing the
rest with asterisks (*), keeping the domain part intact.
// Example usage for masking an email
string maskedEmail = Mask.MaskEmail("example@email.com");
// Output: "ex*****@email.com"
// Example usage for masking a phone number
string maskedPhone = Mask.MaskPhoneNumber("1234567890");
// Output: "******7890"
// You can also use the MaskEmail and MaskPhoneNumber methods as extension methods on strings
string maskedEmail = "example@email.com";
string maskedPhone = "1234567890";
string maskedEmail = maskedEmail.MaskEmail();
string maskedPhone = maskedPhone.MaskPhoneNumber();
License
PandaTech.Crypto is licensed under the MIT License.
Product | Versions 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. |
-
net8.0
- BouncyCastle.NetCore (>= 2.2.1)
- Konscious.Security.Cryptography.Argon2 (>= 1.3.1)
- Microsoft.Extensions.DependencyInjection (>= 8.0.1)
- Pandatech.RegexBox (>= 2.0.1)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Pandatech.Crypto:
Package | Downloads |
---|---|
PandaTech.IEnumerableFilters
This NuGet helps with filtering tables. |
|
Pandatech.EFCoreQueryMagic
Unlock the full potential of your Entity Framework Core applications with Pandatech.EFCoreQueryMagic. This innovative package empowers developers to seamlessly create dynamic, complex queries and filters for SQL tables without diving deep into the intricacies of LINQ or manual query construction. Designed to enhance productivity and maintainability, EFCoreQueryMagic automates the translation of front-end filter requests into optimized, ready-to-execute EF Core queries. Embrace the magic of streamlined data retrieval and manipulation, and elevate your applications to new heights of efficiency and performance. |
|
Pandatech.SharedKernel
Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
4.0.0 | 26 | 11/21/2024 |
3.0.0 | 81 | 10/28/2024 |
2.6.1 | 103 | 10/19/2024 |
2.6.0 | 92 | 10/19/2024 |
2.5.1 | 108 | 10/18/2024 |
2.5.0 | 146 | 6/21/2024 |
2.4.1 | 109 | 6/14/2024 |
2.4.0 | 120 | 6/13/2024 |
2.3.2 | 246 | 5/9/2024 |
2.3.1 | 602 | 3/6/2024 |
2.3.0 | 125 | 3/6/2024 |
2.2.11 | 115 | 3/6/2024 |
2.2.10 | 119 | 3/1/2024 |
2.2.9 | 143 | 2/17/2024 |
2.2.8 | 102 | 2/17/2024 |
2.2.7 | 131 | 2/12/2024 |
2.2.6 | 141 | 1/23/2024 |
2.2.5 | 111 | 1/23/2024 |
2.2.4 | 122 | 1/19/2024 |
2.2.3 | 477 | 11/29/2023 |
2.2.2 | 120 | 11/29/2023 |
2.2.1 | 227 | 11/23/2023 |
2.2.0 | 153 | 11/21/2023 |
2.1.10 | 145 | 11/11/2023 |
2.1.9 | 133 | 11/9/2023 |
2.1.8 | 227 | 11/7/2023 |
2.1.7 | 205 | 11/6/2023 |
2.1.6 | 121 | 11/3/2023 |
2.1.5 | 143 | 11/2/2023 |
2.1.4 | 127 | 11/1/2023 |
2.1.3 | 135 | 11/1/2023 |
2.1.2 | 134 | 10/31/2023 |
2.1.1 | 135 | 10/31/2023 |
2.1.0 | 134 | 10/31/2023 |
2.0.0 | 326 | 10/30/2023 |
1.1.6 | 148 | 10/30/2023 |
1.1.5 | 141 | 10/27/2023 |
1.1.4 | 125 | 10/27/2023 |
1.1.3 | 151 | 10/27/2023 |
1.1.2 | 164 | 10/16/2023 |
1.1.1 | 163 | 10/14/2023 |
1.1.0 | 154 | 10/14/2023 |
1.0.0 | 151 | 10/13/2023 |
HMAC-SHA256 support added