public new PasswordChunk Get(PasswordChunk pReferenceChunk) { PasswordChunk chunk = new PasswordChunk(); chunk.ChunkType = PasswordChunkType.Special; int referenceChunkLength = pReferenceChunk.Text.Length; int length = referenceChunkLength + ((Utils.Coin() || Utils.Coin()) ? 0 : 1); string text = ""; for (int count = 1; count < length; ++count) { text += Numbers[Random.Next(NumbersLength)]; } bool specialFirst = Utils.Coin(); char special = _specials[Random.Next(_specials.Length)]; chunk.Text = specialFirst ? chunk.Text = $"{special}{text}" : text + special; return(chunk); }
public new PasswordChunk Get(PasswordChunk pReferenceChunk) { PasswordChunk chunk = new PasswordChunk(); chunk.ChunkType = PasswordChunkType.Mixed; chunk.Text = $"{Consonants[Random.Next(ConsonantsLength)]}{Vowels[Random.Next(VowelsLength)]}{Consonants[Random.Next(ConsonantsLength)]}"; return(chunk); }
public new PasswordChunk Get(PasswordChunk pReferenceChunk) { PasswordChunk chunk = new PasswordChunk(); chunk.ChunkType = PasswordChunkType.InvertedDiphtongue; chunk.Text = $"{GetDiphtongue()}{Consonants[Random.Next(ConsonantsLength)]}"; return(chunk); }
// ................................................................................................. public string Generate(int pLength, PasswordGeneratorOptionsEx pOptions) { if (pLength < PasswordMinLength) { pLength = PasswordMinLength; } Password password = new Password(); while (password.PasswordLength < pLength) { PasswordChunk chunk = GenerateNextChunk(password); password.AppendChunk(chunk); } bool capitalsRandom = false; if (pOptions.Options.HasFlag(PasswordGeneratorOptions.Capitals)) { if (pOptions.Options.HasFlag(PasswordGeneratorOptions.CapitalsFirstInChunks) || pOptions.Options.HasFlag(PasswordGeneratorOptions.CapitalsLastInChunks)) { CapitalizeChunks(password, pOptions.Options); } else { capitalsRandom = true; } } if (pOptions.Options.HasFlag(PasswordGeneratorOptions.Specials)) { InsertSpecialChunk(password, pOptions.SpecialGroupSymbols, !pOptions.Options.HasFlag(PasswordGeneratorOptions.SpecialsNotBlank), pOptions.SpecialGroupsMin, pOptions.SpecialGroupsMax); } if (capitalsRandom) { CapitalizeRandomPassword(password, pOptions.CapitalsMinimum, pOptions.CapitalsMaximum); } if (pOptions.Options.HasFlag(PasswordGeneratorOptions.Separators)) { InsertSeparators(password, pOptions.Options.HasFlag(PasswordGeneratorOptions.SeparatorRotation), pOptions.FixedSeparatorChar); } LastPassword = password; return(password.Text); }
// ---------------------------------------------------------------------------------------------------------------------------- private void CapitalizeRandomPassword(Password pPassword, int pMinimum, int pMaximum) { int capitalized = 0; do { int probability = 50; for (int index = 0; index < pPassword.ChunkCount; ++index) { PasswordChunk chunk = pPassword.Chunks[index]; if (chunk.ChunkType == PasswordChunkType.Special) { continue; } if (_random.Next(100) < probability) { bool lastLetter; if (chunk.CapitalizedFirst && chunk.CapitalizedLast) { continue; } if (chunk.CapitalizedFirst) { lastLetter = true; } else { lastLetter = !chunk.CapitalizedLast && Utils.Coin(); } if (pPassword.CapitalizeChunk(index, lastLetter)) { capitalized++; probability -= 20; if (probability <= 0) { break; } if (capitalized > pMaximum) { return; } } } } } while (capitalized == 0); }
private void InsertSpecialChunk(Password pPassword, string pSpecialSymbols, bool pAcceptBlanks, int pMinimum, int pMaximum) { if (pSpecialSymbols.Contains(" ") && !pAcceptBlanks) { pSpecialSymbols = pSpecialSymbols.Replace(" ", ""); } pMaximum = (pMaximum <= (pPassword.ChunkCount / 2)) ? pMaximum : (pPassword.ChunkCount / 2); _chunkGenerators[PasswordChunkType.Special].SetCharacterSet(pSpecialSymbols); int index = _random.Next(pPassword.ChunkCount); PasswordChunk specialChunk = _chunkGenerators[PasswordChunkType.Special].Get(pPassword.Chunks[index]); pPassword.ReplaceChunk(specialChunk, index); if (pMaximum == 1) { return; } List <int> validChunks = new List <int>(); for (int count = 0; count < pPassword.ChunkCount; ++count) { validChunks.Add(count); } SpecialChunkRemoveValid(validChunks, index); for (int count = 1; count < pMaximum; ++count) { if (validChunks.Count == 0) { break; } if (_random.Next(100) < 20) { int validIndex = _random.Next(validChunks.Count); index = validChunks[validIndex]; specialChunk = _chunkGenerators[PasswordChunkType.Special].Get(pPassword.Chunks[index]); pPassword.ReplaceChunk(specialChunk, index); SpecialChunkRemoveValid(validChunks, index); } } }
public new PasswordChunk Get(PasswordChunk pReferenceChunk) { PasswordChunk chunk = new PasswordChunk(); chunk.ChunkType = PasswordChunkType.Liquidified; char liquidCompanion = LiquidCompanions[Random.Next(LiquidCompanionsLength)]; string liquid = (LiquidCompanionsRestricted.Contains(liquidCompanion)) ? "r" : Utils.Coin() ? "l" : "r"; chunk.Text = $"{liquidCompanion}{liquid}{Vowels[Random.Next(VowelsLength)]}"; return(chunk); }