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)); }
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 <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()); }