Skip to content

Commit

Permalink
updated singleton Aes256 and Argon2Id to static classes
Browse files Browse the repository at this point in the history
  • Loading branch information
HaikAsatryan committed Oct 28, 2024
1 parent e0c69fe commit 787ef9e
Show file tree
Hide file tree
Showing 33 changed files with 1,644 additions and 1,473 deletions.
191 changes: 191 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
###############################
# PandaTech Editor Config #
###############################


################################
# ReSharper Generated Settings #
################################

[*]
charset = utf-8-bom
end_of_line = crlf
trim_trailing_whitespace = false
insert_final_newline = false
indent_style = space
indent_size = 4

# Microsoft .NET properties
csharp_preferred_modifier_order = public, private, protected, internal, file, new, static, abstract, virtual, sealed, readonly, override, extern, unsafe, volatile, async, required:suggestion
csharp_style_prefer_utf8_string_literals = true:suggestion
csharp_style_var_elsewhere = true:suggestion
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
dotnet_naming_rule.unity_serialized_field_rule.import_to_resharper = True
dotnet_naming_rule.unity_serialized_field_rule.resharper_description = Unity serialized field
dotnet_naming_rule.unity_serialized_field_rule.resharper_guid = 5f0fdb63-c892-4d2c-9324-15c80b22a7ef
dotnet_naming_rule.unity_serialized_field_rule.severity = warning
dotnet_naming_rule.unity_serialized_field_rule.style = lower_camel_case_style
dotnet_naming_rule.unity_serialized_field_rule.symbols = unity_serialized_field_symbols
dotnet_naming_style.lower_camel_case_style.capitalization = camel_case
dotnet_naming_symbols.unity_serialized_field_symbols.applicable_accessibilities = *
dotnet_naming_symbols.unity_serialized_field_symbols.applicable_kinds =
dotnet_naming_symbols.unity_serialized_field_symbols.resharper_applicable_kinds = unity_serialised_field
dotnet_naming_symbols.unity_serialized_field_symbols.resharper_required_modifiers = instance
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
dotnet_style_qualification_for_event = false:suggestion
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion

# ReSharper properties
resharper_align_linq_query = true
resharper_align_multiline_calls_chain = true
resharper_apply_auto_detected_rules = false
resharper_braces_for_for = required
resharper_braces_for_foreach = required
resharper_braces_for_ifelse = required
resharper_braces_for_while = required
resharper_cpp_insert_final_newline = true
resharper_csharp_indent_size = 3
resharper_csharp_max_enum_members_on_line = 0
resharper_csharp_tab_width = 3
resharper_csharp_wrap_arguments_style = chop_if_long
resharper_csharp_wrap_parameters_style = chop_if_long
resharper_formatter_off_tag = @formatter:off
resharper_formatter_on_tag = @formatter:on
resharper_formatter_tags_enabled = true
resharper_keep_existing_declaration_parens_arrangement = false
resharper_keep_existing_expr_member_arrangement = false
resharper_keep_existing_initializer_arrangement = false
resharper_keep_existing_switch_expression_arrangement = false
resharper_max_array_initializer_elements_on_line = 0
resharper_max_initializer_elements_on_line = 0
resharper_place_accessorholder_attribute_on_same_line = false
resharper_place_accessor_attribute_on_same_line = false
resharper_place_field_attribute_on_same_line = false
resharper_place_simple_anonymousmethod_on_single_line = false
resharper_place_simple_embedded_statement_on_same_line = false
resharper_place_simple_initializer_on_single_line = false
resharper_place_simple_property_pattern_on_single_line = false
resharper_use_indent_from_vs = false
resharper_wrap_after_property_in_chained_method_calls = true
resharper_wrap_array_initializer_style = chop_if_long
resharper_wrap_chained_method_calls = chop_always
resharper_wrap_linq_expressions = chop_always
resharper_wrap_list_pattern = chop_if_long
resharper_wrap_object_and_collection_initializer_style = chop_always
resharper_wrap_property_pattern = chop_always

# ReSharper inspection severities
resharper_arrange_redundant_parentheses_highlighting = hint
resharper_arrange_this_qualifier_highlighting = hint
resharper_arrange_type_member_modifiers_highlighting = hint
resharper_arrange_type_modifiers_highlighting = hint
resharper_built_in_type_reference_style_for_member_access_highlighting = hint
resharper_built_in_type_reference_style_highlighting = hint
resharper_enforce_foreach_statement_braces_highlighting = warning
resharper_enforce_for_statement_braces_highlighting = warning
resharper_enforce_if_statement_braces_highlighting = warning
resharper_enforce_lock_statement_braces_highlighting = warning
resharper_enforce_using_statement_braces_highlighting = warning
resharper_enforce_while_statement_braces_highlighting = warning
resharper_mvc_action_not_resolved_highlighting = warning
resharper_mvc_area_not_resolved_highlighting = warning
resharper_mvc_controller_not_resolved_highlighting = warning
resharper_mvc_masterpage_not_resolved_highlighting = warning
resharper_mvc_partial_view_not_resolved_highlighting = warning
resharper_mvc_template_not_resolved_highlighting = warning
resharper_mvc_view_component_not_resolved_highlighting = warning
resharper_mvc_view_component_view_not_resolved_highlighting = warning
resharper_mvc_view_not_resolved_highlighting = warning
resharper_razor_assembly_not_resolved_highlighting = warning
resharper_redundant_base_qualifier_highlighting = warning
resharper_web_config_module_not_resolved_highlighting = warning
resharper_web_config_type_not_resolved_highlighting = warning
resharper_web_config_wrong_module_highlighting = warning

[{*.har,*.jsb2,*.jsb3,*.json,*.jsonc,*.postman_collection,*.postman_collection.json,*.postman_environment,*.postman_environment.json,.babelrc,.eslintrc,.prettierrc,.stylelintrc,.ws-context,bowerrc,jest.config}]
indent_style = space
indent_size = 2

[{*.yaml,*.yml}]
indent_style = space
indent_size = 2

[*.cs]
indent_style = space
indent_size = 3
tab_width = 3

[*.{appxmanifest,asax,ascx,aspx,axaml,build,c,c++,c++m,cc,ccm,cginc,compute,cp,cpp,cppm,cshtml,cu,cuh,cxx,cxxm,dtd,fs,fsi,fsscript,fsx,fx,fxh,h,hh,hlsl,hlsli,hlslinc,hpp,hxx,inc,inl,ino,ipp,ixx,master,ml,mli,mpp,mq4,mq5,mqh,mxx,nuspec,paml,razor,resw,resx,shader,skin,tpp,usf,ush,uxml,vb,xaml,xamlx,xoml,xsd}]
indent_style = space
indent_size = 4
tab_width = 4

#########################
# Custom added settings #
#########################
[*.cs]
dotnet_diagnostic.cs8600.severity = error #Converting null literal or possible null value to non-nullable type.
dotnet_diagnostic.cs8601.severity = error #Possible null reference assignment.
dotnet_diagnostic.cs8602.severity = error #possible dereference of a null reference
dotnet_diagnostic.cs8603.severity = error #possible null reference return
dotnet_diagnostic.cs8604.severity = error #possible null reference argument for parameter
dotnet_diagnostic.cs8605.severity = error #Unboxing a possibly null value
dotnet_diagnostic.cs8618.severity = error # Non-nullable field is uninitialized. Consider declaring as nullable.
dotnet_diagnostic.cs8625.severity = error # Cannot convert null literal to non-nullable reference type.
dotnet_diagnostic.cs8762.severity = error # Nullability of reference types in type doesn't match overridden member.
dotnet_diagnostic.cs1717.severity = error #variable is assigned to itself
dotnet_diagnostic.cs1718.severity = error #comparison made to same variable
dotnet_diagnostic.cs0659.severity = error #overriding object.Equals but not overriding object.GetHashCode
dotnet_diagnostic.cs0251.severity = error #Indexing an array with a negative index (array indices always start at zero)
dotnet_diagnostic.s3363.severity = none #Never set DateTime as PrimaryKey. Ignored as we never do it but have warnings for cache entities.
dotnet_diagnostic.ca2016.severity = error #Forwarding cancellation tokens
csharp_style_namespace_declarations = file_scoped:error
resharper_entity_framework_model_validation_unlimited_string_length_highlighting = none


#########################
# VS added settings #
#########################

[*.cs]
csharp_style_namespace_declarations = file_scoped:error
csharp_indent_labels = one_less_than_current
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_prefer_braces = true:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent

[{*.yaml,*.yml}]
dotnet_style_namespace_match_folder = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 3
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent

[*.csv]
indent_style = tab
tab_width = 4
1 change: 1 addition & 0 deletions Pandatech.Crypto.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.gitignore = .gitignore
Readme.md = Readme.md
global.json = global.json
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
Expand Down
69 changes: 40 additions & 29 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,46 @@ Install-Package Pandatech.Crypto

## How to Use

### Configuring Dependency Injection
### Configuring in Program.cs

Add the following code to your Program.cs file to configure AES256 and Argon2Id services with minimal setup:
Use the following code to configure AES256 and Argon2Id in your `Program.cs`:

```csharp
using Pandatech.Crypto;
using Pandatech.Crypto.Helpers;
using Pandatech.Crypto.Extensions;

// For Aes256
builder.services.AddPandatechCryptoAes256(options =>
{
options.Key = "YourAes256KeyHere"; // Make sure to use a secure key
});
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// For Argon2Id default configuration
builder.services.AddPandatechCryptoArgon2Id();
// Register AES key
app.AddAes256Key("YourBase64EncodedAes256KeyHere");

// For Argon2Id overriding default configurations
builder.services.AddPandatechCryptoArgon2Id(options =>
// Optional - Change default Argon2Id configurations. If below method is not called, default configurations will be used.
app.ConfigureArgon2Id(options =>
{
options.SaltSize = 16;
options.DegreeOfParallelism = 8;
options.Iterations = 5;
options.MemorySize = 128 * 1024;
});
});

app.Run();

```

### AES256 Class

**Encryption/Decryption methods with hashing**

```csharp
byte[] cipherText = aes256.Encrypt("your-plaintext");
string plainText = aes256.Decrypt(cipherText);
using Pandatech.Crypto.Helpers;

// Encrypt using AES256
var encryptedBytes = Aes256.Encrypt("your-plaintext");

// Decrypt AES256-encrypted data
var decryptedText = Aes256.Decrypt(encryptedBytes);

```

**Encryption/Decryption methods without hashing**
Expand All @@ -74,24 +81,26 @@ string plainText = aes256.DecryptWithoutHash(cipherText);

```csharp
string customKey = "your-custom-base64-encoded-key";
byte[] cipherText = aes256.Encrypt("your-plaintext", customKey);
string plainText = aes256.Decrypt(cipherText, customKey);

// Encrypt with a custom key
var encrypted = Aes256.Encrypt("your-plaintext", customKey);

// Decrypt with the same key
var decrypted = Aes256.Decrypt(encrypted, 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.

```csharp
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");
// Encrypt stream
Aes256.Encrypt(inputStream, outputStream, "your-base64-key");

// Decrypt stream
using var decryptedStream = new MemoryStream(outputStream.ToArray());
Aes256.Decrypt(decryptedStream, outputStream, "your-base64-key");
string decryptedText = Encoding.UTF8.GetString(outputStream.ToArray());
```

Expand All @@ -116,11 +125,13 @@ string decryptedText = Encoding.UTF8.GetString(outputStream.ToArray());
**Examples on usage**

```csharp
// Example usage for hashing
var hashedPassword = _argon2Id.HashPassword("yourPassword");
using Pandatech.Crypto.Helpers;

// Example usage for verifying a hash
var isPasswordValid = _argon2Id.VerifyHash("yourPassword", hashedPassword);
// Hash a password using Argon2Id
var hashedPassword = Argon2Id.HashPassword("yourPassword");

// Verify a hashed password
bool isValid = Argon2Id.VerifyHash("yourPassword", hashedPassword);
```

### Random Class
Expand Down
Loading

0 comments on commit 787ef9e

Please sign in to comment.