|
| 1 | +#include "PasswordGenerator.h" |
| 2 | +#include "CharacterSets.h" |
| 3 | +#include <stdexcept> |
| 4 | + |
| 5 | +PasswordGenerator::PasswordGenerator(IRandom& rng) : rng_{rng} {} |
| 6 | + |
| 7 | +void PasswordGenerator::ensureValid(const UserPreferences& prefs) const { |
| 8 | + if (prefs.length == 0) throw std::invalid_argument("Password length must be > 0"); |
| 9 | + if (!prefs.includeLowercase && !prefs.includeUppercase && !prefs.includeDigits && !prefs.includeSymbols) { |
| 10 | + throw std::invalid_argument("At least one character set must be enabled"); |
| 11 | + } |
| 12 | + std::size_t types = (prefs.includeLowercase?1:0) + (prefs.includeUppercase?1:0) + |
| 13 | + (prefs.includeDigits?1:0) + (prefs.includeSymbols?1:0); |
| 14 | + if (prefs.requireEachSelectedType && prefs.length < types) { |
| 15 | + throw std::invalid_argument("Length too short to include at least one of each selected type"); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +std::string PasswordGenerator::buildAlphabet(const UserPreferences& prefs) const { |
| 20 | + std::string alphabet; |
| 21 | + if (prefs.includeLowercase) alphabet += charset::LOWER; |
| 22 | + if (prefs.includeUppercase) alphabet += charset::UPPER; |
| 23 | + if (prefs.includeDigits) alphabet += charset::DIGITS; |
| 24 | + if (prefs.includeSymbols) alphabet += charset::SYMBOLS; |
| 25 | + return alphabet; |
| 26 | +} |
| 27 | + |
| 28 | +char PasswordGenerator::randomFrom(const std::string& alphabet) { |
| 29 | + return alphabet[rng_.nextIndex(alphabet.size())]; |
| 30 | +} |
| 31 | + |
| 32 | +void PasswordGenerator::fisherYatesShuffle(std::string& s) { |
| 33 | + if (s.size() <= 1) return; |
| 34 | + for (std::size_t i = s.size() - 1; i > 0; --i) { |
| 35 | + std::size_t j = rng_.nextIndex(i + 1); |
| 36 | + std::swap(s[i], s[j]); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +std::string PasswordGenerator::generateWithEachType(const UserPreferences& prefs, const std::string& alphabet) { |
| 41 | + std::string pw; |
| 42 | + pw.reserve(prefs.length); |
| 43 | + |
| 44 | + // Ensure at least one char from each selected set |
| 45 | + if (prefs.includeLowercase) pw.push_back(randomFrom(charset::LOWER)); |
| 46 | + if (prefs.includeUppercase) pw.push_back(randomFrom(charset::UPPER)); |
| 47 | + if (prefs.includeDigits) pw.push_back(randomFrom(charset::DIGITS)); |
| 48 | + if (prefs.includeSymbols) pw.push_back(randomFrom(charset::SYMBOLS)); |
| 49 | + |
| 50 | + // Fill remaining with random chars from the combined alphabet |
| 51 | + while (pw.size() < prefs.length) { |
| 52 | + pw.push_back(randomFrom(alphabet)); |
| 53 | + } |
| 54 | + |
| 55 | + fisherYatesShuffle(pw); |
| 56 | + return pw; |
| 57 | +} |
| 58 | + |
| 59 | +std::string PasswordGenerator::generate(const UserPreferences& prefs) { |
| 60 | + ensureValid(prefs); |
| 61 | + const std::string alphabet = buildAlphabet(prefs); |
| 62 | + if (prefs.requireEachSelectedType) return generateWithEachType(prefs, alphabet); |
| 63 | + |
| 64 | + std::string pw; |
| 65 | + pw.reserve(prefs.length); |
| 66 | + for (std::size_t i = 0; i < prefs.length; ++i) { |
| 67 | + pw.push_back(randomFrom(alphabet)); |
| 68 | + } |
| 69 | + return pw; |
| 70 | +} |
| 71 | + |
| 72 | +std::vector<std::string> PasswordGenerator::generateMany(const UserPreferences& prefs) { |
| 73 | + if (prefs.count == 0) throw std::invalid_argument("Count must be > 0"); |
| 74 | + std::vector<std::string> out; |
| 75 | + out.reserve(prefs.count); |
| 76 | + for (std::size_t i = 0; i < prefs.count; ++i) { |
| 77 | + out.push_back(generate(prefs)); |
| 78 | + } |
| 79 | + return out; |
| 80 | +} |
0 commit comments