Implemented authentication.

Near feature complete.
This commit is contained in:
2018-08-05 19:32:07 +00:00
parent a14dddfc05
commit da9a91681c
29 changed files with 816 additions and 99 deletions

View File

@@ -10,6 +10,7 @@ std::string Storage::filename{};
std::map<Storage::KeyHandle, Storage::AppParam> Storage::appParams{};
std::map<Storage::KeyHandle, Storage::PrivKey> Storage::privKeys{};
std::map<Storage::KeyHandle, Storage::PubKey> Storage::pubKeys{};
std::map<Storage::KeyHandle, Storage::KeyCount> Storage::keyCounts{};
void Storage::init(const string &dirPrefix)
{
@@ -18,30 +19,27 @@ void Storage::init(const string &dirPrefix)
string line;
size_t lineNumber = 0;
base64::decoder decoder{};
while (getline(file, line))
{
auto strLineNum = to_string(lineNumber);
stringstream ss{ line };
string keyHStr, appStr, privStr, pubStr;
ss >> keyHStr >> appStr >> privStr >> pubStr;
string keyHStr, appStr, privStr, pubStr, keyCStr;
ss >> keyHStr >> appStr >> privStr >> pubStr >> keyCStr;
if (!ss)
throw runtime_error{ string{ "Invalid syntax of line " } + strLineNum };
char *endP = nullptr;
Storage::KeyHandle keyH{ strtoull(keyHStr.c_str(), &endP, 10) };
auto keyH{ static_cast<Storage::KeyHandle>(strtoull(keyHStr.c_str(), &endP, 10)) };
if (!endP)
throw runtime_error{ "Invalid keyhandle format on line " + strLineNum };
endP = nullptr;
auto keyC{ static_cast<Storage::KeyCount>(strtoull(keyCStr.c_str(), &endP, 10)) };
//if (appStr.size() != 32)
// throw runtime_error{ "Invalid length of app parameter on line " + strLineNum };
//if (privStr.size() != 32)
// throw runtime_error{ "Invalid length of private key on line " + strLineNum };
//if (pubStr.size() != 65)
// throw runtime_error{ "Invalid length of public key on line " + strLineNum };
if (!endP)
throw runtime_error{ "Invalid key count format on line " + strLineNum };
Storage::AppParam appParam{};
b64decode(appStr, appParam);
@@ -52,9 +50,17 @@ void Storage::init(const string &dirPrefix)
Storage::PubKey pubKey{};
b64decode(pubStr, pubKey);
clog << "Loaded key with pubkey: " << hex;
for (auto b : pubKey)
clog << static_cast<uint32_t>(b) << ' ';
clog << dec << endl;
Storage::appParams[keyH] = appParam;
Storage::privKeys[keyH] = privKey;
Storage::pubKeys[keyH] = pubKey;
Storage::keyCounts[keyH] = keyC;
lineNumber++;
}
@@ -63,14 +69,14 @@ void Storage::init(const string &dirPrefix)
void Storage::save()
{
ofstream file{ Storage::filename };
base64::encoder encoder{};
for (auto &keypair : Storage::appParams)
{
const auto keyID = keypair.first;
const auto appParam = keypair.second;
const auto privKey = Storage::privKeys[keypair.first];
const auto pubKey = Storage::pubKeys[keypair.first];
const auto& keyID = keypair.first;
const auto& appParam = keypair.second;
const auto& privKey = Storage::privKeys[keyID];
const auto& pubKey = Storage::pubKeys[keyID];
const auto& keyCount = Storage::keyCounts[keyID];
file << keyID;
file << ' ';
@@ -85,6 +91,8 @@ void Storage::save()
string pubKStr{};
b64encode(pubKey, pubKStr);
file << pubKStr << endl;
file << pubKStr << ' ';
file << keyCount << endl;
}
}