From 147fa762c880ceb93092482e4933852b6a4be10d Mon Sep 17 00:00:00 2001 From: nedko Date: Thu, 25 Jun 2026 16:06:13 +0300 Subject: [PATCH] Added API key generation --- main.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 5c98936..2ddf2ab 100644 --- a/main.cpp +++ b/main.cpp @@ -18,6 +18,56 @@ using nlohmann::json; using namespace std; +constexpr int API_KEY_LENGHT = 16; + +string generate_api_key(int len = API_KEY_LENGHT) +{ + string result = ""; + std::random_device rand; + std::uniform_int_distribution distribution(0, 63); + int single; + char symbol; + + if (len < 0) + { + len = API_KEY_LENGHT; + } + + for (int i = 0; i < len; ++i) + { + single = distribution(rand); + if (single <= 9) + { + symbol = '0' + single; + } + else if ((single >= 10) && (single <= 35)) + { + symbol = 'A' + single - 10; + } + else if ((single >= 36) && (single <= 61)) + { + symbol = 'a' + single - 36; + } + else if (62 == single) + { + symbol = '+'; + } + else if (63 == single) + { + symbol = '/'; + } + // This should never happen + else + { + symbol = '?'; + } + + result += symbol; + } + + return result; +} + void reload_container(TRMNLContainer& container, const string& filename) { json j; @@ -157,8 +207,7 @@ int main(int argc, char **argv) if (trmnl->api_key().empty()) { should_dump = true; - // TODO: Randomly generate key here - trmnl->api_key("nullptr"); + trmnl->api_key(generate_api_key()); } response["status"] = 200;