1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
#include <cctype>
#include <string>
#include <iostream>
#include <random>
/*
Generates a random string based on user's length specification
returns true if successful
*/
bool generate_key(int length, bool uppercase, bool lowercase, bool specialChars, bool numbers, std::string &keyStr)
{
if (!uppercase
&& !lowercase
&& !specialChars
&& !numbers)
{
//Nothing to do here, so we return.
return false;
}
//Values to store the range of the ASCII table
int lowerValue = std::numeric_limits<unsigned char>::min();
int maxValue = std::numeric_limits<unsigned char>::max();
//Random number generator to generate a character
std::random_device rd;
std::mt19937 valueGenerator(rd());
std::uniform_int_distribution<> dist(lowerValue, maxValue);
if (uppercase
&& !lowercase
&& !specialChars
&& !numbers)
{
//Create key out of uppercase letter only
}
else if (!uppercase
&& lowercase
&& !specialChars
&& !numbers)
{
//Create key with lowercase letters only
}
else if (!uppercase
&& !lowercase
&& specialChars
&& !numbers)
{
//Create key using special characters only
}
else if (!uppercase
&& !lowercase
&& !specialChars
&& numbers)
{
//Create key using numbers only
}
else if (uppercase
&& lowercase
&& !specialChars
&& !numbers)
{
//Create key out of uppercase and lower case letters only
}
else if (!uppercase
&& lowercase
&& specialChars
&& !numbers)
{
//Create key out of lowercase letters and special characters
}
else if (!uppercase
&& !lowercase
&& specialChars
&& numbers)
{
//Create key out of special characters and numbers
}
else if (uppercase
&& !lowercase
&& !specialChars
&& numbers)
{
//Create key out of uppercase letters and numbers
}
else if (!uppercase
&& lowercase
&& !specialChars
&& numbers)
{
//Create key out of lowercase letters and numbers
}
else if (uppercase
&& !lowercase
&& specialChars
&& !numbers)
{
//Create key out of uppercase letters and special characters
}
else if (!uppercase
&& lowercase
&& specialChars
&& numbers)
{
//Create key out of lowercase letters, special characters and numbers
}
else if (uppercase
&& !lowercase
&& specialChars
&& numbers)
{
//Create key out of uppercase letters, special characters and numbers
}
else if (uppercase
&& lowercase
&& !specialChars
&& numbers)
{
//Create key out of letters and numbers
}
else if (uppercase
&& lowercase
&& specialChars
&& !numbers)
{
//Create key out of letters and special characters
}
//Create key using all the available characters
unsigned char codeValue = ' ';
for (unsigned i = 0; i < length;)
{
codeValue = static_cast<char>(dist(valueGenerator));
if (std::isprint(codeValue))
{
std::cout << "Pushed character: " << codeValue << '\n';
keyStr.push_back(codeValue);
i++;
}
continue;
}
std::cout << "Generated code is: " << keyStr << '\n';
return true;
}
void skip_to_int()
{
//We found something that wasn't an integer
if (std::cin.fail())
{
std::cin.clear(); //We'd like to look at the characters
//Throw away all non digits
for (char ch; std::cin >> ch;)
{
if (std::isdigit(ch) || ch == '=')
{
std::cin.unget();
return;
}
}
}
std::cout << "No input\n";
}
int get_int(const std::string& sorry)
{
int n = 0;
while (true)
{
if (std::cin >> n)
{
return n;
}
std::cout << sorry;
skip_to_int();
}
}
/*Takes ranged input and returns the a validated integer*/
int get_int(int low, int high, const std::string& greeting, const std::string& sorry)
{
std::cout << greeting << ":[" << low << ':' << high << "]\n";
while (true)
{
int n = get_int("Sorry, that was not a number, please try again\n");
if (low <= n && n <= high)
{
return n;
}
std::cout << sorry << ":[" << low << ':' << high << "]\n";
}
}
char validate_character(const std::string& sorry)
{
char userChoice = ' ';
while (true)
{
if (std::cin >> userChoice)
{
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::isalpha(userChoice))
{
if (std::isupper(userChoice))
{
std::tolower(userChoice);
return userChoice;
}
return userChoice;
}
}
std::cout << sorry;
}
}
/*Returns a validated boolean*/
bool get_yes_or_no_answer(const std::string& greeting, const std::string& sorry)
{
std::cout << greeting << '\n';
while (true)
{
char userChoice = validate_character("You did not entered an alphabetical character.\n");
if (userChoice == 'y')
{
return true;
}
else if (userChoice == 'n')
{
return false;
}
std::cout << sorry;
}
}
bool can_generate_key()
{
int strLenght = get_int(1, std::numeric_limits<int>::max(), "Enter key length within range", "You are out or range");
bool includeUpper = get_yes_or_no_answer("Do you want to include uppercase letters?", "Invalid character input, please try again\n");
bool includeLower = get_yes_or_no_answer("Do you want to include lowercase letters?", "Invalid character input, please try again\n");
bool includeSpecial = get_yes_or_no_answer("Do you want to include special characters?", "Invalid character input, please try again\n");
bool includeNumbers = get_yes_or_no_answer("Do you want to include numbers?", "Invalid character input, please try again\n");
if (!includeUpper
&& !includeLower
&& !includeSpecial
&& !includeNumbers)
{
std::cout << "Key can't be generated, at least one condition needs to be true\n";
return false;
}
return false;//generate_key(strLenght, includeUpper, includeLower, includeSpecial, includeNumbers);
}
int main()
{
//Hardcoded values to test function
std::string CDKey = " ";
generate_key(16, true, true, true, true, CDKey);
return 0;
}
|