passgen/passgen.cpp

63 lines
1.2 KiB
C++

#include <format>
#include <iostream>
#include <fstream>
#include <random>
#include <string>
#include <vector>
#include "pcg_random.hpp"
std::vector<std::string> readWordlist(std::string filePath)
{
std::vector<std::string> map;
std::ifstream file(filePath);
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
map.push_back(line);
}
} else {
std::cout << std::format("ERROR: failed to open '{}'\n", filePath);
}
return map;
}
int main(int argc, char *argv[]) {
std::string path;
int count;
if (argc == 3) {
path = argv[1];
count = atoi(argv[2]);
} else if (argc == 1) {
path = "eff_large_wordlist.txt";
count = 5;
} else {
std::cout << "usage: passgen <wordlist_path> <wordcount>\n";
return 1;
}
std::vector<std::string> wordlist = readWordlist(path);
if (wordlist.size() == 0) {
return 1;
}
pcg_extras::seed_seq_from<std::random_device> seed_source;
pcg32 rng(seed_source);
for (int i = 0; i < count; i++) {
int random_int = std::uniform_int_distribution<int>(0, wordlist.size())(rng);
std::cout << wordlist[random_int];
if (i+1 == count) {
std::cout << "\n";
} else {
std::cout << "-";
}
}
}