99 lines
2.9 KiB
C++
99 lines
2.9 KiB
C++
// keygen - generate microReticulum identity keypairs
|
|
//
|
|
// Example:
|
|
// ./keygen --quantity 6 --format tsv
|
|
// ./keygen -q 6 -f json
|
|
//
|
|
// $Header$
|
|
// $Id$
|
|
|
|
#include <Identity.h>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
|
|
static void usage(const char* argv0) {
|
|
std::cerr
|
|
<< "Usage: " << argv0 << " --quantity N [--format tsv|json] [--public]\n"
|
|
<< " -q, --quantity Number of identities to generate (required)\n"
|
|
<< " -f, --format Output format: tsv (default) or json\n"
|
|
<< " --public Also include public_key in output\n";
|
|
}
|
|
|
|
static bool is_flag(const std::string& a, const char* s) { return a == s; }
|
|
|
|
int main(int argc, char** argv) {
|
|
try {
|
|
int quantity = -1;
|
|
std::string format = "tsv";
|
|
bool include_public = false;
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
std::string a(argv[i]);
|
|
|
|
if (is_flag(a, "-h") || is_flag(a, "--help")) {
|
|
usage(argv[0]);
|
|
return 0;
|
|
} else if (is_flag(a, "-q") || is_flag(a, "--quantity")) {
|
|
if (i + 1 >= argc) throw std::runtime_error("Missing value for --quantity");
|
|
quantity = std::stoi(argv[++i]);
|
|
} else if (is_flag(a, "-f") || is_flag(a, "--format")) {
|
|
if (i + 1 >= argc) throw std::runtime_error("Missing value for --format");
|
|
format = argv[++i];
|
|
} else if (is_flag(a, "--public")) {
|
|
include_public = true;
|
|
} else {
|
|
throw std::runtime_error("Unknown argument: " + a);
|
|
}
|
|
}
|
|
|
|
if (quantity <= 0) {
|
|
usage(argv[0]);
|
|
return 2;
|
|
}
|
|
if (!(format == "tsv" || format == "json")) {
|
|
throw std::runtime_error("Invalid --format (must be tsv or json)");
|
|
}
|
|
|
|
if (format == "tsv") {
|
|
// header row
|
|
std::cout << "n\tid_hex\tprivate_key_hex";
|
|
if (include_public) std::cout << "\tpublic_key_hex";
|
|
std::cout << "\n";
|
|
|
|
for (int n = 1; n <= quantity; n++) {
|
|
RNS::Identity id(true);
|
|
std::cout
|
|
<< n << "\t"
|
|
<< id.hash().toHex() << "\t"
|
|
<< id.get_private_key().toHex();
|
|
if (include_public) std::cout << "\t" << id.get_public_key().toHex();
|
|
std::cout << "\n";
|
|
}
|
|
} else {
|
|
// json
|
|
std::cout << "[\n";
|
|
for (int n = 1; n <= quantity; n++) {
|
|
RNS::Identity id(true);
|
|
|
|
std::cout << " {\n";
|
|
std::cout << " \"n\": " << n << ",\n";
|
|
std::cout << " \"id\": \"" << id.hash().toHex() << "\",\n";
|
|
std::cout << " \"private_key\": \"" << id.get_private_key().toHex() << "\"";
|
|
if (include_public) {
|
|
std::cout << ",\n \"public_key\": \"" << id.get_public_key().toHex() << "\"\n";
|
|
} else {
|
|
std::cout << "\n";
|
|
}
|
|
std::cout << " }" << (n == quantity ? "\n" : ",\n");
|
|
}
|
|
std::cout << "]\n";
|
|
}
|
|
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "Error: " << e.what() << "\n";
|
|
return 1;
|
|
}
|
|
}
|