keycode generation, encrypt, decrypt works

This commit is contained in:
Kevin Darby committed 2019-06-06 13:30:54 -04:00
1 parent 8a965d41ff
commit 6647b28a9f
6 files changed
+70 -44

No files matched your search

+18 -8
View File
@@ -23,6 +23,7 @@
#include <string>
#include <stdio.h>
#include <sstream>
#include <streambuf>
#include <ctime>
#include <cstdlib>
#include <vector>
@@ -40,7 +41,7 @@ std::string KeyCode::helpstr ()
{
std::ostringstream oss;
oss << "\n"
<< "ENCODE -keyfile:[keyfile] -pass:password -users:<nusers> -expiration:<1/23/2050>\n "
<< "ENCODE -keyfile:[keyfile] -pass:password -users:<nusers> -expiration:<2020-05-18>\n "
<< "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n "
<< "KEYGEN -keyfile:[keyfile_prefix]\n ";
@@ -49,8 +50,8 @@ std::string KeyCode::helpstr ()
std::string parse_date (std::string const &date)
{
auto the_date = bts::datetime::parse_simple_date_time_string(date);
return bts::datetime::simple_date_time_string(the_date);
auto the_date = bts::datetime::parse_iso_date_time_string(date);
return bts::datetime::iso_date_time_string(the_date);
}
@@ -137,7 +138,7 @@ void write_keycode(const std::string& users, const std::string& expiration, bts:
std::ostringstream out_stream;
auto nusers = (uint32_t) std::atoi(users.c_str());
auto expir = bts::datetime::parse_simple_date_time_string(expiration);
auto expir = bts::datetime::parse_iso_date_time_string(expiration);
auto expir_ll = bts::datetime::ptime_to_ll(expir);
auto now = bts::datetime::ll_now();
@@ -178,10 +179,16 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri
ekc.set_keycode(encrypted.str());
std::string pb;
google::protobuf::TextFormat::PrintToString(ekc, &pb);
std::cout << std::endl << std::endl;
std::cout << "#nusers: " << users << std::endl;
std::cout << "#expires: " << datetime::simple_date_time_string(datetime::ll_to_ptime(kc.expiration())) << " " << std::to_string(kc.expiration()) << std::endl;
std::cout << "#generation_time: " << datetime::simple_date_time_string(datetime::ll_to_ptime(kc.generation_time())) << " " << std::to_string(kc.generation_time()) << std::endl;
std::cout << pb;
std::cout << "EncryptedKeyCode {" << std::endl;
std::cout << " value {" << std::endl;
std::cout << " " << pb;
std::cout << " }" << std::endl;
std::cout << "}" << std::endl;
std::cout << std::endl << std::endl;
return 0;
}
}
@@ -208,10 +215,13 @@ int decode(const std::vector<std::string>& args)
//KeycodePayloadHelper helper;
bts::config::EncryptedKeyCode ekc;
std::fstream fs(source);
google::protobuf::io::IstreamInputStream pay_stream(&fs);
google::protobuf::TextFormat::Parse(&pay_stream, &ekc);
//google::protobuf::io::IstreamInputStream pay_stream(&fs);
//google::protobuf::TextFormat::Parse(&pay_stream, &ekc);
std::string str((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
config::internal::initialize_for_test(str);
ekc = config::get<bts::config::EncryptedKeyCode>();
std::cout << std::endl << std::endl;
bts::config::KeyCode kc; std::string string_out;
bts::crypto::decrypt_keycode(*pub, ekc, &kc);
google::protobuf::TextFormat::PrintToString(kc, &string_out);