diff --git a/README.md b/README.md index 44644c46..41e4614f 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ private void Func(string s) - Text - List - Random +- Process +- Task ### OTHER - Password diff --git a/YANLib/YANLib.csproj b/YANLib/YANLib.csproj index 2978a995..95989bf9 100644 --- a/YANLib/YANLib.csproj +++ b/YANLib/YANLib.csproj @@ -18,16 +18,20 @@ False README.md git - Extension: -- Numeric -- Text -- List -- Random -Other: -- Password + Fix: +- HasValue (YANText) +- HasCharater (YANText) +Add: +- HasValues (YANText) +- HasCharaters (YANText) +- WaitAnyWithCondition (YANTask) +- ProcessKiller (YANProcess) +- IsValidPasswordStandard (YANPass) +- IsValidPassword (YANPass) MIT False Tynab.YANLib + 1.0.1 diff --git a/YANLib/YANPass.cs b/YANLib/YANPass.cs index f621843b..fdc5db33 100644 --- a/YANLib/YANPass.cs +++ b/YANLib/YANPass.cs @@ -3,11 +3,16 @@ using static System.Security.Cryptography.CryptographicOperations; using static System.Security.Cryptography.RandomNumberGenerator; using static System.Security.Cryptography.Rfc2898DeriveBytes; +using static System.Threading.Tasks.Task; namespace YANLib; public class YANPass { + #region Fields + private readonly List PASSWORD_SPECIAL_CHARATERS_STANDARD = new() { '@', '#', '$', '%' }; + #endregion + #region Properties public int SaltSize { get; set; } = 16; // 128 bits public int KeySize { get; set; } = 32; // 256 bits @@ -47,5 +52,315 @@ public bool Verify(string password, string strHash) return false; } } + + /// + /// Check if password is valid (standard). + /// + /// Input password. + /// Password is valid or not. + public bool IsValidPasswordStandard(string password) + { + // has character + if (!password.HasCharater()) + { + return false; + } + // has 8 character + if (password.Length < 8) + { + return false; + } + var tasks = new Task[4]; + // has lower case + tasks[0] = Run(() => + { + foreach (var c in password) + { + var str = c.ToString(); + if (str == str.ToLower()) + { + return true; + } + } + return false; + }); + // has upper case + tasks[1] = Run(() => + { + foreach (var c in password) + { + var str = c.ToString(); + if (str == str.ToUpper()) + { + return true; + } + } + return false; + }); + // has number + tasks[2] = Run(() => + { + foreach (var c in password) + { + if (int.TryParse(c.ToString(), out var _)) + { + return true; + } + } + return false; + }); + // has special character + tasks[3] = Run(() => + { + foreach (var c in password) + { + if (PASSWORD_SPECIAL_CHARATERS_STANDARD.Contains(c)) + { + return true; + } + } + return false; + }); + WaitAll(tasks); + return tasks[0].Result && tasks[1].Result && tasks[2].Result && tasks[3].Result; + } + + /// + /// Check if password is valid. + /// + /// Input password. + /// Password length limit. + /// Password is valid or not. + public bool IsValidPassword(string password, int len) + { + // has character + if (!password.HasCharater()) + { + return false; + } + // has 8 character + if (password.Length < len) + { + return false; + } + var tasks = new Task[4]; + // has lower case + tasks[0] = Run(() => + { + foreach (var c in password) + { + var str = c.ToString(); + if (str == str.ToLower()) + { + return true; + } + } + return false; + }); + // has upper case + tasks[1] = Run(() => + { + foreach (var c in password) + { + var str = c.ToString(); + if (str == str.ToUpper()) + { + return true; + } + } + return false; + }); + // has number + tasks[2] = Run(() => + { + foreach (var c in password) + { + if (int.TryParse(c.ToString(), out var _)) + { + return true; + } + } + return false; + }); + // has special character + tasks[3] = Run(() => + { + foreach (var c in password) + { + if (PASSWORD_SPECIAL_CHARATERS_STANDARD.Contains(c)) + { + return true; + } + } + return false; + }); + WaitAll(tasks); + return tasks[0].Result && tasks[1].Result && tasks[2].Result && tasks[3].Result; + } + + /// + /// Check if password is valid. + /// + /// Input password. + /// Special characters check. + /// Password is valid or not. + public bool IsValidPassword(string password, params char[] splChars) + { + // has character + if (!password.HasCharater()) + { + return false; + } + // has 8 character + if (password.Length < 8) + { + return false; + } + var tasks = new Task[4]; + // create new list password special charaters + var newPwdSplChar = new List(PASSWORD_SPECIAL_CHARATERS_STANDARD); + foreach (var c in splChars) + { + if (!newPwdSplChar.Contains(c)) + { + newPwdSplChar.Add(c); + } + } + // has lower case + tasks[0] = Run(() => + { + foreach (var c in password) + { + var str = c.ToString(); + if (str == str.ToLower()) + { + return true; + } + } + return false; + }); + // has upper case + tasks[1] = Run(() => + { + foreach (var c in password) + { + var str = c.ToString(); + if (str == str.ToUpper()) + { + return true; + } + } + return false; + }); + // has number + tasks[2] = Run(() => + { + foreach (var c in password) + { + if (int.TryParse(c.ToString(), out var _)) + { + return true; + } + } + return false; + }); + // has special character + tasks[3] = Run(() => + { + foreach (var c in password) + { + if (newPwdSplChar.Contains(c)) + { + return true; + } + } + return false; + }); + WaitAll(tasks); + return tasks[0].Result && tasks[1].Result && tasks[2].Result && tasks[3].Result; + } + + /// + /// Check if password is valid. + /// + /// Input password. + /// Password length limit. + /// Special characters check. + /// Password is valid or not. + public bool IsValidPassword(string password, int len, params char[] splChars) + { + // has character + if (!password.HasCharater()) + { + return false; + } + // has 8 character + if (password.Length < len) + { + return false; + } + var tasks = new Task[4]; + // create new list password special charaters + var newPwdSplChar = new List(PASSWORD_SPECIAL_CHARATERS_STANDARD); + foreach (var c in splChars) + { + if (!newPwdSplChar.Contains(c)) + { + newPwdSplChar.Add(c); + } + } + // has lower case + tasks[0] = Run(() => + { + foreach (var c in password) + { + var str = c.ToString(); + if (str == str.ToLower()) + { + return true; + } + } + return false; + }); + // has upper case + tasks[1] = Run(() => + { + foreach (var c in password) + { + var str = c.ToString(); + if (str == str.ToUpper()) + { + return true; + } + } + return false; + }); + // has number + tasks[2] = Run(() => + { + foreach (var c in password) + { + if (int.TryParse(c.ToString(), out var _)) + { + return true; + } + } + return false; + }); + // has special character + tasks[3] = Run(() => + { + foreach (var c in password) + { + if (newPwdSplChar.Contains(c)) + { + return true; + } + } + return false; + }); + WaitAll(tasks); + return tasks[0].Result && tasks[1].Result && tasks[2].Result && tasks[3].Result; + } #endregion } diff --git a/YANLib/YANProcess.cs b/YANLib/YANProcess.cs new file mode 100644 index 00000000..961d8dea --- /dev/null +++ b/YANLib/YANProcess.cs @@ -0,0 +1,18 @@ +using static System.Diagnostics.Process; + +namespace YANLib; + +public static partial class YANProcess +{ + /// + /// Kill all process with name. + /// + /// Process name. + public static void ProcessKiller(this string name) + { + while (GetProcessesByName(name).Length > 0) + { + GetProcessesByName(name).FirstOrDefault()?.Kill(); + } + } +} diff --git a/YANLib/YANTask.cs b/YANLib/YANTask.cs new file mode 100644 index 00000000..4f1b1a1c --- /dev/null +++ b/YANLib/YANTask.cs @@ -0,0 +1,35 @@ +using static System.Threading.Tasks.Task; +using static System.Threading.Tasks.TaskStatus; + +namespace YANLib; + +public static partial class YANTask +{ + /// + /// Waits for any of the provided objects to complete execution with condition. + /// + /// Object type. + /// Input tasks. + /// Good result. + /// First task completed result. + public static async Task WaitAnyWithCondition(this IEnumerable> tasks, T goodRslt) + { + var taskList = new List>(tasks); + var fstCmpl = default(Task); + while (taskList.Count > 0) + { + var curCmpl = await WhenAny(taskList); + var rslt = curCmpl.Result; + if (curCmpl.Status == RanToCompletion && rslt != null && rslt.Equals(goodRslt)) + { + fstCmpl = curCmpl; + break; + } + else + { + taskList.Remove(curCmpl); + } + } + return (fstCmpl != default(Task)) ? fstCmpl.Result : default; + } +} diff --git a/YANLib/YANText.String.cs b/YANLib/YANText.String.cs index d89a24f6..bbabb8da 100644 --- a/YANLib/YANText.String.cs +++ b/YANLib/YANText.String.cs @@ -7,14 +7,28 @@ public static partial class YANText /// /// Input string. /// String has value or not. - public static bool HasValue(this string str) => string.IsNullOrEmpty(str); + public static bool HasValue(this string str) => !string.IsNullOrEmpty(str); + + /// + /// Check if strings has value. + /// + /// Input strings. + /// Strings has value or not. + public static bool HasValues(params string[] strs) => strs.All(s => !string.IsNullOrEmpty(s)); /// /// Check if string has character. /// /// Input string. /// String has character or not. - public static bool HasCharater(this string str) => string.IsNullOrWhiteSpace(str); + public static bool HasCharater(this string str) => !string.IsNullOrWhiteSpace(str); + + /// + /// Check if strings has character. + /// + /// Input strings. + /// Strings has value or not. + public static bool HasCharaters(params string[] strs)=>strs.All(s => !string.IsNullOrWhiteSpace(s)); /// /// Get value of string, if string null return empty. diff --git a/YANLib/nuget.config b/YANLib/nuget.config new file mode 100644 index 00000000..7cd529ec --- /dev/null +++ b/YANLib/nuget.config @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pic/0.jpg b/pic/0.jpg new file mode 100644 index 00000000..21b6b5e8 Binary files /dev/null and b/pic/0.jpg differ diff --git a/pic/0.png b/pic/0.png deleted file mode 100644 index f65512ba..00000000 Binary files a/pic/0.png and /dev/null differ