Ejemplo n.º 1
0
        private static List <string> GetCharacterGroups(PasswordGenerationOptions options)
        {
            List <string> groups = new List <string>();

            if (options.UpperCase)
            {
                groups.Add(UpperCaseChars);
            }

            if (options.LowerCase)
            {
                groups.Add(LowerCaseChars);
            }

            if (options.Digits)
            {
                groups.Add(DigitChars);
            }

            if (options.Minus)
            {
                groups.Add(MinusChars);
            }

            if (options.Underline)
            {
                groups.Add(UnderlineChars);
            }

            if (options.Space)
            {
                groups.Add(SpaceChars);
            }

            if (options.Specials)
            {
                groups.Add(SpecialChars);
            }

            if (options.SpecialsExtended)
            {
                groups.Add(ExtendedChars);
            }

            if (options.Brackets)
            {
                groups.Add(BracketChars);
            }


            if (options.ExcludeLookAlike)
            {
                for (int i = 0; i < groups.Count; i++)
                {
                    groups[i] = String.Join("", groups[i].Except("Il1|8B6GO0"));
                }
            }

            return(groups);
        }
        //--------------------------------------------------
        // constructor
        public PasswordGenerator(PasswordGenerationOptions pgo)
        {
            this.pgo = pgo;

            FillCharsets();

            Init();
        }
Ejemplo n.º 3
0
        EnforcePasswordGeneratorPoliciesOnOptionsAsync(PasswordGenerationOptions options)
        {
            var enforcedPolicyOptions = await GetPasswordGeneratorPolicyOptions();

            if (enforcedPolicyOptions != null)
            {
                if (options.Length < enforcedPolicyOptions.MinLength)
                {
                    options.Length = enforcedPolicyOptions.MinLength;
                }

                if (enforcedPolicyOptions.UseUppercase)
                {
                    options.Uppercase = true;
                }

                if (enforcedPolicyOptions.UseLowercase)
                {
                    options.Lowercase = true;
                }

                if (enforcedPolicyOptions.UseNumbers)
                {
                    options.Number = true;
                }

                if (options.MinNumber < enforcedPolicyOptions.NumberCount)
                {
                    options.MinNumber = enforcedPolicyOptions.NumberCount;
                }

                if (enforcedPolicyOptions.UseSpecial)
                {
                    options.Special = true;
                }

                if (options.MinSpecial < enforcedPolicyOptions.SpecialCount)
                {
                    options.MinSpecial = enforcedPolicyOptions.SpecialCount;
                }

                // Must normalize these fields because the receiving call expects all options to pass the current rules
                if (options.MinSpecial + options.MinNumber > options.Length)
                {
                    options.MinSpecial = options.Length - options.MinNumber;
                }
            }
            else
            {
                // UI layer expects an instantiated object to prevent more explicit null checks
                enforcedPolicyOptions = new PasswordGeneratorPolicyOptions();
            }

            return(options, enforcedPolicyOptions);
        }
Ejemplo n.º 4
0
 private void AssertPasswords(PasswordGenerationOptions options, Action <string, PasswordGenerationOptions> assertAction)
 {
     using (PasswordGenerator pg = new PasswordGenerator())
     {
         string pass;
         for (int i = 0; i < NumberOfRunsPerTest; i++)
         {
             pass = pg.Generate(options);
             assertAction(pass, options);
         }
     }
 }
        public string GeneratePassword(PasswordGenerationOptions options)
        {
            var pwProfile = new PwProfile
            {
                GeneratorType = PasswordGeneratorType.CharSet,
                Length        = (uint)options.PasswordLength,
                CharSet       = new PwCharSet()
            };

            if (options.UpperCasePatternSelected)
            {
                pwProfile.CharSet.Add(PwCharSet.UpperCase);
            }
            if (options.LowerCasePatternSelected)
            {
                pwProfile.CharSet.Add(PwCharSet.LowerCase);
            }
            if (options.DigitsPatternSelected)
            {
                pwProfile.CharSet.Add(PwCharSet.Digits);
            }
            if (options.SpecialPatternSelected)
            {
                pwProfile.CharSet.Add(PwCharSet.Special);
            }
            if (options.MinusPatternSelected)
            {
                pwProfile.CharSet.Add('-');
            }
            if (options.UnderscorePatternSelected)
            {
                pwProfile.CharSet.Add('_');
            }
            if (options.SpacePatternSelected)
            {
                pwProfile.CharSet.Add(' ');
            }
            if (options.BracketsPatternSelected)
            {
                pwProfile.CharSet.Add(PwCharSet.Brackets);
            }

            pwProfile.CharSet.Add(options.CustomChars);

            ProtectedString password;

            PwGenerator.Generate(out password, pwProfile, null, new CustomPwGeneratorPool());

            return(password.ReadString());
        }
Ejemplo n.º 6
0
        private void AssertSinglePasswordGroups(string password, PasswordGenerationOptions options)
        {
            Assert.True(password.Length == (int)options.Length, "Length is not ok");

            int numberOfUpper   = password.Count(options.ValidUpperCase.Contains);
            int numberOfLower   = password.Count(options.ValidLowerCase.Contains);
            int numberOfSymbols = password.Count(options.ValidSymbols.Contains);
            int numberOfNumbers = password.Count("0123456789".Contains);

            Assert.True(numberOfLower >= options.MinLowerCase, "Number of lower case is less than minimum");
            Assert.True(numberOfUpper >= options.MinUpperCase, "Number of upper case is less than minimum");
            Assert.True(numberOfSymbols >= options.MinSymbols, "Number of symbols is less than minimum");
            Assert.True(numberOfNumbers >= options.MinNumbers, "Number of numbers is less than minimum");
        }
Ejemplo n.º 7
0
 public void Merge(PasswordGenerationOptions defaults)
 {
     Length        = Length ?? defaults.Length;
     Ambiguous     = Ambiguous ?? defaults.Ambiguous;
     Number        = Number ?? defaults.Number;
     MinNumber     = MinNumber ?? defaults.MinNumber;
     Uppercase     = Uppercase ?? defaults.Uppercase;
     MinUppercase  = MinUppercase ?? defaults.MinUppercase;
     Lowercase     = Lowercase ?? defaults.Lowercase;
     MinLowercase  = MinLowercase ?? defaults.MinLowercase;
     Special       = Special ?? defaults.Special;
     MinSpecial    = MinSpecial ?? defaults.MinSpecial;
     Type          = Type ?? defaults.Type;
     NumWords      = NumWords ?? defaults.NumWords;
     WordSeparator = WordSeparator ?? defaults.WordSeparator;
 }
Ejemplo n.º 8
0
        public async Task <PasswordGenerationOptions> GetOptionsAsync()
        {
            if (_optionsCache == null)
            {
                var options = await _storageService.GetAsync <PasswordGenerationOptions>(Keys_Options);

                if (options == null)
                {
                    _optionsCache = _defaultOptions;
                }
                else
                {
                    options.Merge(_defaultOptions);
                    _optionsCache = options;
                }
            }
            return(_optionsCache);
        }
Ejemplo n.º 9
0
            public string Handle(GeneratePasswordCommand message)
            {
                var options = new PasswordGenerationOptions
                {
                    PasswordLength            = message.PasswordLength,
                    BracketsPatternSelected   = message.BracketsPatternSelected,
                    CustomChars               = message.CustomChars,
                    DigitsPatternSelected     = message.DigitsPatternSelected,
                    LowerCasePatternSelected  = message.LowerCasePatternSelected,
                    MinusPatternSelected      = message.MinusPatternSelected,
                    SpacePatternSelected      = message.SpacePatternSelected,
                    SpecialPatternSelected    = message.SpecialPatternSelected,
                    UnderscorePatternSelected = message.UnderscorePatternSelected,
                    UpperCasePatternSelected  = message.UpperCasePatternSelected
                };

                return(_security.GeneratePassword(options));
            }
Ejemplo n.º 10
0
 public void Merge(PasswordGenerationOptions defaults)
 {
     Length             = Length ?? defaults.Length;
     AllowAmbiguousChar = AllowAmbiguousChar ?? defaults.AllowAmbiguousChar;
     Number             = Number ?? defaults.Number;
     MinNumber          = MinNumber ?? defaults.MinNumber;
     Uppercase          = Uppercase ?? defaults.Uppercase;
     MinUppercase       = MinUppercase ?? defaults.MinUppercase;
     Lowercase          = Lowercase ?? defaults.Lowercase;
     MinLowercase       = MinLowercase ?? defaults.MinLowercase;
     Special            = Special ?? defaults.Special;
     MinSpecial         = MinSpecial ?? defaults.MinSpecial;
     Type          = Type ?? defaults.Type;
     NumWords      = NumWords ?? defaults.NumWords;
     WordSeparator = WordSeparator ?? defaults.WordSeparator;
     Capitalize    = Capitalize ?? defaults.Capitalize;
     IncludeNumber = IncludeNumber ?? defaults.IncludeNumber;
 }
Ejemplo n.º 11
0
        private bool IsSinglePasswordInRange(string password, PasswordGenerationOptions options)
        {
            Func <IEnumerable <int>, bool> areIndexesInRange = (numbers) =>
            {
                if (numbers.Count() == 0)
                {
                    return(false);
                }

                if (numbers.Count() != numbers.Distinct().Count())
                {
                    throw new ArgumentException("numbers are not unique");
                }
                return(numbers.Max() - numbers.Min() == numbers.Count() - 1);
            };

            Func <string, List <int> > getGroupIndexes = (string group) =>
            {
                List <int> indexes = new List <int>();
                for (int i = 0; i < password.Length; i++)
                {
                    if (group.Contains(password[i]))
                    {
                        indexes.Add(i);
                    }
                }
                return(indexes);
            };

            Func <string, bool> isGroupInRange = (group) =>
            {
                var indexes = getGroupIndexes(group);
                return(areIndexesInRange(indexes));
            };

            bool lowerAreInRange     = isGroupInRange(options.ValidLowerCase);
            bool upperAreInRange     = isGroupInRange(options.ValidUpperCase);
            bool symbolsAreIsInRange = isGroupInRange(options.ValidSymbols);
            bool numbersAreInRange   = isGroupInRange("0123456789");

            return(lowerAreInRange && upperAreInRange && symbolsAreIsInRange && numbersAreInRange);
        }
        public async Task <string> GeneratePassphraseAsync(PasswordGenerationOptions options)
        {
            options.Merge(_defaultOptions);
            if (options.NumWords.GetValueOrDefault() <= 2)
            {
                options.NumWords = _defaultOptions.NumWords;
            }
            if (options.WordSeparator == null || options.WordSeparator.Length == 0 || options.WordSeparator.Length > 1)
            {
                options.WordSeparator = " ";
            }
            if (options.Capitalize == null)
            {
                options.Capitalize = false;
            }
            if (options.IncludeNumber == null)
            {
                options.IncludeNumber = false;
            }
            var listLength = EEFLongWordList.Instance.List.Count - 1;
            var wordList   = new List <string>();

            for (int i = 0; i < options.NumWords.GetValueOrDefault(); i++)
            {
                var wordIndex = await _cryptoService.RandomNumberAsync(0, listLength);

                if (options.Capitalize.GetValueOrDefault())
                {
                    wordList.Add(Capitalize(EEFLongWordList.Instance.List[wordIndex]));
                }
                else
                {
                    wordList.Add(EEFLongWordList.Instance.List[wordIndex]);
                }
            }
            if (options.IncludeNumber.GetValueOrDefault())
            {
                await AppendRandomNumberToRandomWordAsync(wordList);
            }
            return(string.Join(options.WordSeparator, wordList));
        }
Ejemplo n.º 13
0
        public async Task <string> GeneratePassphraseAsync(PasswordGenerationOptions options)
        {
            options.Merge(_defaultOptions);
            if (options.NumWords.GetValueOrDefault() <= 2)
            {
                options.NumWords = _defaultOptions.NumWords;
            }
            if (options.WordSeparator == null || options.WordSeparator.Length == 0 || options.WordSeparator.Length > 1)
            {
                options.WordSeparator = " ";
            }
            var listLength = WordList.EEFLongWordList.Count - 1;
            var wordList   = new List <string>();

            for (int i = 0; i < options.NumWords.GetValueOrDefault(); i++)
            {
                var wordIndex = await _cryptoService.RandomNumberAsync(0, listLength);

                wordList.Add(WordList.EEFLongWordList[wordIndex]);
            }
            return(string.Join(options.WordSeparator, wordList));
        }
        public async Task <(PasswordGenerationOptions, PasswordGeneratorPolicyOptions)> GetOptionsAsync()
        {
            if (_optionsCache == null)
            {
                var options = await _storageService.GetAsync <PasswordGenerationOptions>(Keys_Options);

                if (options == null)
                {
                    _optionsCache = _defaultOptions;
                }
                else
                {
                    options.Merge(_defaultOptions);
                    _optionsCache = options;
                }
            }

            var(enforcedOptions, enforcedPolicyOptions) = await EnforcePasswordGeneratorPoliciesOnOptionsAsync(
                _optionsCache);

            _optionsCache = enforcedOptions;
            return(_optionsCache, enforcedPolicyOptions);
        }
        public void NormalizeOptions(PasswordGenerationOptions options,
                                     PasswordGeneratorPolicyOptions enforcedPolicyOptions)
        {
            options.MinLowercase = 0;
            options.MinUppercase = 0;

            if (!options.Uppercase.GetValueOrDefault() && !options.Lowercase.GetValueOrDefault() &&
                !options.Number.GetValueOrDefault() && !options.Special.GetValueOrDefault())
            {
                options.Lowercase = true;
            }

            var length = options.Length.GetValueOrDefault();

            if (length < 5)
            {
                options.Length = 5;
            }
            else if (length > 128)
            {
                options.Length = 128;
            }

            if (options.Length < enforcedPolicyOptions.MinLength)
            {
                options.Length = enforcedPolicyOptions.MinLength;
            }

            if (options.MinNumber == null)
            {
                options.MinNumber = 0;
            }
            else if (options.MinNumber > options.Length)
            {
                options.MinNumber = options.Length;
            }
            else if (options.MinNumber > 9)
            {
                options.MinNumber = 9;
            }

            if (options.MinNumber < enforcedPolicyOptions.NumberCount)
            {
                options.MinNumber = enforcedPolicyOptions.NumberCount;
            }

            if (options.MinSpecial == null)
            {
                options.MinSpecial = 0;
            }
            else if (options.MinSpecial > options.Length)
            {
                options.MinSpecial = options.Length;
            }
            else if (options.MinSpecial > 9)
            {
                options.MinSpecial = 9;
            }

            if (options.MinSpecial < enforcedPolicyOptions.SpecialCount)
            {
                options.MinSpecial = enforcedPolicyOptions.SpecialCount;
            }

            if (options.MinSpecial + options.MinNumber > options.Length)
            {
                options.MinSpecial = options.Length - options.MinNumber;
            }

            if (options.NumWords == null || options.Length < 3)
            {
                options.NumWords = 3;
            }
            else if (options.NumWords > 20)
            {
                options.NumWords = 20;
            }

            if (options.NumWords < enforcedPolicyOptions.MinNumberOfWords)
            {
                options.NumWords = enforcedPolicyOptions.MinNumberOfWords;
            }

            if (options.WordSeparator != null && options.WordSeparator.Length > 1)
            {
                options.WordSeparator = options.WordSeparator[0].ToString();
            }

            SanitizePasswordLength(options, false);
        }
        public async Task SaveOptionsAsync(PasswordGenerationOptions options)
        {
            await _storageService.SaveAsync(Keys_Options, options);

            _optionsCache = options;
        }
        public async Task <string> GeneratePasswordAsync(PasswordGenerationOptions options)
        {
            // Overload defaults with given options
            options.Merge(_defaultOptions);
            if (options.Type == "passphrase")
            {
                return(await GeneratePassphraseAsync(options));
            }

            // Sanitize
            SanitizePasswordLength(options, true);

            var positionsBuilder = new StringBuilder();

            if (options.Lowercase.GetValueOrDefault() && options.MinLowercase.GetValueOrDefault() > 0)
            {
                for (int i = 0; i < options.MinLowercase.GetValueOrDefault(); i++)
                {
                    positionsBuilder.Append("l");
                }
            }
            if (options.Uppercase.GetValueOrDefault() && options.MinUppercase.GetValueOrDefault() > 0)
            {
                for (int i = 0; i < options.MinUppercase.GetValueOrDefault(); i++)
                {
                    positionsBuilder.Append("u");
                }
            }
            if (options.Number.GetValueOrDefault() && options.MinNumber.GetValueOrDefault() > 0)
            {
                for (int i = 0; i < options.MinNumber.GetValueOrDefault(); i++)
                {
                    positionsBuilder.Append("n");
                }
            }
            if (options.Special.GetValueOrDefault() && options.MinSpecial.GetValueOrDefault() > 0)
            {
                for (int i = 0; i < options.MinSpecial.GetValueOrDefault(); i++)
                {
                    positionsBuilder.Append("s");
                }
            }
            while (positionsBuilder.Length < options.Length.GetValueOrDefault())
            {
                positionsBuilder.Append("a");
            }

            // Shuffle
            var positions = positionsBuilder.ToString().ToCharArray()
                            .OrderBy(a => _cryptoFunctionService.RandomNumber()).ToArray();

            // Build out other character sets
            var allCharSet       = string.Empty;
            var lowercaseCharSet = LowercaseCharSet;

            if (options.Ambiguous.GetValueOrDefault())
            {
                lowercaseCharSet = string.Concat(lowercaseCharSet, "l");
            }
            if (options.Lowercase.GetValueOrDefault())
            {
                allCharSet = string.Concat(allCharSet, lowercaseCharSet);
            }

            var uppercaseCharSet = UppercaseCharSet;

            if (options.Ambiguous.GetValueOrDefault())
            {
                uppercaseCharSet = string.Concat(uppercaseCharSet, "IO");
            }
            if (options.Uppercase.GetValueOrDefault())
            {
                allCharSet = string.Concat(allCharSet, uppercaseCharSet);
            }

            var numberCharSet = NumberCharSet;

            if (options.Ambiguous.GetValueOrDefault())
            {
                numberCharSet = string.Concat(numberCharSet, "01");
            }
            if (options.Number.GetValueOrDefault())
            {
                allCharSet = string.Concat(allCharSet, numberCharSet);
            }

            var specialCharSet = SpecialCharSet;

            if (options.Special.GetValueOrDefault())
            {
                allCharSet = string.Concat(allCharSet, specialCharSet);
            }

            var password = new StringBuilder();

            for (var i = 0; i < options.Length.GetValueOrDefault(); i++)
            {
                var positionChars = string.Empty;
                switch (positions[i])
                {
                case 'l':
                    positionChars = lowercaseCharSet;
                    break;

                case 'u':
                    positionChars = uppercaseCharSet;
                    break;

                case 'n':
                    positionChars = numberCharSet;
                    break;

                case 's':
                    positionChars = specialCharSet;
                    break;

                case 'a':
                    positionChars = allCharSet;
                    break;
                }

                var randomCharIndex = await _cryptoService.RandomNumberAsync(0, positionChars.Length - 1);

                password.Append(positionChars[randomCharIndex]);
            }

            return(password.ToString());
        }
        EnforcePasswordGeneratorPoliciesOnOptionsAsync(PasswordGenerationOptions options)
        {
            var enforcedPolicyOptions = await GetPasswordGeneratorPolicyOptions();

            if (enforcedPolicyOptions != null)
            {
                if (options.Length < enforcedPolicyOptions.MinLength)
                {
                    options.Length = enforcedPolicyOptions.MinLength;
                }

                if (enforcedPolicyOptions.UseUppercase)
                {
                    options.Uppercase = true;
                }

                if (enforcedPolicyOptions.UseLowercase)
                {
                    options.Lowercase = true;
                }

                if (enforcedPolicyOptions.UseNumbers)
                {
                    options.Number = true;
                }

                if (options.MinNumber < enforcedPolicyOptions.NumberCount)
                {
                    options.MinNumber = enforcedPolicyOptions.NumberCount;
                }

                if (enforcedPolicyOptions.UseSpecial)
                {
                    options.Special = true;
                }

                if (options.MinSpecial < enforcedPolicyOptions.SpecialCount)
                {
                    options.MinSpecial = enforcedPolicyOptions.SpecialCount;
                }

                // Must normalize these fields because the receiving call expects all options to pass the current rules
                if (options.MinSpecial + options.MinNumber > options.Length)
                {
                    options.MinSpecial = options.Length - options.MinNumber;
                }

                if (options.NumWords < enforcedPolicyOptions.MinNumberOfWords)
                {
                    options.NumWords = enforcedPolicyOptions.MinNumberOfWords;
                }

                if (enforcedPolicyOptions.Capitalize)
                {
                    options.Capitalize = true;
                }

                if (enforcedPolicyOptions.IncludeNumber)
                {
                    options.IncludeNumber = true;
                }

                // Force default type if password/passphrase selected via policy
                if (enforcedPolicyOptions.DefaultType == "password" || enforcedPolicyOptions.DefaultType == "passphrase")
                {
                    options.Type = enforcedPolicyOptions.DefaultType;
                }
            }
            else
            {
                // UI layer expects an instantiated object to prevent more explicit null checks
                enforcedPolicyOptions = new PasswordGeneratorPolicyOptions();
            }

            return(options, enforcedPolicyOptions);
        }
Ejemplo n.º 19
0
 protected bool Equals(PasswordGenerationOptions other)
 {
     return(Length == other.Length && UpperCase == other.UpperCase && LowerCase == other.LowerCase && Digits == other.Digits && Minus == other.Minus && Underline == other.Underline && Space == other.Space && Specials == other.Specials && SpecialsExtended == other.SpecialsExtended && Brackets == other.Brackets && ExcludeLookAlike == other.ExcludeLookAlike && AtLeastOneFromEachGroup == other.AtLeastOneFromEachGroup);
 }
Ejemplo n.º 20
0
 public void ClearCache()
 {
     _optionsCache = null;
     _history      = null;
 }
        public async Task <(PasswordGenerationOptions, PasswordGeneratorPolicyOptions)> GetOptionsAsync()
        {
            if (_optionsCache == null)
            {
                var options = await _storageService.GetAsync <PasswordGenerationOptions>(Keys_Options);

                if (options == null)
                {
                    _optionsCache = _defaultOptions;
                }
                else
                {
                    options.Merge(_defaultOptions);
                    _optionsCache = options;
                }
            }

            var enforcedPolicyOptions = await GetPasswordGeneratorPolicyOptions();

            if (enforcedPolicyOptions != null)
            {
                if (_optionsCache.Length < enforcedPolicyOptions.MinLength)
                {
                    _optionsCache.Length = enforcedPolicyOptions.MinLength;
                }

                if (enforcedPolicyOptions.UseUppercase)
                {
                    _optionsCache.Uppercase = true;
                }

                if (enforcedPolicyOptions.UseLowercase)
                {
                    _optionsCache.Lowercase = true;
                }

                if (enforcedPolicyOptions.UseNumbers)
                {
                    _optionsCache.Number = true;
                }

                if (_optionsCache.MinNumber < enforcedPolicyOptions.NumberCount)
                {
                    _optionsCache.MinNumber = enforcedPolicyOptions.NumberCount;
                }

                if (enforcedPolicyOptions.UseSpecial)
                {
                    _optionsCache.Special = true;
                }

                if (_optionsCache.MinSpecial < enforcedPolicyOptions.SpecialCount)
                {
                    _optionsCache.MinSpecial = enforcedPolicyOptions.SpecialCount;
                }

                // Must normalize these fields because the receiving call expects all options to pass the current rules
                if (_optionsCache.MinSpecial + _optionsCache.MinNumber > _optionsCache.Length)
                {
                    _optionsCache.MinSpecial = _optionsCache.Length - _optionsCache.MinNumber;
                }
            }
            else
            {
                // UI layer expects an instantiated object to prevent more explicit null checks
                enforcedPolicyOptions = new PasswordGeneratorPolicyOptions();
            }

            return(_optionsCache, enforcedPolicyOptions);
        }
Ejemplo n.º 22
0
 private void AssertPasswordGroups(PasswordGenerationOptions options) =>
 AssertPasswords(options, AssertSinglePasswordGroups);
Ejemplo n.º 23
0
        public string GetCharacterSet(PasswordGenerationOptions options, List <string> groups)
        {
            var characterSet = String.Join("", groups);

            return(characterSet);
        }
        private void SanitizePasswordLength(PasswordGenerationOptions options, bool forGeneration)
        {
            var minUppercaseCalc = 0;
            var minLowercaseCalc = 0;
            var minNumberCalc    = options.MinNumber;
            var minSpecialCalc   = options.MinNumber;

            if (options.Uppercase.GetValueOrDefault() && options.MinUppercase.GetValueOrDefault() <= 0)
            {
                minUppercaseCalc = 1;
            }
            else if (!options.Uppercase.GetValueOrDefault())
            {
                minUppercaseCalc = 0;
            }

            if (options.Lowercase.GetValueOrDefault() && options.MinLowercase.GetValueOrDefault() <= 0)
            {
                minLowercaseCalc = 1;
            }
            else if (!options.Lowercase.GetValueOrDefault())
            {
                minLowercaseCalc = 0;
            }

            if (options.Number.GetValueOrDefault() && options.MinNumber.GetValueOrDefault() <= 0)
            {
                minNumberCalc = 1;
            }
            else if (!options.Number.GetValueOrDefault())
            {
                minNumberCalc = 0;
            }

            if (options.Special.GetValueOrDefault() && options.MinSpecial.GetValueOrDefault() <= 0)
            {
                minSpecialCalc = 1;
            }
            else if (!options.Special.GetValueOrDefault())
            {
                minSpecialCalc = 0;
            }

            // This should never happen but is a final safety net
            if (options.Length.GetValueOrDefault() < 1)
            {
                options.Length = 10;
            }

            var minLength = minUppercaseCalc + minLowercaseCalc + minNumberCalc + minSpecialCalc;

            // Normalize and Generation both require this modification
            if (options.Length < minLength)
            {
                options.Length = minLength;
            }

            // Apply other changes if the options object passed in is for generation
            if (forGeneration)
            {
                options.MinUppercase = minUppercaseCalc;
                options.MinLowercase = minLowercaseCalc;
                options.MinNumber    = minNumberCalc;
                options.MinSpecial   = minSpecialCalc;
            }
        }
Ejemplo n.º 25
0
        public async Task SaveOptionsAsync(PasswordGenerationOptions options)
        {
            await _stateService.SetPasswordGenerationOptionsAsync(options);

            _optionsCache = options;
        }
Ejemplo n.º 26
0
        public void NormalizeOptions(PasswordGenerationOptions options)
        {
            options.MinLowercase = 0;
            options.MinUppercase = 0;

            if (!options.Uppercase.GetValueOrDefault() && !options.Lowercase.GetValueOrDefault() &&
                !options.Number.GetValueOrDefault() && !options.Special.GetValueOrDefault())
            {
                options.Lowercase = true;
            }

            var length = options.Length.GetValueOrDefault();

            if (length < 5)
            {
                options.Length = 5;
            }
            else if (length > 128)
            {
                options.Length = 128;
            }

            if (options.MinNumber == null)
            {
                options.MinNumber = 0;
            }
            else if (options.MinNumber > options.Length)
            {
                options.MinNumber = options.Length;
            }
            else if (options.MinNumber > 9)
            {
                options.MinNumber = 9;
            }

            if (options.MinSpecial == null)
            {
                options.MinSpecial = 0;
            }
            else if (options.MinSpecial > options.Length)
            {
                options.MinSpecial = options.Length;
            }
            else if (options.MinSpecial > 9)
            {
                options.MinSpecial = 9;
            }

            if (options.MinSpecial + options.MinNumber > options.Length)
            {
                options.MinSpecial = options.Length - options.MinNumber;
            }

            if (options.NumWords == null || options.Length < 3)
            {
                options.NumWords = 3;
            }
            else if (options.NumWords > 20)
            {
                options.NumWords = 20;
            }

            if (options.WordSeparator != null && options.WordSeparator.Length > 1)
            {
                options.WordSeparator = options.WordSeparator[0].ToString();
            }
        }