passgen/passgen.cpp

178 lines
4.8 KiB
C++
Raw Normal View History

#include <cctype>
#include <cstdlib>
#include <functional>
2024-04-28 10:01:50 +00:00
#include <iostream>
2024-04-28 10:32:48 +00:00
#include <fstream>
#include <optional>
#include <ostream>
2024-04-28 11:53:52 +00:00
#include <random>
2024-04-28 10:32:48 +00:00
#include <string>
#include <sstream>
2024-04-28 10:32:48 +00:00
#include <vector>
2024-04-28 10:01:50 +00:00
2024-04-28 11:03:20 +00:00
#include "pcg_random.hpp"
#include "eff_large_wordlist.txt.hpp"
#include "eff_short_wordlist_1.txt.hpp"
2024-04-28 11:03:20 +00:00
enum WordlistType { WORDLIST_EFF_LARGE, WORDLIST_EFF_SHORT, WORDLIST_CUSTOM };
struct PassgenOptions {
bool help = false;
WordlistType wordlist_type = WORDLIST_EFF_SHORT;
int wordcount = 5;
bool camelcase = false;
std::string seperator = "-";
std::optional<std::string> wordlist_path;
};
typedef std::function<void(PassgenOptions&)> NoArgHandle;
typedef std::function<void(PassgenOptions&, const std::string&)> OneArgHandle;
const std::unordered_map<std::string, NoArgHandle> NoArgs {
{"-h", [](PassgenOptions& s) { s.help = true; }},
{"--help", [](PassgenOptions& s) { s.help = true; }},
{"-s", [](PassgenOptions& s) { s.wordlist_type = WORDLIST_EFF_SHORT; }},
{"--short", [](PassgenOptions& s) { s.wordlist_type = WORDLIST_EFF_SHORT; }},
{"-l", [](PassgenOptions& s) { s.wordlist_type = WORDLIST_EFF_LARGE; }},
{"--large", [](PassgenOptions& s) { s.wordlist_type = WORDLIST_EFF_LARGE; }},
{"--camelcase", [](PassgenOptions& s) { s.camelcase = true; }},
{"-cc", [](PassgenOptions& s) { s.camelcase = true; }},
};
const std::unordered_map<std::string, OneArgHandle> OneArgs {
{"-p", [](PassgenOptions& s, const std::string& arg) {
s.wordlist_path = arg;
s.wordlist_type = WORDLIST_CUSTOM;
}},
{"--path", [](PassgenOptions& s, const std::string& arg) {
s.wordlist_path = arg;
s.wordlist_type = WORDLIST_CUSTOM;
}},
{"-wc", [](PassgenOptions& s, const std::string& arg) {
s.wordcount = std::stoi(arg);
}},
{"--wordcount", [](PassgenOptions& s, const std::string& arg) {
s.wordcount = std::stoi(arg);
}},
{"--seperator", [](PassgenOptions& s, const std::string& arg) {
s.seperator = arg;
}},
};
PassgenOptions parse_options(int argc, const char* argv[]) {
PassgenOptions settings;
for(int i = 1; i < argc; i++) {
std::string opt = argv[i];
if (auto j = NoArgs.find(opt); j != NoArgs.end()) {
j->second(settings);
} else if(auto k = OneArgs.find(opt); k != OneArgs.end()) {
if(++i < argc) {
k->second(settings, {argv[i]});
} else {
throw std::runtime_error {"missing param after " + opt};
std::exit(1);
}
} else {
std::cerr << "unrecognized command-line option " << opt << std::endl;
std::exit(1);
}
}
return settings;
}
std::vector<std::string> getWordlist(PassgenOptions opts)
2024-04-28 10:32:48 +00:00
{
std::vector<std::string> map;
if (opts.wordlist_type != WORDLIST_CUSTOM)
{
std::stringstream ss;
if (WORDLIST_EFF_SHORT) {
ss.write(reinterpret_cast<const char*>(eff_short_wordlist_1_txt), eff_short_wordlist_1_txt_len);
} else {
ss.write(reinterpret_cast<const char*>(eff_large_wordlist_txt), eff_large_wordlist_txt_len);
2024-04-28 10:32:48 +00:00
}
std::string line;
while (std::getline(ss, line)) {
map.push_back(line);
}
}
else if (opts.wordlist_path.has_value())
{
std::string path = opts.wordlist_path.value();
std::ifstream file(path);
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
map.push_back(line);
}
} else {
std::cout << "ERROR: failed to open '" << path << "'" << std::endl;
}
} else {
std::cout << "ERROR: no wordlist specified" << std::endl;
2024-04-28 10:32:48 +00:00
}
return map;
}
int main(int argc, const char *argv[]) {
PassgenOptions opts = parse_options(argc, argv);
if (opts.help) {
std::cout << R"(
Usage: passgen [options]
Options:
-h, --help Display this help message and exit
-s, --short Use the short EFF wordlist (dfault)
-l, --large Use the large EFF wordlist
--camelcase, -cc Generate passphrase in CamelCase format
-p <path>, --path <path>
Use a custom wordlist file located at <path>
-wc <count>, --wordcount <count>
Amount of words to use for passphrase, default = 5
--separator <seperator> Specify a character to use as a separator between words, default = `-`
Description:
Generate passphrases based on wordlists.
Examples:
passgen -h
passgen --short 4
passgen -p /path/to/custom_wordlist.txt --wordcount 4
)";
return 0;
2024-04-28 10:32:48 +00:00
}
std::vector<std::string> wordlist = getWordlist(opts);
2024-04-28 10:32:48 +00:00
if (wordlist.size() == 0) {
std::cerr << "Wordlist is empty";
2024-04-28 10:32:48 +00:00
return 1;
}
pcg_extras::seed_seq_from<std::random_device> seed_source;
pcg32 rng(seed_source);
for (int i = 0; i < opts.wordcount; i++) {
2024-04-28 11:53:52 +00:00
int random_int = std::uniform_int_distribution<int>(0, wordlist.size())(rng);
if (opts.camelcase) {
wordlist[random_int][0] = toupper(wordlist[random_int][0]);
}
2024-04-28 11:53:52 +00:00
std::cout << wordlist[random_int];
if (i+1 == opts.wordcount) {
2024-04-28 11:53:52 +00:00
std::cout << "\n";
} else {
std::cout << opts.seperator;
2024-04-28 11:53:52 +00:00
}
}
2024-04-28 10:01:50 +00:00
}