/// <summary> /// Returns a random string index matching the charType parameter. /// </summary> private int RandomCharIndex(Characters.CharType targetType, string s) { string charList = string.Empty; switch (targetType) { case Characters.CharType.AZUpper: charList = azCharList.ToUpper(); break; case Characters.CharType.AZLower: charList = azCharList.ToLower(); break; case Characters.CharType.Number: charList = numCharList; break; case Characters.CharType.Special: charList = specialCharList; break; } Random r = new Random(); while (true) { char c = s[r.Next(0, s.Length)]; if (charList.Contains(c.ToString())) { return(s.IndexOf(c)); } } }
internal static char GetRandomChar(Characters.CharType returnType) { string charList = string.Empty; switch (returnType) { case Characters.CharType.None: charList += Characters.AZCharList.ToUpper(); charList += Characters.AZCharList.ToLower(); charList += Characters.NumCharList; charList += Characters.SpecialCharList; break; case Characters.CharType.AZUpper: charList += Characters.AZCharList.ToUpper(); break; case Characters.CharType.AZLower: charList += Characters.AZCharList.ToLower(); break; case Characters.CharType.Number: charList += Characters.NumCharList; break; case Characters.CharType.Special: charList += Characters.SpecialCharList; break; } int i = new Random().Next(0, charList.Length); return(charList[i]); }
internal static CharType IdentifyCharType(char c) { string character = c.ToString(); Characters.CharType type = Characters.CharType.None; if (Characters.AZCharList.ToUpper().Contains(character)) { type = Characters.CharType.AZUpper; } else if (Characters.AZCharList.ToLower().Contains(character)) { type = Characters.CharType.AZLower; } else if (Characters.NumCharList.Contains(character)) { type = Characters.CharType.Number; } else if (Characters.SpecialCharList.Contains(character)) { type = Characters.CharType.Special; } return(type); }
/// <summary> /// Replaces a random character from a string with one matching the desiredType parameter. /// </summary> private string RandomReplaceChar(Characters.CharType desiredType, string password) { int azUpperCount = 0, azLowerCount = 0, numberCount = 0, specialCount = 0; foreach (char c in password) { if (azCharList.ToUpper().Contains(c.ToString())) { azUpperCount++; } if (azCharList.ToLower().Contains(c.ToString())) { azLowerCount++; } if (numCharList.Contains(c.ToString())) { numberCount++; } if (specialCharList.Contains(c.ToString())) { specialCount++; } } string charList = string.Empty; switch (desiredType) { case Characters.CharType.AZUpper: charList = azCharList.ToUpper(); break; case Characters.CharType.AZLower: charList = azCharList.ToLower(); break; case Characters.CharType.Number: charList = numCharList; break; case Characters.CharType.Special: charList = specialCharList; break; } Random r = new Random(); bool charNotReplaced = true; while (charNotReplaced) { Characters.CharType t = (Characters.CharType)r.Next(1, 5); string newChar = charList[r.Next(0, charList.Length)].ToString(); switch (t) { case Characters.CharType.AZUpper: if (azUpperCount > 1) { int oldCharIndex = RandomCharIndex(Characters.CharType.AZUpper, password); password = password.Remove(oldCharIndex, 1).Insert(oldCharIndex, newChar); charNotReplaced = false; } break; case Characters.CharType.AZLower: if (azLowerCount > 1) { int charIndex = RandomCharIndex(Characters.CharType.AZLower, password); password = password.Remove(charIndex, 1).Insert(charIndex, newChar); charNotReplaced = false; } break; case Characters.CharType.Number: if (numberCount > 1) { int charIndex = RandomCharIndex(Characters.CharType.Number, password); password = password.Remove(charIndex, 1).Insert(charIndex, newChar); charNotReplaced = false; } break; case Characters.CharType.Special: if (specialCount > 1) { int charIndex = RandomCharIndex(Characters.CharType.Special, password); password = password.Remove(charIndex, 1).Insert(charIndex, newChar); charNotReplaced = false; } break; } } return(password); }
public static int Test(string password) { int index = 0; List <int> azLowerIndexList = new List <int>(); List <int> azUpperIndexList = new List <int>(); List <int> numIndexList = new List <int>(); List <int> specialIndexList = new List <int>(); int middleNumbers = 0, middleSpecial = 0; int consecutiveAZUpper = 0, consecutiveAZLower = 0, consecutiveNumbers = 0, consecutiveSpecial = 0; int requirementScore = 0; Characters.CharType previousType = Characters.CharType.None; foreach (char c in password) { if (azCharList.ToUpper().Contains(c)) { azUpperIndexList.Add(index); if (azUpperIndexList.Count == 1) { requirementScore++; } if (previousType == Characters.CharType.AZUpper) { consecutiveAZUpper++; } previousType = Characters.CharType.AZUpper; } if (azCharList.ToLower().Contains(c)) { azLowerIndexList.Add(index); if (azLowerIndexList.Count == 1) { requirementScore++; } if (previousType == Characters.CharType.AZLower) { consecutiveAZLower++; } previousType = Characters.CharType.AZLower; } if (numCharList.Contains(c)) { numIndexList.Add(index); if (numIndexList.Count == 1) { requirementScore++; } if (index != 0 && index != (password.Length - 1)) { middleNumbers++; } if (previousType == Characters.CharType.Number) { consecutiveNumbers++; } previousType = Characters.CharType.Number; } if (specialCharList.Contains(c)) { specialIndexList.Add(index); if (specialIndexList.Count == 1) { requirementScore++; } if (index != 0 && index != (password.Length - 1)) { middleSpecial++; } if (previousType == Characters.CharType.Special) { consecutiveSpecial++; } previousType = Characters.CharType.Special; } index++; } // Additions int lengthScore = (password.Length * 4); int azUpperCountScore = (azUpperIndexList.Count > 0) ? ((password.Length - azUpperIndexList.Count) * 2) : 0; int azLowerCountScore = (azLowerIndexList.Count > 0) ? ((password.Length - azLowerIndexList.Count) * 2) : 0; int numCountScore = (numIndexList.Count * 4); int specialCountScore = (specialIndexList.Count * 6); int middleCountScore = ((middleNumbers + middleSpecial) * 2); if (password.Length >= 8) { requirementScore++; requirementScore = (requirementScore * 2); } else { requirementScore = 0; } int additionPoints = lengthScore + azUpperCountScore + azLowerCountScore + numCountScore + specialCountScore + middleCountScore + requirementScore; // Deductions int deductionPoints = 0; if (password.Length == (azLowerIndexList.Count + azUpperIndexList.Count)) { deductionPoints += password.Length; } if (password.Length == numIndexList.Count) { deductionPoints += password.Length; } // Count the number of characters that are repeated. int repeatingChars = 0; foreach (char c in password) { if (password.Count(x => x == c) > 1) { repeatingChars++; } } // The following code checks and counts character sequences (E.G. abc 123 !@#.) int azSequenceCount = 0; int numSequenceCount = 0; int specialSequenceCount = 0; for (int i = 0; i < password.Length; i++) { if (i > 0) { char c = password[i]; if (azCharList.Contains(c)) { int cIndex = azCharList.IndexOf(c); char prevChar = (i > 0) ? password[i - 1] : '\0'; char nextChar = (i < password.Length - 1) ? password[i + 1] : '\0'; int prevIndex = cIndex - 1; int nextIndex = cIndex + 1; if (prevIndex >= 0 && nextIndex < azCharList.Length) { if (azCharList[cIndex - 1] == prevChar && azCharList[cIndex + 1] == nextChar) { azSequenceCount++; } } } else if (numCharList.Contains(c)) { int cIndex = numCharList.IndexOf(c); char prevChar = (i > 0) ? password[i - 1] : '\0'; char nextChar = (i < password.Length - 1) ? password[i + 1] : '\0'; int prevIndex = cIndex - 1; int nextIndex = cIndex + 1; if (prevIndex >= 0 && nextIndex < numCharList.Length) { if (numCharList[cIndex - 1] == prevChar && numCharList[cIndex + 1] == nextChar) { numSequenceCount++; } } } else if (specialCharList.Contains(c)) { int cIndex = specialCharList.IndexOf(c); char prevChar = (i > 0) ? password[i - 1] : '\0'; char nextChar = (i < password.Length - 1) ? password[i + 1] : '\0'; int prevIndex = cIndex - 1; int nextIndex = cIndex + 1; if (prevIndex >= 0 && nextIndex < specialCharList.Length) { if (specialCharList[cIndex - 1] == prevChar && specialCharList[cIndex + 1] == nextChar) { specialSequenceCount++; } } } } } deductionPoints += repeatingChars; deductionPoints += (consecutiveAZUpper * 2); deductionPoints += (consecutiveAZLower * 2); deductionPoints += (consecutiveNumbers * 2); // deductionPoints += (consecutiveSpecial * 2); deductionPoints += (azSequenceCount * 3); deductionPoints += (numSequenceCount * 3); deductionPoints += (specialSequenceCount * 3); return(additionPoints - deductionPoints); }