This commit is contained in:
		
							parent
							
								
									37c156b24a
								
							
						
					
					
						commit
						a30101f3aa
					
				@ -10,16 +10,17 @@
 | 
				
			|||||||
std::string random_string(std::size_t length, bool punc) {
 | 
					std::string random_string(std::size_t length, bool punc) {
 | 
				
			||||||
    std::string CHARACTERS;
 | 
					    std::string CHARACTERS;
 | 
				
			||||||
    std::string ALPHANUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 | 
					    std::string ALPHANUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 | 
				
			||||||
    std::string SPECIALS = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
 | 
					    const std::string SPECIALS = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (punc) {
 | 
					    if (punc) {
 | 
				
			||||||
        CHARACTERS = ALPHANUM.append(SPECIALS);
 | 
					        CHARACTERS = ALPHANUM.append(SPECIALS);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        CHARACTERS = ALPHANUM;
 | 
					        CHARACTERS = ALPHANUM;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    std::random_device random_device;
 | 
					    std::random_device random_device;
 | 
				
			||||||
    std::mt19937 generator(random_device());
 | 
					    std::mt19937 generator(random_device());
 | 
				
			||||||
    std::uniform_int_distribution<> distribution(0.0, CHARACTERS.size() - 1.0);
 | 
					    std::uniform_int_distribution<> distribution(0, static_cast<int>(CHARACTERS.size() - 1));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    std::string random_string;
 | 
					    std::string random_string;
 | 
				
			||||||
    for (std::size_t i = 0; i < length; ++i) {
 | 
					    for (std::size_t i = 0; i < length; ++i) {
 | 
				
			||||||
@ -28,8 +29,8 @@ std::string random_string(std::size_t length, bool punc) {
 | 
				
			|||||||
    return random_string;
 | 
					    return random_string;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void show_usage(std::string binname) {
 | 
					void show_usage(const std::string& name) {
 | 
				
			||||||
    std::cerr << "Usage: " << binname << " [OPTIONS] " << std::endl
 | 
					    std::cerr << "Usage: " << name << " [OPTIONS] " << std::endl
 | 
				
			||||||
              << "Options:" << std::endl
 | 
					              << "Options:" << std::endl
 | 
				
			||||||
              << "\t-h, --help          \t\tShow this help message" << std::endl
 | 
					              << "\t-h, --help          \t\tShow this help message" << std::endl
 | 
				
			||||||
              << "\t-l, --length [n]    \t\tThe length of the password (default: 32)" << std::endl
 | 
					              << "\t-l, --length [n]    \t\tThe length of the password (default: 32)" << std::endl
 | 
				
			||||||
@ -38,43 +39,41 @@ void show_usage(std::string binname) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main(int argc, char *argv[]) {
 | 
					int main(int argc, char *argv[]) {
 | 
				
			||||||
    std::string passlenstr;
 | 
					    int passwordLength = 0;
 | 
				
			||||||
    int passlen = 0;
 | 
					 | 
				
			||||||
    bool punc = false;
 | 
					    bool punc = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (argc < 1) {
 | 
					    if (argc < 1) {
 | 
				
			||||||
        std::cout << random_string(32, false) << std::endl;
 | 
					        std::cout << random_string(32, false) << std::endl;
 | 
				
			||||||
        return 0;
 | 
					        return 0;
 | 
				
			||||||
    } else {
 | 
					    }
 | 
				
			||||||
        for (int i = 1; i < argc; ++i) {
 | 
					    for (int i = 1; i < argc; ++i) {
 | 
				
			||||||
            std::string arg = argv[i];
 | 
					        std::string arg = argv[i];
 | 
				
			||||||
            if ((arg == "-h") || (arg == "--help")) {
 | 
					        if ((arg == "-h") || (arg == "--help")) {
 | 
				
			||||||
                show_usage(argv[0]);
 | 
					            show_usage(argv[0]);
 | 
				
			||||||
                return 0;
 | 
					            return 0;
 | 
				
			||||||
            } else if (arg == "-p") {
 | 
					        }
 | 
				
			||||||
                punc = true;
 | 
					        if (arg == "-p") {
 | 
				
			||||||
            } else if ((arg == "-l") || (arg == "--length")) {
 | 
					            punc = true;
 | 
				
			||||||
                if (not argv[i +1]) {
 | 
					        } else if ((arg == "-l") || (arg == "--length")) {
 | 
				
			||||||
                    std::cerr << "Specify a password length" << std::endl;
 | 
					            if (not argv[i +1]) {
 | 
				
			||||||
                    return 1;
 | 
					                std::cerr << "Specify a password length" << std::endl;
 | 
				
			||||||
                } else {
 | 
					                return 1;
 | 
				
			||||||
                    passlenstr = argv[i + 1];
 | 
					            }
 | 
				
			||||||
                    try {
 | 
					            std::string passwordLengthStr = argv[i + 1];
 | 
				
			||||||
                        passlen = std::stoi(passlenstr);
 | 
					            try {
 | 
				
			||||||
                    } catch (const std::invalid_argument& e){
 | 
					                passwordLength = std::stoi(passwordLengthStr);
 | 
				
			||||||
                        std::cerr << "Length must be a valid integer" << std::endl;
 | 
					            } catch ([[maybe_unused]] const std::invalid_argument& e){
 | 
				
			||||||
                        return 1;
 | 
					                std::cerr << "Length must be a valid integer" << std::endl;
 | 
				
			||||||
                    }
 | 
					                return 1;
 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (passlen == 0) {
 | 
					 | 
				
			||||||
            passlen = 32;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        std::string password = random_string(passlen, punc);
 | 
					 | 
				
			||||||
        std::cout << password << std::endl;
 | 
					 | 
				
			||||||
        return 0;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (passwordLength == 0) {
 | 
				
			||||||
 | 
					        passwordLength = 32;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    std::string password = random_string(passwordLength, punc);
 | 
				
			||||||
 | 
					    std::cout << password << std::endl;
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user