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
|
//
// Catfishes Password Creator, C++11 version (as supported by VS2010)
//
#include <algorithm>
#include <array>
#include <iostream>
#include <random>
#include <regex>
#include <sstream>
#include <string>
const size_t maxPasswordSize = 128;
int main(int argc, char **argv)
{
if (argc != 2)
{
std::cerr << "\nUsage:\n\tprogram.exe passwordLength" << std::endl;
return 1;
}
const std::string userParameter(argv[1]);
const std::regex naturalNumber("\\+?[[:digit:]]+");
if (!std::regex_match(userParameter, naturalNumber))
{
std::cerr << "\nExpected a positive integer, got this: `" << userParameter << "'." << std::endl;
return 1;
}
size_t passwordSize;
std::istringstream getPasswordSize(userParameter);
getPasswordSize >> passwordSize;
if (passwordSize == 0 || passwordSize > maxPasswordSize)
{
std::cerr << "\nCan't have a password of size " << passwordSize << "!" << std::endl;
return 1;
}
const std::array<char, 62> ingredients = {
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9'
};
std::string password(passwordSize, ' ');
std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_int_distribution<size_t> indexFarm(0, ingredients.size() - 1);
std::generate(password.begin(), password.end(), [&](){
return ingredients.at(indexFarm(generator));
});
std::cout << password << std::endl;
return 0;
}
|