/// <summary> /// Generates a random password based on the rules passed in the settings parameter /// This does not do any validation /// </summary> /// <param name="settings">Password generator settings object</param> /// <returns>a random password</returns> private string GenerateRandomPassword(PasswordGeneratorSettings settings) { const int MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS = 2; char[] password = new char[settings.PasswordLength]; char[] characters = settings.CharacterSet.ToCharArray(); char[] shuffledChars = Shuffle(characters.Select(x => x)).ToArray(); string shuffledCharacterSet = string.Join(null, shuffledChars); int characterSetLength = shuffledCharacterSet.Length; System.Random random = new System.Random(); for (int characterPosition = 0; characterPosition < settings.PasswordLength; characterPosition++) { password[characterPosition] = shuffledCharacterSet[random.Next(characterSetLength - 1)]; bool moreThanTwoIdenticalInARow = characterPosition > MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS && password[characterPosition] == password[characterPosition - 1] && password[characterPosition - 1] == password[characterPosition - 2]; if (moreThanTwoIdenticalInARow) { characterPosition--; } } return(string.Join(null, password)); }
/// <summary> /// When you give it a password and some _settings, it validates the password against the _settings. /// </summary> /// <param name="settings">Password settings</param> /// <param name="password">Password to test</param> /// <returns>True or False to say if the password is valid or not</returns> public bool PasswordIsValid(PasswordGeneratorSettings settings, string password) { const string REGEX_LOWERCASE = @"[a-z]"; const string REGEX_UPPERCASE = @"[A-Z]"; const string REGEX_NUMERIC = @"[\d]"; const string REGEX_SPECIAL = @"([!#$%&*@\\])+"; bool lowerCaseIsValid = !settings.IncludeLowercase || (settings.IncludeLowercase && Regex.IsMatch(password, REGEX_LOWERCASE)); bool upperCaseIsValid = !settings.IncludeUppercase || (settings.IncludeUppercase && Regex.IsMatch(password, REGEX_UPPERCASE)); bool numericIsValid = !settings.IncludeNumeric || (settings.IncludeNumeric && Regex.IsMatch(password, REGEX_NUMERIC)); bool specialIsValid = !settings.IncludeSpecial || (settings.IncludeSpecial && Regex.IsMatch(password, REGEX_SPECIAL)); return(lowerCaseIsValid && upperCaseIsValid && numericIsValid && specialIsValid && LengthIsValid(password.Length, settings.MinimumLength, settings.MaximumLength)); }
public NormalPasswordGenerator IncludeNumeric() { this._settings = _settings.AddNumeric(); return(this); }
public NormalPasswordGenerator IncludeSpecial() { this._settings = _settings.AddSpecial(); return(this); }
public NormalPasswordGenerator IncludeUppercase() { this._settings = _settings.AddUppercase(); return(this); }
public NormalPasswordGenerator(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial, int passwordLength, int maximumAttempts) { _settings = new PasswordGeneratorSettings(includeLowercase, includeUppercase, includeNumeric, includeSpecial, passwordLength, maximumAttempts, false); }
public NormalPasswordGenerator(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSpecial) { _settings = new PasswordGeneratorSettings(includeLowercase, includeUppercase, includeNumeric, includeSpecial, _defaultPasswordLength, _defaultMaxPasswordAttempts, false); }
public NormalPasswordGenerator(int passwordLength) { _settings = new PasswordGeneratorSettings(_defaultIncludeLowercase, _defaultIncludeUppercase, _defaultIncludeNumeric, _defaultIncludeSpecial, passwordLength, _defaultMaxPasswordAttempts, true); }
public NormalPasswordGenerator(PasswordGeneratorSettings settings) { _settings = settings; }