Skip to content

Commit e192564

Browse files
authored
Merge pull request #30 from I-RzR-I/feature/EncryptionDataPassCodeGenerator
Encryption data, pass code generator
2 parents 78cf008 + 37b7ebd commit e192564

30 files changed

+1331
-176
lines changed

docs/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,10 @@
126126
-> Adjust dynamic property/ies select avoid `System.Linq.Dynamic.Core`;<br />
127127

128128
### **v2.0.1.8588**
129-
-> Remove unused package `Microsoft.CodeAnalysis.Common`;<br />
129+
-> Remove unused package `Microsoft.CodeAnalysis.Common`;<br />
130+
131+
### **v2.1.0.0**
132+
-> Add new string extensions: `ToStringArray`, `ArrayToString`;<br />
133+
-> Add new array extensions: `AppendItem`, `AppendIfNotExists`, `RemoveItem`, `RemoveAtIdx`;<br />
134+
-> Add new enumerable extensions: `GetDuplicates`, `ForEach`, `ForEachAndReturn`;<br />
135+
-> Add passcode/password generation util;<br />

src/DomainCommonExtensions/ArraysExtensions/ArrayExtensions.cs

Lines changed: 162 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,43 @@
1414
// </summary>
1515
// ***********************************************************************
1616

17+
using System;
18+
using System.Linq;
19+
using DomainCommonExtensions.CommonExtensions;
20+
using DomainCommonExtensions.DataTypeExtensions;
21+
1722
namespace DomainCommonExtensions.ArraysExtensions
1823
{
24+
/// -------------------------------------------------------------------------------------------------
1925
/// <summary>
20-
/// Array extensions
26+
/// Array extensions.
2127
/// </summary>
22-
/// <remarks></remarks>
28+
/// =================================================================================================
2329
public static class ArrayExtensions
2430
{
31+
/// -------------------------------------------------------------------------------------------------
2532
/// <summary>
26-
/// Returns the first index of the given value
33+
/// Returns the first index of the given value.
2734
/// </summary>
28-
/// <param name="array">Input array</param>
29-
/// <param name="value">Value to search</param>
30-
/// <returns></returns>
31-
/// <typeparam name="T">Type of input</typeparam>
32-
/// <remarks>In case when no found any value result is -1</remarks>
33-
public static int IndexOf<T>(this T[] array, T value)
35+
/// <remarks>
36+
/// In case when no found any value result is -1.
37+
/// </remarks>
38+
/// <typeparam name="T">Type of input.</typeparam>
39+
/// <param name="source">Input source.</param>
40+
/// <param name="value">Value to search.</param>
41+
/// <returns>
42+
/// An int.
43+
/// </returns>
44+
/// =================================================================================================
45+
public static int IndexOf<T>(this T[] source, T value)
3446
{
3547
const int index = -1;
3648
try
3749
{
38-
if (array.IsNullOrEmptyEnumerable()) return index;
50+
if (source.IsNullOrEmptyEnumerable()) return index;
3951

40-
for (var i = 0; i < array.Length; i++)
41-
if (Equals(array[i], value))
52+
for (var i = 0; i < source.Length; i++)
53+
if (Equals(source[i], value))
4254
return i;
4355
}
4456
catch
@@ -48,5 +60,143 @@ public static int IndexOf<T>(this T[] array, T value)
4860

4961
return index;
5062
}
63+
64+
/// -------------------------------------------------------------------------------------------------
65+
/// <summary>
66+
/// Append an item to source array.
67+
/// </summary>
68+
/// <typeparam name="T">Generic type parameter. Source type.</typeparam>
69+
/// <param name="source">Input source.</param>
70+
/// <param name="item">The item.</param>
71+
/// <returns>
72+
/// Returns a new T[].
73+
/// </returns>
74+
/// =================================================================================================
75+
public static T[] AppendItem<T>(this T[] source, T item)
76+
{
77+
if (source.IsNullOrEmptyEnumerable() && item.IsNull()) return new T[] { };
78+
if (item.IsNull()) return source;
79+
if (source.IsNullOrEmptyEnumerable()) return new[] { item };
80+
81+
var result = new T[source.Length + 1];
82+
source.CopyTo(result, 0);
83+
result[source.Length] = item;
84+
85+
return result;
86+
}
87+
88+
/// -------------------------------------------------------------------------------------------------
89+
/// <summary>
90+
/// Append an item/s to source array.
91+
/// </summary>
92+
/// <typeparam name="T">Generic type parameter. Source type.</typeparam>
93+
/// <param name="source">Input source.</param>
94+
/// <param name="items">A variable-length parameters list containing items.</param>
95+
/// <returns>
96+
/// Returns a new T[].
97+
/// </returns>
98+
/// =================================================================================================
99+
public static T[] AppendItem<T>(this T[] source, params T[] items)
100+
{
101+
if (source.IsNullOrEmptyEnumerable() && items.IsNullOrEmptyEnumerable()) return new T[] { };
102+
if (items.IsNullOrEmptyEnumerable()) return source;
103+
if (source.IsNullOrEmptyEnumerable()) return items;
104+
105+
var result = new T[source.Length + items.Length];
106+
source.CopyTo(result, 0);
107+
items.CopyTo(result, source.Length);
108+
109+
return result;
110+
}
111+
112+
/// -------------------------------------------------------------------------------------------------
113+
/// <summary>
114+
/// Append an item to source array.
115+
/// </summary>
116+
/// <typeparam name="T">Generic type parameter.</typeparam>
117+
/// <param name="source">Input source.</param>
118+
/// <param name="index">Zero-based index of the.</param>
119+
/// <param name="item">The item.</param>
120+
/// <returns>
121+
/// Returns a new T[].
122+
/// </returns>
123+
/// =================================================================================================
124+
public static T[] AppendItem<T>(this T[] source, int index, T item)
125+
{
126+
if (source.IsNullOrEmptyEnumerable()) return new T[] { };
127+
if (item.IsNull()) return source;
128+
if (index.IsLessZero()) return source;
129+
130+
source[index] = item;
131+
132+
return source;
133+
}
134+
135+
/// -------------------------------------------------------------------------------------------------
136+
/// <summary>
137+
/// Append an items to source array.
138+
/// </summary>
139+
/// <typeparam name="T">Generic type parameter. Source type.</typeparam>
140+
/// <param name="source">Input source.</param>
141+
/// <param name="itemsToAppend">The items to append.</param>
142+
/// <returns>
143+
/// Returns a new T[].
144+
/// </returns>
145+
/// =================================================================================================
146+
public static T[] AppendIfNotExists<T>(this T[] source, T[] itemsToAppend)
147+
{
148+
if (source.IsNullOrEmptyEnumerable() && itemsToAppend.IsNullOrEmptyEnumerable()) return new T[] { };
149+
if (source.IsNullOrEmptyEnumerable()) return itemsToAppend;
150+
if (itemsToAppend.IsNullOrEmptyEnumerable()) return source;
151+
152+
return source.AppendItem((from t in itemsToAppend let existIdx = source.IndexOf(t) where existIdx == -1 select t).ToArray());
153+
}
154+
155+
/// -------------------------------------------------------------------------------------------------
156+
/// <summary>
157+
/// Removes the item from source array.
158+
/// </summary>
159+
/// <typeparam name="T">Generic type parameter. Source type.</typeparam>
160+
/// <param name="source">Input source.</param>
161+
/// <param name="item">The item.</param>
162+
/// <returns>
163+
/// Returns a new T[].
164+
/// </returns>
165+
/// =================================================================================================
166+
public static T[] RemoveItem<T>(this T[] source, T item)
167+
{
168+
if (source.IsNullOrEmptyEnumerable()) return new T[] { };
169+
if (item.IsNull()) return source;
170+
171+
var idx = source.IndexOf(item);
172+
173+
return source.RemoveAtIdx(idx);
174+
}
175+
176+
/// -------------------------------------------------------------------------------------------------
177+
/// <summary>
178+
/// A T[] extension method that removes at index.
179+
/// </summary>
180+
/// <typeparam name="T">Generic type parameter. Source type.</typeparam>
181+
/// <param name="source">Input source.</param>
182+
/// <param name="index">Zero-based index of the.</param>
183+
/// <returns>
184+
/// A T[].
185+
/// </returns>
186+
/// =================================================================================================
187+
public static T[] RemoveAtIdx<T>(this T[] source, int index)
188+
{
189+
if (source.IsNullOrEmptyEnumerable()) return new T[] { };
190+
if (index.IsLessZero()) return source;
191+
192+
T[] dest = new T[source.Length - 1];
193+
if (index > 0)
194+
Array.Copy(source, 0, dest, 0, index);
195+
196+
if (index < source.Length - 1)
197+
Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
198+
199+
return dest;
200+
}
51201
}
52202
}

src/DomainCommonExtensions/ArraysExtensions/EnumerableExtensions.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,5 +467,57 @@ public static IEnumerable<string> GetDuplicates(this IEnumerable<string> list)
467467

468468
return duplicates.ToArray();
469469
}
470+
471+
/// <summary>
472+
/// Get list duplicates
473+
/// </summary>
474+
/// <param name="list">Source list</param>
475+
/// <typeparam name="T">Type of list</typeparam>
476+
/// <returns></returns>
477+
/// <remarks></remarks>
478+
public static IEnumerable<T> GetDuplicates<T>(this IEnumerable<T> list)
479+
{
480+
var duplicates = list
481+
.GroupBy(x => x)
482+
.Where(g => g.Count() > 1)
483+
.Select(y => y.Key)
484+
.ToArray();
485+
486+
return duplicates.ToArray();
487+
}
488+
489+
/// <summary>
490+
/// Execute action for every item
491+
/// </summary>
492+
/// <param name="list">Source list</param>
493+
/// <param name="action">Action to be executed</param>
494+
/// <typeparam name="T">Type of list</typeparam>
495+
/// <returns></returns>
496+
/// <remarks></remarks>
497+
public static void ForEach<T>(this IEnumerable<T> list, Action<T> action)
498+
{
499+
foreach (T item in list)
500+
{
501+
action(item);
502+
}
503+
}
504+
505+
/// <summary>
506+
/// Execute action for every item and return new data
507+
/// </summary>
508+
/// <param name="list">Source list</param>
509+
/// <param name="action">Action to be executed</param>
510+
/// <typeparam name="T">Type of list</typeparam>
511+
/// <returns></returns>
512+
/// <remarks></remarks>
513+
public static IEnumerable<T> ForEachAndReturn<T>(this IEnumerable<T> list, Action<T> action)
514+
{
515+
foreach (T item in list)
516+
{
517+
action(item);
518+
}
519+
520+
return list;
521+
}
470522
}
471523
}

src/DomainCommonExtensions/CommonExtensions/Encryption/AESEncryptionExtensions.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ public static partial class CryptoExtensions
3636
/// <param name="plainText">Plaint text to be encrypted.</param>
3737
/// <param name="key">Encryption key.</param>
3838
/// <param name="iv">Initialization vector.</param>
39+
/// <param name="isLongStringKey">Indicate if is provided by user long key or not. Default value = false.</param>
40+
/// <param name="keySize">Key size. Default value = 256.</param>
41+
/// <param name="blockSize">Block size. Default value = 128.</param>
3942
/// <returns></returns>
40-
public static string AesEncryptString(this string plainText, string key, byte[] iv)
43+
public static string AesEncryptString(this string plainText, string key, byte[] iv, bool isLongStringKey = false, int keySize = 256, int blockSize = 128)
4144
{
4245
key.ThrowIfArgNullOrEmpty(nameof(key));
4346
plainText.ThrowIfArgNull(nameof(plainText));
4447
iv.ThrowIfArgNull(nameof(iv));
4548

46-
//var iv = new byte[16];
4749
byte[] array;
4850
using (var aes = Aes.Create())
4951
{
50-
aes.Key = Encoding.UTF8.GetBytes(key);
52+
aes.KeySize = keySize;
53+
aes.BlockSize = blockSize;
54+
aes.Padding = PaddingMode.PKCS7;
55+
aes.Mode = CipherMode.CBC;
56+
aes.Key = isLongStringKey.IsTrue() ? GetHashByteKey(key) : Encoding.UTF8.GetBytes(key);
5157
aes.IV = iv;
5258
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
5359
using (var memoryStream = new MemoryStream())
@@ -73,18 +79,24 @@ public static string AesEncryptString(this string plainText, string key, byte[]
7379
/// <param name="cipherText">Encrypted text to be decrypted</param>
7480
/// <param name="key">Decryption key.</param>
7581
/// <param name="iv">Initialization vector.</param>
82+
/// <param name="isLongStringKey">Indicate if is provided by user long key or not. Default value = false.</param>
83+
/// <param name="keySize">Key size. Default value = 256.</param>
84+
/// <param name="blockSize">Block size. Default value = 128.</param>
7685
/// <returns></returns>
77-
public static string AesDecryptString(this string cipherText, string key, byte[] iv)
86+
public static string AesDecryptString(this string cipherText, string key, byte[] iv, bool isLongStringKey = false, int keySize = 256, int blockSize = 128)
7887
{
7988
key.ThrowIfArgNullOrEmpty(nameof(key));
8089
cipherText.ThrowIfArgNullOrEmpty(nameof(cipherText));
8190
iv.ThrowIfArgNull(nameof(iv));
8291

83-
//var iv = new byte[16];
8492
var buffer = Convert.FromBase64String(cipherText);
8593
using (var aes = Aes.Create())
8694
{
87-
aes.Key = Encoding.UTF8.GetBytes(key);
95+
aes.KeySize = keySize;
96+
aes.BlockSize = blockSize;
97+
aes.Padding = PaddingMode.PKCS7;
98+
aes.Mode = CipherMode.CBC;
99+
aes.Key = isLongStringKey.IsTrue() ? GetHashByteKey(key) : Encoding.UTF8.GetBytes(key);
88100
aes.IV = iv;
89101
var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
90102
using (var memoryStream = new MemoryStream(buffer))
@@ -99,5 +111,18 @@ public static string AesDecryptString(this string cipherText, string key, byte[]
99111
}
100112
}
101113
}
114+
115+
/// <summary>
116+
/// Get AES key byte from user key hash.
117+
/// </summary>
118+
/// <param name="userKey">User custom long key.</param>
119+
/// <returns></returns>
120+
private static byte[] GetHashByteKey(string userKey)
121+
{
122+
var keyBytes = Encoding.UTF8.GetBytes(userKey);
123+
using var md5 = MD5.Create();
124+
125+
return md5.ComputeHash(keyBytes);
126+
}
102127
}
103128
}

0 commit comments

Comments
 (0)