private static List <Func <char, bool> > BuildRulesForChar(CharOptions charOptions) { var rulesForChar = new List <Func <char, bool> >(); if ((charOptions & CharOptions.Letter) == CharOptions.Letter) { rulesForChar.Add(char.IsLetter); } if ((charOptions & CharOptions.Upper) == CharOptions.Upper) { rulesForChar.Add(char.IsUpper); } if ((charOptions & CharOptions.Lower) == CharOptions.Lower) { rulesForChar.Add(char.IsLower); } if ((charOptions & CharOptions.Digit) == CharOptions.Digit) { rulesForChar.Add(char.IsDigit); } if ((charOptions & CharOptions.Number) == CharOptions.Number) { rulesForChar.Add(char.IsNumber); } if ((charOptions & CharOptions.Separator) == CharOptions.Separator) { rulesForChar.Add(char.IsSeparator); } if ((charOptions & CharOptions.WhiteSpace) == CharOptions.WhiteSpace) { rulesForChar.Add(char.IsWhiteSpace); } if ((charOptions & CharOptions.Symbol) == CharOptions.Symbol) { rulesForChar.Add(char.IsSymbol); } if ((charOptions & CharOptions.Control) == CharOptions.Control) { rulesForChar.Add(char.IsControl); } if ((charOptions & CharOptions.Punctuation) == CharOptions.Punctuation) { rulesForChar.Add(char.IsPunctuation); } return(rulesForChar); }
/// <summary> /// Gets valid content with chars from CharOptions and specialChars. /// </summary> /// <param name="s"></param> /// <param name="charOptions"></param> /// <param name="specialChars"></param> /// <returns></returns> public static string GetValidContent(this string s, CharOptions charOptions, string specialChars = null) { if (s.IsNullOrEmpty()) { return(string.Empty); } var rulesForChar = BuildRulesForChar(charOptions); if (!specialChars.IsNullOrEmpty()) { rulesForChar.Add(specialChars.Contains); } var result = s .Where(c => rulesForChar.Any(func => func(c))) .Aggregate(string.Empty, (current, c) => current + c); return(specialChars.IsNullOrEmpty() ? result : result.ReplaceTwoAndMoreCharsBySomeOne(specialChars)); }
private bool ConformsLevel(PasswordStrength strength, int length, int distinctCount, CharOptions charOptions) { StrengthLevelData levelStt = _settings.StrengthLevels.FirstOrDefault(lvl => lvl.Strength == strength); if (levelStt != null && levelStt.Conforms(length, distinctCount, charOptions)) { return(true); } return(false); }
public bool Conforms(int length, int distinctCount, CharOptions options) { return(length >= MinLength && distinctCount >= MinDistinctLength && (Options & options) == Options); // all required option flags are present in 'options' }
public bool Conforms(int length, int distinctCount, CharOptions options) { return length >= MinLength && distinctCount >= MinDistinctLength && (Options & options) == Options; // all required option flags are present in 'options' }
public static bool IsSet(this CharOptions flags, CharOptions flag) { return (flags & flag) != 0; }
public ValidCharactersValidator(CharOptions charOptions, string specialChars) { CharOptions = charOptions; SpecialChars = specialChars; }
public static IRuleBuilderOptions <T, TProperty> ValidCharacters <T, TProperty>( this IRuleBuilder <T, TProperty> ruleBuilder, CharOptions charOptions, string specialChars) { return(ruleBuilder.SetValidator(new ValidCharactersValidator(charOptions, specialChars))); }
public static bool IsSet(this CharOptions flags, CharOptions flag) { return((flags & flag) != 0); }
private bool ConformsLevel(PasswordStrength strength, int length, int distinctCount, CharOptions charOptions) { StrengthLevelData levelStt = _settings.StrengthLevels.FirstOrDefault(lvl => lvl.Strength == strength); if (levelStt != null && levelStt.Conforms(length, distinctCount, charOptions)) return true; return false; }