Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Globalization;
- namespace ConsoleApplication5
- {
- static class Program
- {
- static void Main(string[] args)
- {
- string password = Guid.NewGuid().ToString();
- string[] names = GenerateNames(password, 10000);
- TestMethods(password, names, new Func<string, int, string[], bool>[] { StringManipulationOnly, RegexStringJoinAllParts, RegexZeroWidthPlusOneAndDotSplat, RegexZeroWidth, RegexZeroWidthPlusOne, RegexZeroWidthPlusOneDotSplatReluctant });
- Console.ReadKey();
- }
- private static void TestMethods(string password, string[] names, Func<string, int, string[], bool>[] funcs)
- {
- foreach (var func in funcs)
- {
- GC.Collect();
- GC.WaitForPendingFinalizers();
- Thread.Sleep(2500);
- Console.WriteLine("Method: " + func.Method.Name);
- int passed = 0;
- int failed = 0;
- DateTime start = DateTime.UtcNow;
- for (int i = 1; i < names.Length; i++)
- {
- if (func.Invoke(password, 3, new[] { names[i], names[i - 1] }))
- {
- passed++;
- }
- else
- {
- failed++;
- }
- }
- DateTime end = DateTime.UtcNow;
- Console.WriteLine("Time taken: {0}ms. Passed: {1}. Failed {2}.", (end - start).TotalMilliseconds, passed, failed);
- }
- }
- private static string[] GenerateNames(string password, int count)
- {
- var passwordParts = password.AllPartsOfLength(3).ToArray();
- var otherParts = Guid.NewGuid().ToString().AllPartsOfLength(3).Except(passwordParts).ToArray();
- var random = new Random(DateTime.UtcNow.Millisecond);
- int partCount = passwordParts.Count();
- int otherPartCount = otherParts.Count();
- List<string> checkNames = new List<string>();
- for (int i = 0; i < count; i++)
- {
- if (i % 3 == 0)
- {
- // take random part from username
- int otherpart1 = random.Next(0, otherPartCount);
- int otherpart2 = random.Next(0, otherPartCount);
- int passwordpart = random.Next(0, partCount);
- switch (i % 3)
- {
- case 0: checkNames.Add(passwordParts[passwordpart] + otherParts[otherpart1] + otherParts[otherpart2]); break;
- case 1: checkNames.Add(otherParts[otherpart1] + passwordParts[passwordpart] + otherParts[otherpart2]); break;
- case 2: checkNames.Add(otherParts[otherpart1] + otherParts[otherpart2] + passwordParts[passwordpart]); break;
- }
- }
- else
- {
- // take random part from Guid.NewGuid()
- checkNames.Add(string.Join("|", Enumerable.Range(0, 3).Select<int, string>(tmp => otherParts[random.Next(0, otherPartCount)])));
- }
- }
- return checkNames.ToArray();
- }
- public static bool StringManipulationOnly(string password, int partLength, params string[] partSources)
- {
- bool passwordOk = !partSources.SelectMany(source => source.AllPartsOfLength(3))
- .Any(part => password.Contains(part));
- return passwordOk;
- }
- public static bool RegexStringJoinAllParts(string password, int partLength, params string[] partSources)
- {
- var parts = partSources.SelectMany(source => source.AllPartsOfLength(3)).Select(part => Regex.Escape(part));
- string regex = "(" + string.Join("|", parts) + ")";
- bool isPasswordOk = !Regex.Match(password, regex).Success;
- return isPasswordOk;
- }
- public static bool RegexZeroWidthPlusOneAndDotSplat(string password, int partLength, params string[] partSources)
- {
- return partSources.All(source => password.IsNotSequentialChars(source, partLength));
- }
- public static bool RegexZeroWidth(string password, int partLength, params string[] partSources)
- {
- return partSources.All(source => password.IsNotSequentialChars2(source, partLength));
- }
- public static bool RegexZeroWidthPlusOne(string password, int partLength, params string[] partSources)
- {
- return partSources.All(source => password.IsNotSequentialChars2(source, partLength));
- }
- public static bool RegexZeroWidthPlusOneDotSplatReluctant(string password, int partLength, params string[] partSources)
- {
- return partSources.All(source => password.IsNotSequentialChars2(source, partLength));
- }
- public static bool IsNotSequentialChars(this string Src, string Dest, int check_len)
- {
- Match m = Regex.Match(Src, "(?=(.{" + check_len + "})).");
- bool bOK = true;
- while (bOK && m.Success)
- {
- bOK = !Regex.Match(Dest, ".*" + Regex.Escape(m.Groups[1].Value)).Success;
- if (!bOK)
- return false;
- m = m.NextMatch();
- }
- return bOK;
- }
- public static bool IsNotSequentialCharsReluctant(this string Src, string Dest, int check_len)
- {
- Match m = Regex.Match(Src, "(?=(.{" + check_len + "})).");
- bool bOK = true;
- while (bOK && m.Success)
- {
- bOK = !Regex.Match(Dest, ".*?" + Regex.Escape(m.Groups[1].Value)).Success;
- if (!bOK)
- return false;
- m = m.NextMatch();
- }
- return bOK;
- }
- public static bool IsNotSequentialChars2(this string Src, string Dest, int check_len)
- {
- Match m = Regex.Match(Src, "(?=(.{" + check_len + "}))");
- bool bOK = true;
- while (bOK && m.Success)
- {
- bOK = !Regex.Match(Dest, Regex.Escape(m.Groups[1].Value)).Success;
- if (!bOK)
- return false;
- m = m.NextMatch();
- }
- return bOK;
- }
- public static bool IsNotSequentialChars3(this string Src, string Dest, int check_len)
- {
- Match m = Regex.Match(Src, "(?=(.{" + check_len + "})).");
- bool bOK = true;
- while (bOK && m.Success)
- {
- bOK = !Regex.Match(Dest, Regex.Escape(m.Groups[1].Value)).Success;
- if (!bOK)
- return false;
- m = m.NextMatch();
- }
- return bOK;
- }
- public static IEnumerable<string> AllPartsOfLength(this string value, int length)
- {
- for (int startPos = 0; startPos <= value.Length - length; startPos++)
- {
- yield return value.Substring(startPos, length);
- }
- yield break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement