{"id":21623027,"url":"https://github.com/pandatecham/be-lib-pandatech-crypto","last_synced_at":"2025-03-18T19:27:16.850Z","repository":{"id":200152636,"uuid":"703998908","full_name":"PandaTechAM/be-lib-pandatech-crypto","owner":"PandaTechAM","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-17T12:04:53.000Z","size":258,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"development","last_synced_at":"2025-02-17T13:22:03.644Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PandaTechAM.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-12T10:29:56.000Z","updated_at":"2025-02-17T12:04:19.000Z","dependencies_parsed_at":"2024-03-06T07:45:42.845Z","dependency_job_id":"2f701efb-3287-4da5-af2f-3fe5158448a5","html_url":"https://github.com/PandaTechAM/be-lib-pandatech-crypto","commit_stats":null,"previous_names":["pandatecham/be-lib-pandatech-crypto"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PandaTechAM%2Fbe-lib-pandatech-crypto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PandaTechAM%2Fbe-lib-pandatech-crypto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PandaTechAM%2Fbe-lib-pandatech-crypto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PandaTechAM%2Fbe-lib-pandatech-crypto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PandaTechAM","download_url":"https://codeload.github.com/PandaTechAM/be-lib-pandatech-crypto/tar.gz/refs/heads/development","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244287918,"owners_count":20428896,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-25T00:11:19.662Z","updated_at":"2025-03-18T19:27:16.819Z","avatar_url":"https://github.com/PandaTechAM.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PandaTech.Crypto\n\n## Introduction\n\nPandaTech.Crypto is a **wrapper library** that consolidates several widely used cryptographic libraries and tools into\none\n**simple-to-use package**. It eliminates the need for multiple dependencies, excessive `using` directives, and\nduplicated\ncode, offering an **intuitive API** to streamline **most popular** cryptographic tasks.\n\nWhether you need to **encrypt data**, **hash passwords**, or **generate secure random tokens**, PandaTech.Crypto\nprovides\nlightweight abstractions over popular cryptographic solutions, ensuring simplicity and usability without sacrificing\nperformance.\n\nThe **Argon2Id** password hashing is optimized to run efficiently even in **resource-constrained environments** (e.g.,\nhash\ngeneration under 500ms on a container with 1 vCore and 1GB of RAM). Other operations such as **AES encryption**, **SHA**\nhashing, and **GZip** compression are lightweight enough for almost any environment.\n\n## Installation\n\nInstall the NuGet package via the Package Manager Console:\n\n```bash\nInstall-Package Pandatech.Crypto\n```\n\n## How to Use\n\n### Configuring in Program.cs\n\nUse the following code to configure AES256 and Argon2Id in your `Program.cs`:\n\n```csharp\nusing Pandatech.Crypto.Helpers;\nusing Pandatech.Crypto.Extensions;\n\nvar builder = WebApplication.CreateBuilder(args);\nbuilder.AddAes256Key(\"YourBase64EncodedAes256KeyHere\");\n\n// Optional - Change default Argon2Id configurations. If below method is not called, default configurations will be used.\nbuilder.ConfigureArgon2Id(options =\u003e\n{\n   options.SaltSize = 16;\n   options.DegreeOfParallelism = 8;\n   options.Iterations = 5;\n   options.MemorySize = 128 * 1024;\n}); \n\nvar app = builder.Build();\n\napp.Run();\n\n```\n\n### AES256 Class\n\n**Encryption/Decryption methods with hashing**\n\n```csharp\nusing Pandatech.Crypto.Helpers;\n\n// Encrypt using AES256\nvar encryptedBytes = Aes256.Encrypt(\"your-plaintext\");\n\n// Decrypt AES256-encrypted data\nvar decryptedText = Aes256.Decrypt(encryptedBytes);\n\n```\n\n**Encryption/Decryption methods without hashing**\n\n```csharp\nbyte[] cipherText = aes256.EncryptWithoutHash(\"your-plaintext\");\nstring plainText = aes256.DecryptWithoutHash(cipherText);\n```\n\n**Encryption/Decryption methods with custom key (overriding options for one time)**\n\n```csharp\nstring customKey = \"your-custom-base64-encoded-key\";\n\n// Encrypt with a custom key\nvar encrypted = Aes256.Encrypt(\"your-plaintext\", customKey);\n\n// Decrypt with the same key\nvar decrypted = Aes256.Decrypt(encrypted, customKey);\n```\n\n**Stream-based Encryption/Decryption methods**\n\n```csharp\nusing var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(\"your-plaintext\"));\nusing var outputStream = new MemoryStream();\n\n// Encrypt stream\nAes256.Encrypt(inputStream, outputStream, \"your-base64-key\");\n\n// Decrypt stream\nusing var decryptedStream = new MemoryStream(outputStream.ToArray());\nAes256.Decrypt(decryptedStream, outputStream, \"your-base64-key\");\nstring decryptedText = Encoding.UTF8.GetString(outputStream.ToArray());\n```\n\n**Notes**\n\n1. **IV**: A random IV is generated for each Encryption, enhancing security.\n2. **PaddingMode**: PKCS7\n3. **Hashing**: The AES256 class by defaults also uses SHA3 512 hash before encryption and stores it in front of byte\n   array in order to be able to do unique cheques and other operations on encrypted fields. For example imagine you are\n   encrypting emails in your software and also want that emails to be unique. With our Aes256 class by default your\n   emails will be unique as in front will be the unique hash.\n\n### Argon2id Class\n\n**Default Configurations**\n\n1. **Salt**: A random salt is generated for each password hash, enhancing security.\n2. **DegreeOfParallelism**: 8\n3. **Iterations**: 5\n4. **MemorySize**: 128 MB\n\n**Examples on usage**\n\n```csharp\nusing Pandatech.Crypto.Helpers;\n\n// Hash a password using Argon2Id\nvar hashedPassword = Argon2Id.HashPassword(\"yourPassword\");\n\n// Verify a hashed password\nbool isValid = Argon2Id.VerifyHash(\"yourPassword\", hashedPassword);\n```\n\n### Random Class\n\n```csharp\nvar randomBytes = Random.GenerateBytes(16);\nvar aesKey = Random.GenerateAes256KeyString();\nvar unimaginableUniqueAndRandomToken = Random.GenerateSecureToken() //256-bit token in string format\n```\n\n### Password Class\n\n```csharp\nvar includeUppercase = true;\nvar includeLowercase = true;\nvar includeDigits = true;\nvar includeSpecialChars = true;\n\n//Method for generating random password\nstring password = Password.GenerateRandom(16, includeUppercase, includeLowercase, includeDigits, includeSpecialChars);\n\n//Method for validation of password\nbool isValid = Password.Validate(password, 16, includeUppercase, includeLowercase, includeDigits, includeSpecialChars);\n```\n\n### Sha2 Class\n\nThe `Sha2` class simplifies HMAC-SHA256 operations by offering byte array, hex, and Base64 outputs. It also hat params\nstring[] where the method automatically concatenates all strings and then computes the hash.\n\n```csharp\n// Prepare the key and message\nvar key = Encoding.UTF8.GetBytes(\"secret\");\nvar message1 = \"Hello\";\nvar message2 = \"World\";\n\n// Compute HMAC-SHA256 as a byte array\nbyte[] hashBytes = Sha2.ComputeHmacSha256(key, message1, message2);\n\n// Get HMAC-SHA256 as a hex string\nstring hexHash = Sha2.GetHmacSha256Hex(key, message1, message2);\n// Output: 2e91612bb72b29d82f32789d063de62d5897a4ee5d3b5d34459801b94397b099\n\n// Get HMAC-SHA256 as a Base64 string\nstring base64Hash = Sha2.GetHmacSha256Base64(key, message1, message2);\n// Output: LpFhK7crKdgvMnidBj3mLViXpO5dO100RZgBuUOXsJk=\n```\n\n### Sha3 Class\n\n```csharp\n// Example usage for generating hash\nvar sha3Hash = Sha3.Hash(\"yourPlainText\");\n\n// Example usage for verifying a hash\nvar isHashValid = Sha3.VerifyHash(\"yourPlainText\", sha3Hash);\n```\n\n### GZip Class\n\nCompression and Decompression\nThe `GZip` class provides methods for compressing and decompressing data using GZip. It supports operations on strings,\nbyte arrays, and streams.\n\nExample usage for compressing and decompressing a string:\n\n```csharp\nusing Pandatech.Crypto;\n\n// Compress a string\nstring data = \"Sample Data\";\nbyte[] compressedData = GZip.Compress(data);\n\n// Decompress back to string\nstring decompressedData = Encoding.UTF8.GetString(GZip.Decompress(compressedData));\n```\n\nExample usage for compressing and decompressing with streams:\n\n```csharp\nusing var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(\"Sample Data\"));\nusing var compressedStream = new MemoryStream();\nGZip.Compress(inputStream, compressedStream);\nbyte[] compressedData = compressedStream.ToArray();\n\nusing var inputStream = new MemoryStream(compressedData);\nusing var decompressedStream = new MemoryStream();\nGZip.Decompress(inputStream, decompressedStream);\nstring decompressedData = Encoding.UTF8.GetString(decompressedStream.ToArray());\n```\n\n### Mask Class\n\nThe `Mask` class in the PandaTech.Crypto library provides methods to mask sensitive information like email addresses and\nphone numbers, ensuring that they are partially hidden and thus safeguarded.\n\n#### Masking Email Addresses\n\nThe `MaskEmail` method masks the local part of an email address, showing only the first two characters and replacing the\nrest with asterisks (*), keeping the domain part intact.\n\n```csharp\n// Example usage for masking an email\nstring maskedEmail = Mask.MaskEmail(\"example@email.com\");\n\n// Output: \"ex*****@email.com\"\n// Example usage for masking a phone number\nstring maskedPhone = Mask.MaskPhoneNumber(\"1234567890\");\n\n// Output: \"******7890\"\n\n// You can also use the MaskEmail and MaskPhoneNumber methods as extension methods on strings\nstring maskedEmail = \"example@email.com\";\nstring maskedPhone = \"1234567890\";\n\nstring maskedEmail = maskedEmail.MaskEmail();\nstring maskedPhone = maskedPhone.MaskPhoneNumber();\n```\n\n## License\n\nPandaTech.Crypto is licensed under the MIT License.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpandatecham%2Fbe-lib-pandatech-crypto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpandatecham%2Fbe-lib-pandatech-crypto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpandatecham%2Fbe-lib-pandatech-crypto/lists"}