/// <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> private static bool PasswordIsValid(IPasswordSettings settings, string password) { const string regexLowercase = @"[a-z]"; const string regexUppercase = @"[A-Z]"; const string regexNumeric = @"[\d]"; var lowerCaseIsValid = !settings.IncludeLowercase || settings.IncludeLowercase && Regex.IsMatch(password, regexLowercase); var upperCaseIsValid = !settings.IncludeUppercase || settings.IncludeUppercase && Regex.IsMatch(password, regexUppercase); var numericIsValid = !settings.IncludeNumeric || settings.IncludeNumeric && Regex.IsMatch(password, regexNumeric); var specialIsValid = !settings.IncludeSpecial; if (settings.IncludeSpecial && !string.IsNullOrWhiteSpace(settings.SpecialCharacters)) { var listA = settings.SpecialCharacters.ToCharArray(); var listB = password.ToCharArray(); specialIsValid = listA.Any(x => listB.Contains(x)); } return(lowerCaseIsValid && upperCaseIsValid && numericIsValid && specialIsValid && LengthIsValid(password.Length, settings.MinimumLength, settings.MaximumLength)); }
/// <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 static string GenerateRandomPassword(IPasswordSettings settings) { const int maximumIdenticalConsecutiveChars = 2; var password = new char[settings.PasswordLength]; var characters = settings.CharacterSet.ToCharArray(); var shuffledChars = Shuffle(characters.Select(x => x)).ToArray(); var shuffledCharacterSet = string.Join(null, shuffledChars); var characterSetLength = shuffledCharacterSet.Length; for (var characterPosition = 0; characterPosition < settings.PasswordLength; characterPosition++) { password[characterPosition] = shuffledCharacterSet[GetRandomNumberInRange(0, characterSetLength - 1)]; var moreThanTwoIdenticalInARow = characterPosition > maximumIdenticalConsecutiveChars && password[characterPosition] == password[characterPosition - 1] && password[characterPosition - 1] == password[characterPosition - 2]; if (moreThanTwoIdenticalInARow) { characterPosition--; } } return(string.Join(null, password)); }
private static string GetCombinedCharSet(IPasswordSettings settings) { string result = String.Empty; if (settings.IncludeNumeric) { result += CharSets.Numerics; } if (settings.IncludeAlphaLower) { result += CharSets.AlphaLower; } if (settings.IncludeAlphaUpper) { result += CharSets.AlphaUpper; } if (settings.IncludeSymbolSetNormal) { result += CharSets.SymbolCommon; } if (settings.IncludeSymbolSetExtended) { result += CharSets.SymbolUnCommon; } return(result); }
private static bool ContainsAllCharSets(IPasswordSettings settings, string password) { if (settings.IncludeNumeric && !password.Any(x => CharSets.Numerics.Contains(x))) { return(false); } if (settings.IncludeAlphaLower && !password.Any(x => CharSets.AlphaLower.Contains(x))) { return(false); } if (settings.IncludeAlphaUpper && !password.Any(x => CharSets.AlphaUpper.Contains(x))) { return(false); } if (settings.IncludeSymbolSetNormal && !password.Any(x => CharSets.SymbolCommon.Contains(x))) { return(false); } if (settings.IncludeSymbolSetExtended && !password.Any(x => CharSets.SymbolUnCommon.Contains(x))) { return(false); } return(true); }
private static string CreatePassword(string combinedCharSet, int length, IPasswordSettings settings) { var random = new CryptoRandom(); for (int i = 0; i < Int32.MaxValue; i++) { var sb = new StringBuilder(); for (int pwdIndex = 0; pwdIndex < length; pwdIndex++) { int rndIndex = random.Next(0, combinedCharSet.Length); sb.Append(combinedCharSet[rndIndex]); } var password = sb.ToString(); if (settings.ForceEach && ContainsAllCharSets(settings, password)) { return(password); } else if (!settings.ForceEach) { return(password); } } return(null); }
private static IEnumerable <CharSets.DigitTypes> GetDigitTypes(IPasswordSettings settings) { var result = new List <CharSets.DigitTypes>(); if (settings.IncludeNumeric) { result.Add(CharSets.DigitTypes.Numerics); } if (settings.IncludeAlphaLower) { result.Add(CharSets.DigitTypes.AlphaLower); } if (settings.IncludeAlphaUpper) { result.Add(CharSets.DigitTypes.AlphaUpper); } if (settings.IncludeSymbolSetNormal) { result.Add(CharSets.DigitTypes.SymbolCommon); } if (settings.IncludeSymbolSetExtended) { result.Add(CharSets.DigitTypes.SymbolUnCommon); } return(result); }
private string GeneratePassword(IPasswordSettings settings) { var password = new char[settings.PasswordLength]; var shuffledCharacters = Shuffle(settings.CharacterSet); for (int position = 0; position < settings.PasswordLength; position++) { password[position] = shuffledCharacters.ToCharArray()[GetRandomNumber(shuffledCharacters.Length - 1)]; } return(new string(password)); }
public static string Generate(IPasswordSettings settings) { if (!settings.IsValid()) { return(null); } var combinedCharSet = GetCombinedCharSet(settings); var password = CreatePassword(combinedCharSet, settings.Length, settings); return(password); }
public static bool IsValid(this IPasswordSettings settings) { if (settings.Length < 1) { return(false); } if (!settings.IncludeNumeric && !settings.IncludeAlphaLower && !settings.IncludeAlphaUpper && !settings.IncludeSymbolSetNormal && !settings.IncludeSymbolSetExtended) { return(false); } if (settings.ForceEach) { int sets = 0; if (settings.IncludeNumeric) { sets++; } if (settings.IncludeAlphaLower) { sets++; } if (settings.IncludeAlphaUpper) { sets++; } if (settings.IncludeSymbolSetNormal) { sets++; } if (settings.IncludeSymbolSetExtended) { sets++; } if (settings.Length < sets) { return(false); } } return(true); }
public static void OverrideFrom(this IPasswordSettings settings, IPasswordSettings source) { if (settings == null || source == null) { return; } settings.Length = source.Length; settings.ForceEach = source.ForceEach; settings.IncludeNumeric = source.IncludeNumeric; settings.IncludeAlphaLower = source.IncludeAlphaLower; settings.IncludeAlphaUpper = source.IncludeAlphaUpper; settings.IncludeSymbolSetNormal = source.IncludeSymbolSetNormal; settings.IncludeSymbolSetExtended = source.IncludeSymbolSetExtended; }
/// <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> private static bool PasswordIsValid(IPasswordSettings settings, string password) { const string regexLowercase = @"[a-z]"; const string regexUppercase = @"[A-Z]"; const string regexNumeric = @"[\d]"; const string regexSpecial = @"([!#$%&*@\\])+"; var lowerCaseIsValid = !settings.IncludeLowercase || settings.IncludeLowercase && Regex.IsMatch(password, regexLowercase); var upperCaseIsValid = !settings.IncludeUppercase || settings.IncludeUppercase && Regex.IsMatch(password, regexUppercase); var numericIsValid = !settings.IncludeNumeric || settings.IncludeNumeric && Regex.IsMatch(password, regexNumeric); var specialIsValid = !settings.IncludeSpecial || settings.IncludeSpecial && Regex.IsMatch(password, regexSpecial); return(lowerCaseIsValid && upperCaseIsValid && numericIsValid && specialIsValid && LengthIsValid(password.Length, settings.MinimumLength, settings.MaximumLength)); }
public AspNetMembershipProviderSettingsWrapper(IRegistrationSettings registration, IPasswordSettings password, ILoginSettings login) { Registration = registration; Password = password; Login = login; }
public IPassword IncludeNumeric() { Settings = Settings.AddNumeric(); return(this); }
public IPassword IncludeUppercase() { Settings = Settings.AddUppercase(); return(this); }
public Password(IPasswordSettings settings) { Settings = settings; _rng = new RNGCryptoServiceProvider(); }
public Password(bool includeLowercase, bool includeUppercase, bool includeNumeric, bool includeSymbols, bool avoidAmbiguous, int length) { Settings = new PasswordSettings(includeLowercase, includeUppercase, includeNumeric, includeSymbols, avoidAmbiguous, length); _rngcsp = RandomNumberGenerator.Create(); }
public Password(IPasswordSettings passwordSettings) { Settings = passwordSettings; _rngcsp = RandomNumberGenerator.Create(); }
public IPassword IncludeSpecial(string specialCharactersToInclude) { Settings = Settings.AddSpecial(specialCharactersToInclude); return(this); }
public IPassword IncludeSpecial() { Settings = Settings.AddSpecial(); return(this); }
public static int CalcEntropy(IPasswordSettings settings) { var digitTypes = GetDigitTypes(settings); return(GetPasswordCardinality(settings.Length, digitTypes)); }
public static int CalcEntropy(IPasswordSettings settings) { return(EntropyCalculator.CalcEntropy(settings)); }
public Password(IPasswordSettings settings) { Settings = settings; _rng = RandomNumberGenerator.Create(); }
public Password(IPasswordSettings settings) { Settings = settings; }